本文整理汇总了Java中org.springframework.mock.web.MockHttpServletRequest类的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletRequest类的具体用法?Java MockHttpServletRequest怎么用?Java MockHttpServletRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MockHttpServletRequest类属于org.springframework.mock.web包,在下文中一共展示了MockHttpServletRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAddEventWithWebAuthenticationDetails
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testAddEventWithWebAuthenticationDetails() {
HttpSession session = new MockHttpSession(null, "test-session-id");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
request.setRemoteAddr("1.2.3.4");
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
Map<String, Object> data = new HashMap<>();
data.put("test-key", details);
AuditEvent event = new AuditEvent("test-user", "test-type", data);
customAuditEventRepository.add(event);
List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
assertThat(persistentAuditEvents).hasSize(1);
PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
示例2: testSuccessfulAuthenticationWithNoServiceAndWarn
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testSuccessfulAuthenticationWithNoServiceAndWarn()
throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
final MockRequestContext context = new MockRequestContext();
request.addParameter("username", "test");
request.addParameter("password", "test");
request.addParameter("warn", "true");
context.setExternalContext(new ServletExternalContext(
new MockServletContext(), request, response));
context.getRequestScope().put("credentials",
TestUtils.getCredentialsWithSameUsernameAndPassword());
// this.action.bind(context);
// assertEquals("success", this.action.submit(context).getId());
// assertNotNull(response.getCookie(this.warnCookieGenerator
// .getCookieName()));
}
示例3: verifyNoCasService
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void verifyNoCasService() 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.handleRequest(mockRequest, mockResponse);
assertEquals(400, mockResponse.getStatus());
assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}
示例4: test_for_issue
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
public void test_for_issue() throws Exception {
MockHttpServletRequest mockReq = new MockHttpServletRequest();
DefaultSavedRequest request = new DefaultSavedRequest(mockReq, new PortResolver() {
public int getServerPort(ServletRequest servletRequest) {
return 0;
}
});
String str = JSON.toJSONString(request, SerializerFeature.WriteClassName);
// System.out.println(str);
JSON.parseObject(str, Object.class, config);
JSON.parseObject(str);
}
示例5: renderView
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
private Map<?, ?> renderView() throws Exception{
final ModelAndView modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl();
final JstlView v = (JstlView) resolver.resolveViewName(modelAndView.getViewName(), Locale.getDefault());
final MockHttpServletRequest req = new MockHttpServletRequest(new MockServletContext());
v.setServletContext(req.getServletContext());
req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
new GenericWebApplicationContext(req.getServletContext()));
final Cas30ResponseView view = new Cas30ResponseView(v);
view.setServicesManager(this.servicesManager);
view.setCasAttributeEncoder(new DefaultCasAttributeEncoder(this.servicesManager));
final MockHttpServletResponse resp = new MockHttpServletResponse();
view.render(modelAndView.getModel(), req, resp);
return (Map<?, ?>) req.getAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_ATTRIBUTE_NAME_ATTRIBUTES);
}
示例6: verifySuccessfulAuthenticationWithServiceAndWarn
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void verifySuccessfulAuthenticationWithServiceAndWarn() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
final MockRequestContext context = new MockRequestContext();
request.addParameter(USERNAME_PARAM, TEST);
request.addParameter(PASSWORD_PARAM, TEST);
request.addParameter("warn", "true");
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, TEST);
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
final Credential c = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword();
putCredentialInRequestScope(context, c);
assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, this.action.execute(context).getId());
assertNotNull(response.getCookie(this.warnCookieGenerator.getCookieName()));
}
示例7: ensureHostnameAndIpShouldDoSpnego
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void ensureHostnameAndIpShouldDoSpnego() {
final HostNameSpnegoKnownClientSystemsFilterAction action =
new HostNameSpnegoKnownClientSystemsFilterAction("74\\..+", "", 0, "\\w+\\.\\w+\\.\\w+");
final MockRequestContext ctx = new MockRequestContext();
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setRemoteAddr(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());
}
示例8: ensureRemoteIpShouldBeChecked
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void ensureRemoteIpShouldBeChecked() {
final BaseSpnegoKnownClientSystemsFilterAction action =
new BaseSpnegoKnownClientSystemsFilterAction("^192\\.158\\..+");
final MockRequestContext ctx = new MockRequestContext();
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setRemoteAddr("192.158.5.781");
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:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:17,代码来源:AllSpnegoKnownClientSystemsFilterActionTest.java
示例9: testRenewWithServiceAndSameCredentials
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testRenewWithServiceAndSameCredentials() 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");
request.addParameter("username", "test");
request.addParameter("password", "test");
context.setExternalContext(new ServletExternalContext(
new MockServletContext(), request, new MockHttpServletResponse()));
context.getFlowScope().put("service", TestUtils.getService("test"));
// this.action.bind(context);
// assertEquals("warn", this.action.submit(context).getId());
}
示例10: testNoCasService
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testNoCasService() throws Exception {
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();
final ServicesManager servicesManager = mock(ServicesManager.class);
when(servicesManager.getAllServices()).thenReturn(new ArrayList<RegisteredService>());
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.setServicesManager(servicesManager);
oauth20WrapperController.afterPropertiesSet();
oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(400, mockResponse.getStatus());
assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}
示例11: verifyValidServiceTicketAndPgtUrlMismatch
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void verifyValidServiceTicketAndPgtUrlMismatch() 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(PGT_URL_PARAM, "http://www.github.com");
final ModelAndView modelAndView = this.serviceValidateController.handleRequestInternal(request, new MockHttpServletResponse());
assertFalse(modelAndView.getView().toString().contains(SUCCESS));
assertNull(modelAndView.getModel().get(PGT_IOU_PARAM));
}
示例12: testFailedAuthenticationWithNoService
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testFailedAuthenticationWithNoService() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockRequestContext context = new MockRequestContext();
request.addParameter("username", "test");
request.addParameter("password", "test2");
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());
}
示例13: testRenewWithServiceAndDifferentCredentials
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void testRenewWithServiceAndDifferentCredentials() 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");
request.addParameter("username", "test2");
request.addParameter("password", "test2");
context.setExternalContext(new ServletExternalContext(
new MockServletContext(), request, new MockHttpServletResponse()));
// this.action.bind(context);
// assertEquals("success", this.action.submit(context).getId());
}
示例14: verifyAddRegisteredServiceWithValues
import org.springframework.mock.web.MockHttpServletRequest; //导入依赖的package包/类
@Test
public void verifyAddRegisteredServiceWithValues() throws Exception {
final RegisteredServiceImpl svc = new RegisteredServiceImpl();
svc.setDescription("description");
svc.setServiceId("serviceId");
svc.setName("name");
svc.setEvaluationOrder(123);
assertTrue(this.manager.getAllServices().isEmpty());
final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
this.controller.saveService(new MockHttpServletRequest(),
new MockHttpServletResponse(),
data, mock(BindingResult.class));
final Collection<RegisteredService> services = this.manager.getAllServices();
assertEquals(1, services.size());
for(final RegisteredService rs : this.manager.getAllServices()) {
assertTrue(rs instanceof RegexRegisteredService);
}
}
示例15: 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);
}