本文整理汇总了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");
}