Search in sources :

Example 51 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method compareToMediaTypeAll.

// SPR-8536
@Test
public void compareToMediaTypeAll() {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/"));
    ProducesRequestCondition condition1 = new ProducesRequestCondition();
    ProducesRequestCondition condition2 = new ProducesRequestCondition("application/json");
    assertThat(condition1.compareTo(condition2, exchange) < 0).as("Should have picked '*/*' condition as an exact match").isTrue();
    assertThat(condition2.compareTo(condition1, exchange) > 0).as("Should have picked '*/*' condition as an exact match").isTrue();
    condition1 = new ProducesRequestCondition("*/*");
    condition2 = new ProducesRequestCondition("application/json");
    assertThat(condition1.compareTo(condition2, exchange) < 0).isTrue();
    assertThat(condition2.compareTo(condition1, exchange) > 0).isTrue();
    exchange = MockServerWebExchange.from(get("/").header("Accept", "*/*"));
    condition1 = new ProducesRequestCondition();
    condition2 = new ProducesRequestCondition("application/json");
    assertThat(condition1.compareTo(condition2, exchange) < 0).isTrue();
    assertThat(condition2.compareTo(condition1, exchange) > 0).isTrue();
    condition1 = new ProducesRequestCondition("*/*");
    condition2 = new ProducesRequestCondition("application/json");
    assertThat(condition1.compareTo(condition2, exchange) < 0).isTrue();
    assertThat(condition2.compareTo(condition1, exchange) > 0).isTrue();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 52 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method compareTo.

@Test
public void compareTo() {
    ProducesRequestCondition html = new ProducesRequestCondition("text/html");
    ProducesRequestCondition xml = new ProducesRequestCondition("application/xml");
    ProducesRequestCondition none = new ProducesRequestCondition();
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "application/xml, text/html"));
    assertThat(html.compareTo(xml, exchange) > 0).isTrue();
    assertThat(xml.compareTo(html, exchange) < 0).isTrue();
    assertThat(xml.compareTo(none, exchange) < 0).isTrue();
    assertThat(none.compareTo(xml, exchange) > 0).isTrue();
    assertThat(html.compareTo(none, exchange) < 0).isTrue();
    assertThat(none.compareTo(html, exchange) > 0).isTrue();
    exchange = MockServerWebExchange.from(get("/").header("Accept", "application/xml, text/*"));
    assertThat(html.compareTo(xml, exchange) > 0).isTrue();
    assertThat(xml.compareTo(html, exchange) < 0).isTrue();
    exchange = MockServerWebExchange.from(get("/").header("Accept", "application/pdf"));
    assertThat(html.compareTo(xml, exchange)).isEqualTo(0);
    assertThat(xml.compareTo(html, exchange)).isEqualTo(0);
    // See SPR-7000
    exchange = MockServerWebExchange.from(get("/").header("Accept", "text/html;q=0.9,application/xml"));
    assertThat(html.compareTo(xml, exchange) > 0).isTrue();
    assertThat(xml.compareTo(html, exchange) < 0).isTrue();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 53 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method match.

@Test
public void match() {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain"));
    ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
    assertThat(condition.getMatchingCondition(exchange)).isNotNull();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 54 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class HandlerResultHandlerTests method producibleMediaTypesRequestAttribute.

@Test
void producibleMediaTypesRequestAttribute() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
    exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(IMAGE_GIF));
    List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
    MediaType actual = resultHandler.selectMediaType(exchange, () -> mediaTypes);
    assertThat(actual).isEqualTo(IMAGE_GIF);
}
Also used : MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 55 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class HandlerResultHandlerTests method removeQualityParameter.

@Test
void removeQualityParameter() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path").header("Accept", "text/plain; q=0.5"));
    List<MediaType> mediaTypes = Arrays.asList(APPLICATION_JSON, TEXT_PLAIN);
    MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes);
    assertThat(actual).isEqualTo(TEXT_PLAIN);
}
Also used : MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)224 Test (org.junit.jupiter.api.Test)216 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)61 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HttpHeaders (org.springframework.http.HttpHeaders)25 Resource (org.springframework.core.io.Resource)24 HandlerResult (org.springframework.web.reactive.HandlerResult)23 MethodParameter (org.springframework.core.MethodParameter)22 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 MediaType (org.springframework.http.MediaType)19 Mono (reactor.core.publisher.Mono)18 BeforeEach (org.junit.jupiter.api.BeforeEach)17 HttpMethod (org.springframework.http.HttpMethod)15 StepVerifier (reactor.test.StepVerifier)13 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 List (java.util.List)12 HttpStatus (org.springframework.http.HttpStatus)12 IOException (java.io.IOException)10