@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!")); }
protected TestProcess testDelete(String api) throws Exception { MockHttpServletRequestBuilder msrb = initDefaultSetting(delete(api)); return test(msrb); }
@Test public void requestWhenIgnoringRequestMatcherThenUnionsWithConfiguredIgnoringAntMatchers() throws Exception { this.spring.register(IgnoringPathsAndMatchers.class, BasicController.class).autowire(); this.mvc.perform(put("/csrf")) .andExpect(status().isForbidden()); this.mvc.perform(post("/csrf")) .andExpect(status().isOk()); this.mvc.perform(put("/no-csrf")) .andExpect(status().isOk()); }
public static void deleteIdentityZone(String zoneId, MockMvc mockMvc) throws Exception { String identityToken = getClientCredentialsOAuthAccessToken(mockMvc, "identity", "identitysecret", "zones.write,scim.zones", null); mockMvc.perform(delete("/identity-zones/" + zoneId) .header("Authorization", "Bearer " + identityToken) .contentType(APPLICATION_JSON) .accept(APPLICATION_JSON)) .andExpect(status().isOk()); }
@Test public void testDeleteExternalGroupMapUsingNonExistentId() throws Exception { String externalGroup = "cn=developers,ou=scopes,dc=test,dc=com"; String groupId = "non-existent"; MockHttpServletRequestBuilder post = MockMvcRequestBuilders.delete("/Groups/External/groupId/" + groupId + "/externalGroup/" + externalGroup + "/origin/ldap") .header("Authorization", "Bearer " + scimWriteToken) .accept(APPLICATION_JSON); ResultActions result = getMockMvc().perform(post); result.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 requestWhenIgnoringRequestMatchersThenAugmentedByConfiguredRequestMatcher() throws Exception { this.spring.register(IgnoringRequestMatchers.class, BasicController.class).autowire(); this.mvc.perform(get("/path")) .andExpect(status().isForbidden()); this.mvc.perform(post("/path")) .andExpect(status().isOk()); }
@Test public void testContentAsString() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string("Hello world!")); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")); // Hamcrest matchers... this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!"))); this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"))); }
@Test public void successfulLoginWhenConfiguredWithCustomAuthenticationSuccessThenForwardsAccordingly() throws Exception { this.spring.configLocations(this.xml("WithAuthenticationSuccessForwardUrl")).autowire(); this.mvc.perform(post("/login") .param("username", "user") .param("password", "password")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/success_forward_url")); }
@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 requestWhenInterceptUrlMatchesMethodThenSecuresAccordingly() throws Exception { this.spring.configLocations(xml("InterceptUrlMethod")).autowire(); this.mvc.perform(get("/protected") .with(httpBasic("user", "password"))) .andExpect(status().isOk()); this.mvc.perform(post("/protected") .with(httpBasic("user", "password"))) .andExpect(status().isForbidden()); this.mvc.perform(post("/protected") .with(httpBasic("poster", "password"))) .andExpect(status().isOk()); this.mvc.perform(delete("/protected") .with(httpBasic("poster", "password"))) .andExpect(status().isForbidden()); this.mvc.perform(delete("/protected") .with(httpBasic("admin", "password"))) .andExpect(status().isOk()); }
@Test public void requestTokenWhenUsingPasswordGrantTypeThenOk() throws Exception { this.mvc.perform(post("/oauth/token") .param("grant_type", "password") .param("username", "subject") .param("password", "password") .header("Authorization", "Basic cmVhZGVyOnNlY3JldA==")) .andExpect(status().isOk()); }
@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)); }