Search in sources :

Example 1 with MvcResult

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");
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) Map(java.util.Map) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with MvcResult

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());
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with MvcResult

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!");
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with MvcResult

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));
        }
    };
}
Also used : MediaType(org.springframework.http.MediaType) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 5 with MvcResult

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());
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)628 Test (org.junit.Test)365 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)178 Test (org.junit.jupiter.api.Test)163 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)69 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)39 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)38 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)36 MockHttpSession (org.springframework.mock.web.MockHttpSession)35 ResultActions (org.springframework.test.web.servlet.ResultActions)27 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)26 ExpectedDatabase (com.github.springtestdbunit.annotation.ExpectedDatabase)26 Map (java.util.Map)24 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)24 WithMockUser (org.springframework.security.test.context.support.WithMockUser)23 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)22 AbstractSaturnConsoleTest (com.vip.saturn.job.console.AbstractSaturnConsoleTest)20 Cookie (javax.servlet.http.Cookie)18 MockMvc (org.springframework.test.web.servlet.MockMvc)17 HttpSession (jakarta.servlet.http.HttpSession)16