本文整理汇总了Java中org.springframework.mock.web.MockHttpServletResponse类的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletResponse类的具体用法?Java MockHttpServletResponse怎么用?Java MockHttpServletResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MockHttpServletResponse类属于org.springframework.mock.web包,在下文中一共展示了MockHttpServletResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verifyResettingContexPath
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyResettingContexPath() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath(CONST_CONTEXT_PATH);
final MockRequestContext context = new MockRequestContext();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
this.action.doExecute(context);
assertEquals(CONST_CONTEXT_PATH + '/', this.warnCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.tgtCookieGenerator.getCookiePath());
request.setContextPath(CONST_CONTEXT_PATH_2);
this.action.doExecute(context);
assertNotSame(CONST_CONTEXT_PATH_2 + '/', this.warnCookieGenerator.getCookiePath());
assertNotSame(CONST_CONTEXT_PATH_2 + '/', this.tgtCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.warnCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.tgtCookieGenerator.getCookiePath());
}
示例2: run_on_valid_response
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void run_on_valid_response() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
MockHttpServletResponse response = new MockHttpServletResponse();
context.setResponseGZipped(false);
context.setResponse(response);
InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
context.setResponseDataStream(in);
filter.run();
assertEquals("UTF-8", response.getCharacterEncoding());
assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
示例3: verifySuccessfulAuthenticationWithServiceAndWarn
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifySuccessfulAuthenticationWithServiceAndWarn() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
final MockRequestContext context = new MockRequestContext();
WebUtils.putLoginTicket(context, "LOGIN");
request.addParameter("lt", "LOGIN");
request.addParameter("username", "test");
request.addParameter("password", "test");
request.addParameter("warn", "true");
request.addParameter("service", "test");
context.setExternalContext(new ServletExternalContext(
new MockServletContext(), request, response));
final Credential c = org.jasig.cas.authentication.TestUtils.getCredentialsWithSameUsernameAndPassword();
putCredentialInRequestScope(context, c);
final MessageContext messageContext = mock(MessageContext.class);
assertEquals("success", this.action.submit(context, c, messageContext).getId());
assertNotNull(response.getCookie(this.warnCookieGenerator.getCookieName()));
}
示例4: testFindRoles
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void testFindRoles() throws Exception {
RoleEntity dbResult = new RoleEntity()
.setId("123")
.setCode("12345")
.setDescription("Description 12345");
Page<RoleEntity> pageResponseBody = new PageImpl<>(Arrays.asList(dbResult));
Page<RoleRestData> expectedResponseBody = new PageImpl<>(Arrays.asList(RoleRestData.builder()
.fromRoleEntity(dbResult).build()));
when(roleService.findRoles(anyString(), any())).thenReturn(pageResponseBody);
ResultActions resultActions = mockMvc.perform(get("/api/roles")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("role-read-all"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
verify(roleService).findRoles(anyString(), any());
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expectedResponseBody));
}
示例5: verifyWrongSecret
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyWrongSecret() throws Exception {
clearAllServices();
final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT
+ OAuthConstants.ACCESS_TOKEN_URL);
mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
mockRequest.setParameter(OAuthConstants.CODE, CODE);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
((OAuth20WrapperController) oauth20WrapperController)
.getServicesManager().save(getRegisteredService(REDIRECT_URI, WRONG_CLIENT_SECRET));
oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(400, mockResponse.getStatus());
assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:20,代码来源:OAuth20AccessTokenControllerTests.java
示例6: should_upload_and_delete_a_document
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void should_upload_and_delete_a_document() throws Exception {
final MockHttpServletResponse response = mvc.perform(fileUpload("/documents")
.file(FILE)
.param("classification", Classifications.PRIVATE.toString())
.headers(headers))
.andReturn().getResponse();
final String url = getSelfUrlFromResponse(response);
mvc.perform(delete(url)
.headers(headers))
.andExpect(status().is(204));
mvc.perform(get(url)
.headers(headers))
.andExpect(status().isNotFound());
}
示例7: verifyValidServiceTicketAndFormatAsJson
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyValidServiceTicketAndFormatAsJson() throws Exception {
final Service svc = CoreAuthenticationTestUtils.getService("proxyService");
final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), svc);
final TicketGrantingTicket tId = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
final ServiceTicket sId = getCentralAuthenticationService().grantServiceTicket(tId.getId(), svc, ctx);
final MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter(SERVICE_PARAM, svc.getId());
request.addParameter(TICKET_PARAM, sId.getId());
request.addParameter("format", ValidationResponseType.JSON.name());
final ModelAndView modelAndView = this.serviceValidateController.handleRequestInternal(request, new MockHttpServletResponse());
assertTrue(modelAndView.getView().toString().contains("Json"));
}
示例8: testDoFilterInternalWithInvalidOrgName
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void testDoFilterInternalWithInvalidOrgName() throws IOException, ServletException {
JwtPayloadHelper payload = new JwtPayloadHelper()
.withName("invalid-name")
.withOrgType(ORG_TYPE);
request.addHeader("Authorization", JwtTestHelper.createJwt(payload));
JwtAuthorizationFilter testJwtAuthFilter = new JwtAuthorizationFilter(authenticationManager);
PowerMockito.mockStatic(SecurityContextHolder.class);
SecurityContext mockSecurityContext = PowerMockito.mock(SecurityContext.class);
PowerMockito.when(SecurityContextHolder.getContext()).thenReturn(mockSecurityContext);
testJwtAuthFilter.doFilterInternal(request, response, filterChain);
verify(filterChain, times(1)).doFilter(any(MockHttpServletRequest.class), any(MockHttpServletResponse.class));
verify(SecurityContextHolder.getContext(), times(0)).setAuthentication(any(UsernamePasswordAuthenticationToken.class));
}
示例9: verifyOK
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyOK() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + '?' + OAuthConstants.CODE + '=' + SERVICE_TICKET, map.get("callbackUrl"));
}
示例10: testJWTFilter
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的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);
}
示例11: testRenewWithServiceAndBadCredentials
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void testRenewWithServiceAndBadCredentials() throws Exception {
final String ticketGrantingTicket = getCentralAuthenticationService()
.createTicketGrantingTicket(
TestUtils.getCredentialsWithSameUsernameAndPassword());
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockRequestContext context = new MockRequestContext();
context.getFlowScope().put("ticketGrantingTicketId", ticketGrantingTicket);
request.addParameter("renew", "true");
request.addParameter("service", "test");
context.setExternalContext(new ServletExternalContext(
new MockServletContext(), request, new MockHttpServletResponse()));
context.getRequestScope().put("credentials",
TestUtils.getCredentialsWithDifferentUsernameAndPassword());
context.getRequestScope().put(
"org.springframework.validation.BindException.credentials",
new BindException(TestUtils
.getCredentialsWithDifferentUsernameAndPassword(),
"credentials"));
// this.action.bind(context);
// assertEquals("error", this.action.submit(context).getId());
}
示例12: verifyClientNoCasService
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyClientNoCasService() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuth20Constants.ACCESS_TOKEN_URL);
mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuth20Constants.CLIENT_SECRET, CLIENT_SECRET);
mockRequest.setParameter(OAuth20Constants.GRANT_TYPE, OAuth20GrantTypes.AUTHORIZATION_CODE.name().toLowerCase());
final Principal principal = createPrincipal();
final RegisteredService registeredService = getRegisteredService(REDIRECT_URI, CLIENT_SECRET);
final OAuthCode code = addCode(principal, registeredService);
mockRequest.setParameter(OAuth20Constants.CODE, code.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
requiresAuthenticationInterceptor.preHandle(mockRequest, mockResponse, null);
oAuth20AccessTokenController.handleRequest(mockRequest, mockResponse);
assertEquals(HttpStatus.SC_UNAUTHORIZED, mockResponse.getStatus());
assertEquals(ERROR_EQUALS + OAuth20Constants.INVALID_REQUEST, mockResponse.getContentAsString());
}
示例13: verifyClientNoClientSecret
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyClientNoClientSecret() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuth20Constants.ACCESS_TOKEN_URL);
mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuth20Constants.GRANT_TYPE, OAuth20GrantTypes.AUTHORIZATION_CODE.name().toLowerCase());
final Principal principal = createPrincipal();
final RegisteredService service = addRegisteredService();
final OAuthCode code = addCode(principal, service);
mockRequest.setParameter(OAuth20Constants.CODE, code.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
requiresAuthenticationInterceptor.preHandle(mockRequest, mockResponse, null);
oAuth20AccessTokenController.handleRequest(mockRequest, mockResponse);
assertEquals(HttpStatus.SC_UNAUTHORIZED, mockResponse.getStatus());
assertEquals(ERROR_EQUALS + OAuth20Constants.INVALID_REQUEST, mockResponse.getContentAsString());
}
示例14: verifyResettingContextPath
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyResettingContextPath() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath(CONST_CONTEXT_PATH);
final MockRequestContext context = new MockRequestContext();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
this.action.doExecute(context);
assertEquals(CONST_CONTEXT_PATH + '/', this.warnCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.tgtCookieGenerator.getCookiePath());
request.setContextPath(CONST_CONTEXT_PATH_2);
this.action.doExecute(context);
assertNotSame(CONST_CONTEXT_PATH_2 + '/', this.warnCookieGenerator.getCookiePath());
assertNotSame(CONST_CONTEXT_PATH_2 + '/', this.tgtCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.warnCookieGenerator.getCookiePath());
assertEquals(CONST_CONTEXT_PATH + '/', this.tgtCookieGenerator.getCookiePath());
}
示例15: verifyValidServiceTicket
import org.springframework.mock.web.MockHttpServletResponse; //导入依赖的package包/类
@Test
public void verifyValidServiceTicket() throws Exception {
final AuthenticationContext ctx = org.jasig.cas.authentication.TestUtils
.getAuthenticationContext(getAuthenticationSystemSupport(), SERVICE);
final TicketGrantingTicket tId = getCentralAuthenticationService()
.createTicketGrantingTicket(ctx);
final ServiceTicket sId = getCentralAuthenticationService().grantServiceTicket(tId.getId(),
SERVICE, ctx);
final MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("service", SERVICE.getId());
request.addParameter("ticket", sId.getId());
assertEquals(AbstractServiceValidateController.DEFAULT_SERVICE_SUCCESS_VIEW_NAME,
this.serviceValidateController.handleRequestInternal(request,
new MockHttpServletResponse()).getViewName());
}