本文整理匯總了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);
}
示例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");
}
示例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());
}
示例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");
}
示例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());
}
示例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());
}
示例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);
}
示例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);
}
示例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();
}
示例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");
}
示例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);
}
示例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));
}
示例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);
}
示例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");
}