use of org.springframework.test.web.servlet.MvcResult in project spring-boot by spring-projects.
the class SampleSecureOAuth2ApplicationTests method useAppSecretsPlusUserAccountToGetBearerToken.
@Test
public void useAppSecretsPlusUserAccountToGetBearerToken() throws Exception {
String header = "Basic " + new String(Base64.encode("foo:bar".getBytes()));
MvcResult result = this.mvc.perform(post("/oauth/token").header("Authorization", header).param("grant_type", "password").param("scope", "read").param("username", "greg").param("password", "turnquist")).andExpect(status().isOk()).andDo(print()).andReturn();
Object accessToken = this.objectMapper.readValue(result.getResponse().getContentAsString(), Map.class).get("access_token");
MvcResult flightsAction = this.mvc.perform(get("/flights/1").accept(MediaTypes.HAL_JSON).header("Authorization", "Bearer " + accessToken)).andExpect(header().string("Content-Type", MediaTypes.HAL_JSON.toString() + ";charset=UTF-8")).andExpect(status().isOk()).andDo(print()).andReturn();
Flight flight = this.objectMapper.readValue(flightsAction.getResponse().getContentAsString(), Flight.class);
assertThat(flight.getOrigin()).isEqualTo("Nashville");
assertThat(flight.getDestination()).isEqualTo("Dallas");
assertThat(flight.getAirline()).isEqualTo("Spring Ways");
assertThat(flight.getFlightNumber()).isEqualTo("OAUTH2");
assertThat(flight.getTraveler()).isEqualTo("Greg Turnquist");
}
use of org.springframework.test.web.servlet.MvcResult in project spring-boot by spring-projects.
the class SampleHypermediaJpaApplicationIntegrationTests method docs.
@Test
public void docs() throws Exception {
MvcResult response = this.mockMvc.perform(get("/admin/docs/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()).andReturn();
System.err.println(response.getResponse().getContentAsString());
}
use of org.springframework.test.web.servlet.MvcResult in project spring-boot by spring-projects.
the class BasicErrorControllerMockMvcTests method testErrorWithResponseStatus.
@Test
public void testErrorWithResponseStatus() throws Exception {
MvcResult result = this.mockMvc.perform(get("/bang")).andExpect(status().isNotFound()).andReturn();
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error")).andReturn();
String content = response.getResponse().getContentAsString();
assertThat(content).contains("Expected!");
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class ContentResultMatchers method contentTypeCompatibleWith.
/**
* Assert the ServletResponse content type is compatible with the given
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
*/
public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String actual = result.getResponse().getContentType();
assertTrue("Content type not set", actual != null);
MediaType actualContentType = MediaType.parseMediaType(actual);
assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType));
}
};
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class CookieResultMatchers method secure.
/**
* Assert whether the cookie must be sent over a secure protocol or not.
*/
public ResultMatcher secure(final String name, final boolean secure) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name);
assertEquals("Response cookie secure", secure, cookie.getSecure());
}
};
}
Aggregations