當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。