@Test // SPR-13079 public void deferredResultWithDelayedError() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().is5xxServerError()) .andExpect(content().string("Delayed Error")); }
@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 testNewAccountPage() throws Exception { mockMvc.perform(get("/create_account").param("client_id", "client-id").param("redirect_uri", "http://example.com/redirect")) .andExpect(status().isOk()) .andExpect(model().attribute("client_id", "client-id")) .andExpect(model().attribute("redirect_uri", "http://example.com/redirect")) .andExpect(view().name("accounts/new_activation_email")) .andExpect(xpath("//*[@type='hidden' and @value='client-id']").exists()) .andExpect(xpath("//*[@type='hidden' and @value='http://example.com/redirect']").exists()); }
@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 multipartRequestWithOptionalFileArrayNotPresent() throws Exception { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilearray").file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attributeDoesNotExist("fileContent")) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); }
@Test public void changePassword_withInvalidPassword_returnsErrorJson() throws Exception { String toolongpassword = new RandomValueStringGenerator(260).generate(); String code = getExpiringCode(null, null); getMockMvc().perform(post("/password_change") .header("Authorization", "Bearer " + loginToken) .contentType(APPLICATION_JSON) .content("{\"code\":\"" + code + "\",\"new_password\":\""+toolongpassword+"\"}")) .andExpect(status().isUnprocessableEntity()) .andExpect(jsonPath("$.error").value("invalid_password")) .andExpect(jsonPath("$.message").value("Password must be no more than 255 characters in length.")); }
@Test void testLoginWithRemoteUaaJsonPrompts() throws Exception { mockMvc.perform(get("/login") .accept(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(view().name("login")) .andExpect(model().attribute("prompts", hasKey("username"))) .andExpect(model().attribute("prompts", hasKey("password"))); }
@Test void introspectToken_withInvalidToken() throws Exception { mockMvc.perform( post("/introspect") .header("Authorization", "Basic " + basic) .header(ACCEPT, APPLICATION_JSON_VALUE) .header(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE) .param("token", "invalid-token")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.active").value("false")) .andExpect(header().string(CONTENT_TYPE, "application/json;charset=UTF-8")); }
@Test public void defaultViewResolver() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Corea")) .andExpect(model().attribute("person", hasProperty("name", equalTo("Corea")))) .andExpect(status().isOk()) .andExpect(forwardedUrl("person/show")); // InternalResourceViewResolver }
@Test public void requestWhenUsingHpkpDefaultsThenIncludesHpkpHeaderUsingSha256() throws Exception { this.spring.configLocations(this.xml("DefaultsDisabledWithHpkpDefaults")).autowire(); this.mvc.perform(get("/").secure(true)) .andExpect(status().isOk()) .andExpect(header().string( "Public-Key-Pins-Report-Only", "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\"")) .andExpect(excludesDefaults()); }
private void resetPassword(String defaultPassword) throws Exception { String code = getExpiringCode(null, null); MockHttpServletRequestBuilder post = post("/password_change") .header("Authorization", "Bearer " + loginToken) .contentType(APPLICATION_JSON) .content("{\"code\":\"" + code + "\",\"new_password\":\"" + defaultPassword + "\"}") .accept(APPLICATION_JSON); getMockMvc().perform(post) .andExpect(status().isOk()) .andExpect(jsonPath("$.user_id").exists()) .andExpect(jsonPath("$.username").value(user.getUserName())); }
.build(); mockMvc.perform(get("/person/Corea")) .andExpect(status().isOk()) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(forwardedUrl("person/show")); mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.person.name").value("Corea")); mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_XML)) .andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
@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)); }