当前位置: 首页>>代码示例>>Java>>正文


Java MockHttpServletRequest.addHeader方法代码示例

本文整理汇总了Java中org.springframework.mock.web.MockHttpServletRequest.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletRequest.addHeader方法的具体用法?Java MockHttpServletRequest.addHeader怎么用?Java MockHttpServletRequest.addHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.mock.web.MockHttpServletRequest的用法示例。


在下文中一共展示了MockHttpServletRequest.addHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testJWTFilter

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testJWTFilter() throws Exception {
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
        "test-user",
        "test-password",
        Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.USER))
    );
    String jwt = tokenProvider.createToken(authentication, false);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
    request.setRequestURI("/api/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain = new MockFilterChain();
    jwtFilter.doFilter(request, response, filterChain);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("test-user");
    assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials().toString()).isEqualTo(jwt);
}
 
开发者ID:deepu105,项目名称:spring-io,代码行数:19,代码来源:JWTFilterTest.java

示例2: onSimpleCollectionGetShouldReturnCollectionOfResources

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/");
	request.setRequestURI("/api/tasks/");
	request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	servlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data[0].type");
	assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
	assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
开发者ID:crnk-project,项目名称:crnk-framework,代码行数:27,代码来源:CrnkServletTest.java

示例3: testGetUrl

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testGetUrl() {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/");
	request.setRequestURI("/api/tasks/");
	request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
	request.addHeader("Accept", "*/*");
	request.setServerName("test");
	request.setServerPort(1234);


	ServletRequestContext context = new ServletRequestContext(servletContext, request, response, "/api");

	Assert.assertEquals("http://test:1234/api", context.getBaseUrl());
	Assert.assertEquals("/tasks/", context.getPath());
}
 
开发者ID:crnk-project,项目名称:crnk-framework,代码行数:20,代码来源:ServletRequestContextTest.java

示例4: onSimpleResourceGetShouldReturnOneResource

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/1");
	request.setRequestURI("/api/tasks/1");
	request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	servlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data.type");
	assertJsonPartEquals("\"1\"", responseContent, "data.id");
	assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
开发者ID:crnk-project,项目名称:crnk-framework,代码行数:27,代码来源:CrnkServletTest.java

示例5: ensureAltRemoteIpHeaderShouldBeChecked

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void ensureAltRemoteIpHeaderShouldBeChecked() {
    final BaseSpnegoKnownClientSystemsFilterAction action =
            new BaseSpnegoKnownClientSystemsFilterAction("^74\\.125\\..+", "alternateRemoteIp", 120);

    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteAddr("555.555.555.555");
    req.addHeader("alternateRemoteIp", ALTERNATE_REMOTE_IP);
    final ServletExternalContext extCtx = new ServletExternalContext(
            new MockServletContext(), req,
            new MockHttpServletResponse());
    ctx.setExternalContext(extCtx);

    final Event ev = action.doExecute(ctx);
    assertEquals(ev.getId(), new EventFactorySupport().yes(this).getId());
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:18,代码来源:AllSpnegoKnownClientSystemsFilterActionTests.java

示例6: ensureAltRemoteIpHeaderShouldBeChecked

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void ensureAltRemoteIpHeaderShouldBeChecked() {
    final BaseSpnegoKnownClientSystemsFilterAction action =
            new BaseSpnegoKnownClientSystemsFilterAction("^74\\.125\\..+", "alternateRemoteIp");

    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteAddr("555.555.555.555");
    req.addHeader("alternateRemoteIp", "74.125.136.102");
    final ServletExternalContext extCtx = new ServletExternalContext(
            new MockServletContext(), req,
            new MockHttpServletResponse());
    ctx.setExternalContext(extCtx);

    final Event ev = action.doExecute(ctx);
    assertEquals(ev.getId(), new EventFactorySupport().yes(this).getId());
}
 
开发者ID:yuweijun,项目名称:cas-server-4.2.1,代码行数:18,代码来源:AllSpnegoKnownClientSystemsFilterActionTest.java

示例7: testMNT_11990

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testMNT_11990() throws Exception
{
    MockHttpServletRequest lockRequest = new MockHttpServletRequest(); 
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 3600);        
    lockRequest.addHeader(WebDAV.HEADER_IF, "(<" + WebDAV.makeLockToken(fileNodeRef, userName) + ">)");
    lockRequest.setRequestURI("/" + TEST_FILE_NAME);

    lockMethod.setDetails(lockRequest, new MockHttpServletResponse(), davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            try
            {
                lockMethod.executeImpl();
                fail("Lock should not be refreshed for non-locked file.");
            }
            catch (WebDAVServerException e)
            {
                assertEquals(e.getHttpStatusCode(), HttpServletResponse.SC_BAD_REQUEST);
            }
            return null;
        }
    };
    
    // try to refresh lock for non-locked node        
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:34,代码来源:LockMethodTest.java

