private void testContextPathServletPathInvalid(String contextPath, String servletPath, String message) { try { this.builder.contextPath(contextPath); this.builder.servletPath(servletPath); this.builder.buildRequest(this.servletContext); } catch (IllegalArgumentException ex) { assertEquals(message, ex.getMessage()); } }
@Test public void testRequestAttributeEqualTo() throws Exception { this.mockMvc.perform(get("/main/1").servletPath("/main")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/{id}")) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/1")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, equalTo("/{id}"))) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, equalTo("/1"))); }
@Test public void contextPathServletPathInfo() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/"); this.builder.servletPath("/index.html"); this.builder.pathInfo(null); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals("", request.getContextPath()); assertEquals("/index.html", request.getServletPath()); assertNull(request.getPathInfo()); }
@Test public void contextPathServletPath() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42"); this.builder.contextPath("/travel"); this.builder.servletPath("/main"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals("/travel", request.getContextPath()); assertEquals("/main", request.getServletPath()); assertEquals("/hotels/42", request.getPathInfo()); }
@Test public void contextPathServletPathInfoEmpty() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42"); this.builder.contextPath("/travel"); this.builder.servletPath("/hotels/42"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals("/travel", request.getContextPath()); assertEquals("/hotels/42", request.getServletPath()); assertNull(request.getPathInfo()); }
@Test public void requestWhenUsingMvcMatchersAndServletPathThenAuthorizesRequestsAccordingly() throws Exception { this.spring.configLocations(this.xml("MvcMatchersServletPath")).autowire(); MockServletContext servletContext = mockServletContext("/spring"); ConfigurableWebApplicationContext context = (ConfigurableWebApplicationContext) this.spring.getContext(); context.setServletContext(servletContext); this.mvc.perform(get("/spring/path").servletPath("/spring")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/spring/path.html").servletPath("/spring")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/spring/path/").servletPath("/spring")) .andExpect(status().isUnauthorized()); }
@Test void multiple_oidc_providers_use_response_type_in_url( @Autowired JdbcIdentityProviderProvisioning jdbcIdentityProviderProvisioning ) throws Exception { String subdomain = "oidc-idp-discovery-multi-" + generator.generate().toLowerCase(); IdentityZone zone = MultitenancyFixture.identityZone(subdomain, subdomain); createOtherIdentityZone(zone.getSubdomain(), mockMvc, webApplicationContext, false); createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, null); createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, "code id_token"); mockMvc.perform(get("/login") .header("Accept", TEXT_HTML) .servletPath("/login") .with(new SetServerNameRequestPostProcessor(zone.getSubdomain() + ".localhost"))) .andExpect(status().isOk()) .andExpect(content().string(containsString("http://myauthurl.com?client_id=id&response_type=code&"))) .andExpect(content().string(containsString("http://myauthurl.com?client_id=id&response_type=code+id_token&"))); }
@Test public void testUserInfoEndpointIsCorrect() throws Exception { for (String host : Arrays.asList("localhost", "subdomain.localhost")) { for (String url : Arrays.asList("/.well-known/openid-configuration", "/oauth/token/.well-known/openid-configuration")) { MockHttpServletResponse response = mockMvc.perform( get(url) .header("Host", host) .servletPath(url) .with(new SetServerNameRequestPostProcessor(host)) .accept(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse(); OpenIdConfiguration openIdConfiguration = JsonUtils.readValue(response.getContentAsString(), OpenIdConfiguration.class); mockMvc.perform(get(openIdConfiguration.getUserInfoUrl())) .andExpect(status().isUnauthorized()); } } } }
.accept(TEXT_HTML) .session(session) .servletPath("/login") .with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))
@Test void idpDiscoveryRedirectsToOIDCProvider( @Autowired JdbcIdentityProviderProvisioning jdbcIdentityProviderProvisioning ) throws Exception { String subdomain = "oidc-discovery-" + generator.generate().toLowerCase(); IdentityZone zone = MultitenancyFixture.identityZone(subdomain, subdomain); createOtherIdentityZone(zone.getSubdomain(), mockMvc, webApplicationContext, false); String originKey = createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, "id_token code"); MvcResult mvcResult = mockMvc.perform(post("/login/idp_discovery") .with(cookieCsrf()) .header("Accept", TEXT_HTML) .servletPath("/login/idp_discovery") .param("email", "marissa@test.org") .with(new SetServerNameRequestPostProcessor(zone.getSubdomain() + ".localhost"))) .andExpect(status().isFound()) .andReturn(); String location = mvcResult.getResponse().getHeader("Location"); Map<String, String> queryParams = UriComponentsBuilder.fromUriString(location).build().getQueryParams().toSingleValueMap(); assertThat(location, startsWith("http://myauthurl.com")); assertThat(queryParams, hasEntry("client_id", "id")); assertThat(queryParams, hasEntry("response_type", "id_token+code")); assertThat(queryParams, hasEntry("redirect_uri", "http%3A%2F%2F" + subdomain + ".localhost%2Flogin%2Fcallback%2F" + originKey)); assertThat(queryParams, hasKey("nonce")); }
.servletPath("/login") .with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))) .andExpect(status().isFound())
.servletPath("/login") .with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))) .andExpect(status().isFound())
get(url) .header("Host", host) .servletPath(url) .with(new SetServerNameRequestPostProcessor(host)) .accept(APPLICATION_JSON))
.servletPath("/login") .with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))) .andExpect(status().isFound())
.servletPath("/.well-known/openid-configuration") .accept(APPLICATION_JSON)) .andExpect(status().isOk())
@Test public void shouldTriggerJobAndReturnItsURL() throws Exception { when(jobService.startAsyncJob("someJobType")).thenReturn(Optional.of("theJobId")); mockMvc.perform(MockMvcRequestBuilders .post("/some-microservice/internal/jobs/someJobType") .servletPath("/internal/jobs/someJobType")) .andExpect(status().is(204)) .andExpect(header().string("Location", "http://localhost/some-microservice/internal/jobs/theJobId")); verify(jobService).startAsyncJob("someJobType"); }
@Test public void shouldReturnJobIfJobExists() throws Exception { // given ZoneId cet = ZoneId.of("CET"); OffsetDateTime now = OffsetDateTime.now(cet).truncatedTo(ChronoUnit.MILLIS); JobInfo expectedJob = newJobInfo("42", "TEST", fixed(now.toInstant(), cet), "localhost"); when(jobService.findJob("42")).thenReturn(Optional.of(expectedJob)); String nowAsString = ISO_OFFSET_DATE_TIME.format(now); mockMvc.perform(MockMvcRequestBuilders .get("/some-microservice/internal/jobs/42") .servletPath("/internal/jobs/42")) .andExpect(status().is(200)) .andExpect(jsonPath("$.status").value("OK")) .andExpect(jsonPath("$.messages").isArray()) .andExpect(jsonPath("$.jobType").value("TEST")) .andExpect(jsonPath("$.hostname").value("localhost")) .andExpect(jsonPath("$.started").value(nowAsString)) .andExpect(jsonPath("$.stopped").value("")) .andExpect(jsonPath("$.lastUpdated").value(nowAsString)) .andExpect(jsonPath("$.jobUri").value("http://localhost/some-microservice/internal/jobs/42")) .andExpect(jsonPath("$.links").isArray()) .andExpect(jsonPath("$.links[0].href").value("http://localhost/some-microservice/internal/jobs/42")) .andExpect(jsonPath("$.links[1].href").value("http://localhost/some-microservice/internal/jobdefinitions/TEST")) .andExpect(jsonPath("$.links[2].href").value("http://localhost/some-microservice/internal/jobs")) .andExpect(jsonPath("$.links[3].href").value("http://localhost/some-microservice/internal/jobs?type=TEST")) .andExpect(jsonPath("$.runtime").value("00:00:00")) .andExpect(jsonPath("$.state").value("Running")); verify(jobService).findJob("42"); }