@Test // SPR-12597 public void completableFutureWithImmediateValue() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void testChangingAPasswordWithABadRequest() throws Exception { MockHttpServletRequestBuilder post = post("/password_change") .contentType(APPLICATION_JSON) .content("{\"new_password\":\"new_secret\"}") .accept(APPLICATION_JSON); mockMvc.perform(post) .andExpect(status().isBadRequest()); }
/** * http/http-basic equivalent */ @Test public void basicAuthenticationWhenUsingDefaultsThenMatchesNamespace() throws Exception { this.spring.register(HttpBasicConfig.class, UserConfig.class).autowire(); this.mvc.perform(get("/")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/") .with(httpBasic("user", "invalid"))) .andExpect(status().isUnauthorized()) .andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Realm\"")); this.mvc.perform(get("/") .with(httpBasic("user", "password"))) .andExpect(status().isNotFound()); }
@Test public void requestWhenHttpPatternUsesRegexMatchingThenMatchesAccordingly() throws Exception { this.spring.configLocations(xml("RegexSecurityPattern")).autowire(); this.mvc.perform(get("/protected")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/unprotected")) .andExpect(status().isNotFound()); }
@Test public void streaming() throws Exception { this.mockMvc.perform(get("/1").param("streaming", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) // fetch async result similar to "asyncDispatch" builder .andExpect(status().isOk()) .andExpect(content().string("name=Joe")); }
@Test public void saveSpecial() throws Exception { this.mockMvc.perform(post("/people").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void testFeedWithLinefeedChars() throws Exception { // Map<String, String> namespace = Collections.singletonMap("ns", ""); standaloneSetup(new BlogFeedController()).build() .perform(get("/blog.atom").accept(MediaType.APPLICATION_ATOM_XML)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_ATOM_XML)) .andExpect(xpath("//feed/title").string("Test Feed")) .andExpect(xpath("//feed/icon").string("http://www.example.com/favicon.ico")); }
@Test public void getPerson() throws Exception { this.mockMvc.perform(get("/persons/Joe").flashAttr("message", "success!")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/index")) .andExpect(model().size(2)) .andExpect(model().attribute("person", new Person("Joe"))) .andExpect(model().attribute("message", "success!")) .andExpect(flash().attributeCount(0)); }
@Test public void testSwitchingZoneWithSubdomain() throws Exception { IdentityZone identityZone = createZone(identityToken); String zoneAdminToken = MockMvcUtils.getZoneAdminToken(getMockMvc(),adminToken, identityZone.getId()); ClientDetails client = createClientInOtherZone(zoneAdminToken, status().isCreated(), SUBDOMAIN_HEADER, identityZone.getSubdomain()); getMockMvc().perform( post("/oauth/token") .param("grant_type","client_credentials") .header("Authorization", "Basic " + new String(Base64.encodeBase64((client.getClientId() + ":" + client.getClientSecret()).getBytes()))) .with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))) .andExpect(status().isOk()); }
@Test public void testCreatingAPasswordResetWhenTheUserDoesNotExist() throws Exception { when(scimUserProvisioning.query("userName eq \"user@example.com\" and origin eq \"" + OriginKeys.UAA + "\"", IdentityZoneHolder.get().getId())) .thenReturn(Arrays.<ScimUser>asList()); MockHttpServletRequestBuilder post = post("/password_resets") .contentType(APPLICATION_JSON) .content("user@example.com") .accept(APPLICATION_JSON); mockMvc.perform(post) .andExpect(status().isNotFound()); }
@Test public void lookupUsingOnlyOrigin() throws Exception { String filter = "origin eq \"uaa\""; MockHttpServletRequestBuilder post = post("/ids/Users") .header("Authorization", "Bearer " + scimLookupIdUserToken) .accept(APPLICATION_JSON) .param("filter", filter) .param("startIndex", String.valueOf(1)) .param("count", String.valueOf(50)); getMockMvc().perform(post) .andExpect(status().isBadRequest()); }
@Test public void requestWhenBearerTokenResolverAllowsQueryParameterAndRequestContainsTwoTokensThenInvalidRequest() throws Exception { this.spring.register(AllowBearerTokenAsQueryParameterConfig.class, JwtDecoderConfig.class, BasicController.class).autowire(); JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class); when(decoder.decode(anyString())).thenReturn(JWT); this.mvc.perform(get("/authenticated") .with(bearerToken(JWT_TOKEN)) .param("access_token", JWT_TOKEN)) .andExpect(status().isBadRequest()) .andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, containsString("invalid_request"))); }