示例8: authenticateWithEmptyTokenTest

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void authenticateWithEmptyTokenTest() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(authHeaderName, "");

    final Authentication authentication = tokenService.authenticate(request);

    assertNull(authentication);
}
 
开发者ID:akraskovski,项目名称:product-management-system,代码行数:10,代码来源:TokenServiceTest.java

示例9: testJWTFilterInvalidToken

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testJWTFilterInvalidToken() throws Exception {
    String jwt = "wrong_jwt";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
    request.setRequestURI("/api/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain = new MockFilterChain();
    jwtFilter.doFilter(request, response, filterChain);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
 
开发者ID:torgcrm,项目名称:TorgCRM-Server,代码行数:13,代码来源:JWTFilterTest.java

示例10: testJWTFilterWrongScheme

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testJWTFilterWrongScheme() throws Exception {
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
        "test-user",
        "test-password",
        Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.USER))
    );
    String jwt = tokenProvider.createToken(authentication, false);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Basic " + jwt);
    request.setRequestURI("/api/test");
}
 
开发者ID:deepu105,项目名称:spring-io,代码行数:13,代码来源:JWTFilterTest.java

示例11: testMatchingConditionWithNoMatch_shouldReturnNull

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testMatchingConditionWithNoMatch_shouldReturnNull() {
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.addHeader("Accept", "acceptedMediaType-v3.0+json");
    VersionRequestCondition matchingCondition = versionRequestCondition.getMatchingCondition(httpServletRequest);
    assertNull(matchingCondition);
}
 
开发者ID:fintonic,项目名称:versioning-spring-boot-starter,代码行数:8,代码来源:VersionRequestConditionTest.java

示例12: testExtractTraceIdFromRequest_missingSpan

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testExtractTraceIdFromRequest_missingSpan() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader(TRACE_ID_HEADER, TEST_TRACE_ID);

	String traceId = this.extractor.extractTraceIdFromRequest(request);

	assertThat(traceId, is(TEST_TRACE_ID));
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gcp,代码行数:10,代码来源:XCloudTraceIdExtractorTests.java

示例13: authenticateIfUserChangedPasswordTest

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void authenticateIfUserChangedPasswordTest() {
    final User user = createUser();
    final String token = tokenService.generate(user.getUsername(), user.getPassword()).getToken();

    user.setPassword(randomAlphabetic(20));
    userService.update(user);

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(authHeaderName, token);

    final Authentication authentication = tokenService.authenticate(request);

    assertNull(authentication);
}
 
开发者ID:akraskovski,项目名称:product-management-system,代码行数:16,代码来源:TokenServiceTest.java

示例14: defaultCookieGood

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void defaultCookieGood() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("user-agent", "the agent");
    final DefaultCasCookieValueManager mgmr = new DefaultCasCookieValueManager(new DefaultCipherExecutor(ENC_KEY, SIGN_KEY));
    assertNotNull(mgmr.buildCookieValue("cas", request));
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:8,代码来源:DefaultCasCookieValueManagerTests.java

示例15: onSimpleResourceGetShouldReturnOneResource

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
	MockFilterChain filterChain = new MockFilterChain();

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath(null);
	request.setPathInfo(null);
	request.setRequestURI("/api/tasks/1");
	request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	filter.doFilter(request, response, filterChain);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data.type");
	assertJsonPartEquals("\"1\"", responseContent, "data.id");
	assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
开发者ID:crnk-project,项目名称:crnk-framework,代码行数:29,代码来源:CrnkFilterTest.java


注:本文中的org.springframework.mock.web.MockHttpServletRequest.addHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。