@Test public void noParamGreetingShouldReturnDefaultMessage() throws Exception { this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) .andExpect(jsonPath("$.content").value("Hello, World!")); }
@Test public void paramGreetingShouldReturnTailoredMessage() throws Exception { this.mockMvc.perform(get("/greeting").param("name", "Spring Community")) .andDo(print()).andExpect(status().isOk()) .andExpect(jsonPath("$.content").value("Hello, Spring Community!")); }
public ValidatableMockMvcResponse apply(ResultHandler resultHandler, ResultHandler... resultHandlers) { AssertParameter.notNull(resultHandler, ResultMatcher.class); try { resultActions.andDo(resultHandler); } catch (Exception e) { SafeExceptionRethrower.safeRethrow(e); } for (ResultHandler handler : resultHandlers) { try { resultActions.andDo(handler); } catch (Exception e) { SafeExceptionRethrower.safeRethrow(e); } } return this; }
@Test public void testPrint() throws Exception { StringWriter writer = new StringWriter(); standaloneSetup(new SimpleController()) .build() .perform(get("/").content("Hello Request".getBytes())) .andDo(log()) .andDo(print()) .andDo(print(System.err)) .andDo(print(writer)) ; System.out.println(); System.out.println("==============================================================="); System.out.println(writer.toString()); }
private ResultActions actions(MockHttpServletRequestBuilder requestBuilder) throws Exception { ResultActions actions = MockMvcClientHttpRequestFactory.this.mockMvc .perform(requestBuilder); List<Snippet> snippets = new ArrayList<>(); for (String field : this.fields) { snippets.add(new ResponseFieldSnippet(field)); } actions.andDo(document(this.label, preprocessResponse(prettyPrint()), snippets.toArray(new Snippet[0]))); this.fields = new ArrayList<>(); return actions; }
@Test // SPR-12735 public void printAsyncResult() throws Exception { StringWriter writer = new StringWriter(); MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) .andDo(print(writer)) .andExpect(request().asyncStarted()) .andReturn(); assertTrue(writer.toString().contains("Async started = true")); writer = new StringWriter(); this.asyncController.onMessage("Joe"); this.mockMvc.perform(asyncDispatch(mvcResult)) .andDo(print(writer)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); assertTrue(writer.toString().contains("Async started = false")); }
@Test public void person() throws Exception { this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void person() throws Exception { this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@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 streamingSlow() throws Exception { this.mockMvc.perform(get("/1").param("streamingSlow", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) .andExpect(status().isOk()) .andExpect(content().string("name=Joe&someBoolean=true")); }
@Test public void streamingJson() throws Exception { this.mockMvc.perform(get("/1").param("streamingJson", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}")); }
@Test void invalid_request() throws Exception { mockMvc.perform(get("/invalid_request")) .andDo( document("{ClassName}/{methodName}", preprocessResponse(prettyPrint())) ) .andExpect(status().isOk()); }
@Test public void testResetPasswordPage() throws Exception { ExpiringCode code = codeStore.generateCode("{\"user_id\" : \"some-user-id\"}", new Timestamp(System.currentTimeMillis() + 1000000), null, IdentityZoneHolder.get().getId()); mockMvc.perform(get("/reset_password").param("email", "user@example.com").param("code", code.getCode())) .andExpect(status().isOk()) .andDo(print()) .andExpect(view().name("reset_password")) .andExpect(model().attribute("email", "email")) .andExpect(model().attribute("username", "username")) .andExpect(content().string(containsString("<div class=\"email-display\">Username: username</div>"))) .andExpect(content().string(containsString("<input type=\"hidden\" name=\"username\" value=\"username\"/>"))); }
private void performAuthentication(ScimUser user, boolean success) throws Exception { mockMvc.perform( post("/login.do") .accept("text/html") .with(cookieCsrf()) .param("username", user.getUserName()) .param("password", USER_PASSWORD)) .andDo(print()) .andExpect(success ? authenticated() : unauthenticated()); } }
private ResultActions performLoginWithSession() throws Exception { return getMockMvc().perform(post("/login.do") .session(session) .param("username", user.getUserName()) .param("password", password) .with(cookieCsrf())) .andDo(print()) .andExpect(status().isFound()); }
@Test public void patch_has_one_path() throws Exception { getMockMvc().perform( patch("/Group/groupId/members") .header("Authorization", "Bearer " + scimWriteToken) .header("Content-Type", APPLICATION_JSON_VALUE) ) .andDo(print()) .andExpect(header().string("Location", "http://localhost/login?error=invalid_login_request")) .andExpect(status().isFound()); //gets caught by the ui filter for unknown URIs but wantsJson; }
public String performIdpAuthentication() throws Exception { RequestPostProcessor marissa = securityContext(getUaaSecurityContext("marissa", getWebApplicationContext(), idpZone.getIdentityZone())); return getMockMvc().perform( get("/saml/idp/initiate") .header("Host", idpZone.getIdentityZone().getSubdomain()+".localhost") .param("sp", entityId) .with(marissa) ) .andDo(print()) .andReturn().getResponse().getContentAsString(); }
@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 void check_token_delete() throws Exception { mockMvc.perform( delete("/introspect") .header("Authorization", "Basic " + basic) .header(ACCEPT, APPLICATION_JSON_VALUE) .header(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE) .param("token", token)) .andDo(log()) .andExpect(status().isMethodNotAllowed()) .andExpect(jsonPath("$.error").value("method_not_allowed")) .andExpect(jsonPath("$.error_description").value(HtmlUtils.htmlEscape("Request method 'DELETE' not supported", "ISO-8859-1"))); } }
@Test void introspectToken_withValidToken() throws Exception { mockMvc.perform( post("/introspect") .header("Authorization", "Basic " + basic) .header(ACCEPT, APPLICATION_JSON_VALUE) .header(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE) .param("token", token)) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.active").value("true")) .andExpect(jsonPath("$.client_id").value(CLIENTID)) .andExpect(header().string(CONTENT_TYPE, "application/json;charset=UTF-8")); }