org.springframework.mock.web.MockServletContext Java Examples
The following examples show how to use
org.springframework.mock.web.MockServletContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenIdSingleSignOnActionTests.java From springboot-shiro-cas-mybatis with MIT License | 7 votes |
@Test public void verifyBadUsername() throws Exception { final MockRequestContext context = new MockRequestContext(); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter(OpenIdConstants.OPENID_IDENTITY, "fablah"); request.setParameter(OpenIdConstants.OPENID_RETURNTO, "http://www.cnn.com"); final OpenIdService service = OpenIdService.createServiceFrom(request, null); context.getFlowScope().put("service", service); context.getFlowScope().put("ticketGrantingTicketId", "tgtId"); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); assertEquals("error", this.action.execute(context).getId()); }
Example #2
Source File: LdapSpnegoKnownClientSystemsFilterActionTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Test public void ensureLdapAttributeShouldDoSpnego() { final LdapSpnegoKnownClientSystemsFilterAction action = new LdapSpnegoKnownClientSystemsFilterAction(this.connectionFactory, this.searchRequest, "mail"); final MockRequestContext ctx = new MockRequestContext(); final MockHttpServletRequest req = new MockHttpServletRequest(); req.setRemoteAddr("localhost"); 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()); }
Example #3
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@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())); }
Example #4
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-servlet.xml", "file:src/main/webapp/WEB-INF/deployerConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #5
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-management-servlet.xml", "file:src/main/webapp/WEB-INF/managementConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #6
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@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()); }
Example #7
Source File: SendTicketGrantingTicketActionTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Test public void verifyTgtToSetRemovingOldTgt() throws Exception { final MockHttpServletResponse response = new MockHttpServletResponse(); final MockHttpServletRequest request = new MockHttpServletRequest(); final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class); when(tgt.getId()).thenReturn("test"); request.setCookies(new Cookie("TGT", "test5")); WebUtils.putTicketGrantingTicketInScopes(this.context, tgt); this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response)); assertEquals("success", this.action.execute(this.context).getId()); request.setCookies(response.getCookies()); assertEquals(tgt.getId(), this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request)); }
Example #8
Source File: GenerateServiceTicketActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Test public void testServiceTicketFromCookie() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); context.getFlowScope().put("ticketGrantingTicketId", this.ticketGrantingTicket); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); request.setCookies(new Cookie[] {new Cookie("TGT", this.ticketGrantingTicket)}); this.action.execute(context); assertNotNull(WebUtils.getServiceTicketFromRequestScope(context)); }
Example #9
Source File: GuiceBindingSpringContextLoaderListenerTest.java From attic-rave with Apache License 2.0 | 6 votes |
@Test public void initialize_test() { Injector injector = Guice.createInjector(createNiceMock(Module.class)); MockServletContext mockServletContext = new MockServletContext(); mockServletContext.addInitParameter("contextConfigLocation", "classpath:rave-shindig-test-applicationContext.xml, classpath:rave-shindig-test-dataContext.xml"); mockServletContext.setAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE, injector); GuiceBindingSpringContextLoaderListener listener = new GuiceBindingSpringContextLoaderListener(); ServletContextEvent event = createNiceMock(ServletContextEvent.class); expect(event.getServletContext()).andReturn(mockServletContext).anyTimes(); replay(event); listener.contextInitialized(event); assertThat((Injector)mockServletContext.getAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE), is(not(sameInstance(injector)))); }
Example #10
Source File: SendTicketGrantingTicketActionTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Test public void verifySsoSessionCookieOnRenewAsParameter() throws Exception { final MockHttpServletResponse response = new MockHttpServletResponse(); final MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter(CasProtocolConstants.PARAMETER_RENEW, "true"); final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class); when(tgt.getId()).thenReturn("test"); request.setCookies(new Cookie("TGT", "test5")); WebUtils.putTicketGrantingTicketInScopes(this.context, tgt); this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response)); this.action.setCreateSsoSessionCookieOnRenewAuthentications(false); assertEquals("success", this.action.execute(this.context).getId()); assertEquals(0, response.getCookies().length); }
Example #11
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@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()); }
Example #12
Source File: GenerateServiceTicketActionTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Test public void verifyTicketGrantingTicketNotTgtButGateway() throws Exception { final MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); final MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); request.addParameter("gateway", "true"); final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class); when(tgt.getId()).thenReturn("bleh"); WebUtils.putTicketGrantingTicketInScopes(context, tgt); assertEquals("gateway", this.action.execute(context).getId()); }
Example #13
Source File: ResourceControllerTest.java From geomajas-project-server with GNU Affero General Public License v3.0 | 6 votes |
@Test public void testIncludeServletPath() throws ServletException, IOException { // create an empty mock context MockServletContext context = new TestServletContext(true); MockHttpServletRequest request = new MockHttpServletRequest(context); request.setServletPath("/org"); request.setPathInfo("/geomajas/servlet/test.js"); request.setMethod("GET"); MockHttpServletResponse response = new MockHttpServletResponse(); ResourceController resourceController = new ResourceController(); resourceController.setIncludeServletPath(true); resourceController.setServletContext(context); resourceController.getResource(request, response); Resource resource = new ClassPathResource("/org/geomajas/servlet/test.js"); Assert.assertArrayEquals(IOUtils.toByteArray(resource.getInputStream()), response.getContentAsByteArray()); }
Example #14
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@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()); }
Example #15
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@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()); }
Example #16
Source File: CustomRequestAttributesRequestContextHolderTests.java From spring-analysis-note with MIT License | 6 votes |
@Before public void setUp() { ServletContext servletContext = new MockServletContext(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext); mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK); RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse())); this.wac.setServletContext(servletContext); new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class); this.wac.refresh(); this.mockMvc = webAppContextSetup(this.wac) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }
Example #17
Source File: AuthenticationViaFormActionTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Test public void verifySuccessfulAuthenticationWithNoService() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest(); final MockRequestContext context = new MockRequestContext(); WebUtils.putLoginTicket(context, "LOGIN"); request.addParameter("lt", "LOGIN"); request.addParameter("username", "test"); request.addParameter("password", "test"); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); final Credential c = TestUtils.getCredentialsWithSameUsernameAndPassword(); putCredentialInRequestScope(context, c); final MessageContext messageContext = mock(MessageContext.class); assertEquals("success", this.action.submit(context, c, messageContext).getId()); }
Example #18
Source File: OpenTracingServerModuleTest.java From crnk-framework with Apache License 2.0 | 6 votes |
@Before public void setup() throws ServletException { simpleTransactionNames = false; tracer = Mockito.mock(Tracer.class); MockServletContext servletContext = new MockServletContext(); initModule(); 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", "*/*"); }
Example #19
Source File: SpringBootAnnotationConfigProviderTest.java From joinfaces with Apache License 2.0 | 5 votes |
@Test void getConfiguration_noBasePackage() { this.webApplicationContextRunner.run(context -> { SpringBootAnnotationConfigProvider annotationConfigProvider = context.getBean( SpringBootAnnotationConfigProvider.class); assertThat(annotationConfigProvider).isNotNull(); MockServletContext servletContext = new MockServletContext(); Configuration configuration = annotationConfigProvider.getConfiguration(servletContext); assertThat(configuration.getRules().isEmpty()).isFalse(); }); }
Example #20
Source File: PrincipalFromRequestUserPrincipalNonInteractiveCredentialsActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
@Test public void testRemoteUserExists() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest(); request.setUserPrincipal(new Principal() { @Override public String getName() { return "test"; } }); final MockRequestContext context = new MockRequestContext(); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); assertEquals("success", this.action.execute(context).getId()); }
Example #21
Source File: WebConfigurerTest.java From jhipster-microservices-example with Apache License 2.0 | 5 votes |
@Before public void setup() { servletContext = spy(new MockServletContext()); doReturn(new MockFilterRegistration()) .when(servletContext).addFilter(anyString(), any(Filter.class)); doReturn(new MockServletRegistration()) .when(servletContext).addServlet(anyString(), any(Servlet.class)); env = new MockEnvironment(); props = new JHipsterProperties(); webConfigurer = new WebConfigurer(env, props, new MockHazelcastInstance()); metricRegistry = new MetricRegistry(); webConfigurer.setMetricRegistry(metricRegistry); }
Example #22
Source File: StandaloneMockMvcBuilder.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected WebApplicationContext initWebAppContext() { MockServletContext servletContext = new MockServletContext(); StubWebApplicationContext wac = new StubWebApplicationContext(servletContext); registerMvcSingletons(wac); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); return wac; }
Example #23
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
Example #24
Source File: TrimouViewResolverTest.java From trimou with Apache License 2.0 | 5 votes |
@Before public void init() { resolver = new TrimouViewResolver(); resolver.setApplicationContext(new StaticWebApplicationContext()); resolver.setServletContext(new MockServletContext()); resolver.setPrefix("classpath:/trimou-templates/"); }
Example #25
Source File: GenerateServiceTicketActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
@Test public void testTicketGrantingTicketFromRequest() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); WebUtils.putTicketGrantingTicketInRequestScope(context, this.ticketGrantingTicket); this.action.execute(context); assertNotNull(WebUtils.getServiceTicketFromRequestScope(context)); }
Example #26
Source File: JseInformationsTagTest.java From sinavi-jfw with Apache License 2.0 | 5 votes |
@Before public void setup() throws Exception { MockServletContext sc = new MockServletContext(); request = new MockHttpServletRequest(sc); response = new MockHttpServletResponse(); page = new MockPageContext(sc, request, response); request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); }
Example #27
Source File: MockMultipartHttpServletRequestBuilderTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void test() { MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/"); parent.characterEncoding("UTF-8"); Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent); assertNotNull(result); assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass()); MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result; MockHttpServletRequest request = builder.buildRequest(new MockServletContext()); assertEquals("UTF-8", request.getCharacterEncoding()); }
Example #28
Source File: AuthenticationViaFormActionTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
@Test public void testSuccessfulAuthenticationWithNoService() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest(); final MockRequestContext context = new MockRequestContext(); request.addParameter("username", "test"); request.addParameter("password", "test"); context.setExternalContext(new ServletExternalContext( new MockServletContext(), request, new MockHttpServletResponse())); context.getRequestScope().put("credentials", TestUtils.getCredentialsWithSameUsernameAndPassword()); // this.action.bind(context); // assertEquals("success", this.action.submit(context).getId()); }
Example #29
Source File: HttpFilterBenchmarks.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Benchmark @Measurement(iterations = 5, time = 1) @Fork(3) public void filterWithoutSleuth(BenchmarkContext context) throws IOException, ServletException { MockHttpServletRequest request = builder().buildRequest(new MockServletContext()); MockHttpServletResponse response = new MockHttpServletResponse(); response.setContentType(MediaType.APPLICATION_JSON_VALUE); context.dummyFilter.doFilter(request, response, new MockFilterChain()); }
Example #30
Source File: TestUtils.java From springboot-shiro-cas-mybatis with MIT License | 5 votes |
public static MockRequestContext getContext( final MockHttpServletRequest request, final MockHttpServletResponse response) { final MockRequestContext context = new MockRequestContext(); context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response)); return context; }