Search in sources :

Example 61 with MockServerWebExchange

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

the class HeadersRequestConditionTests method headerValueMatchNegated.

@Test
public void headerValueMatchNegated() {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("foo", "baz"));
    HeadersRequestCondition condition = new HeadersRequestCondition("foo!=bar");
    assertThat(condition.getMatchingCondition(exchange)).isNotNull();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 62 with MockServerWebExchange

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

the class PatternsRequestConditionTests method matchPatternContainsExtension.

@Test
public void matchPatternContainsExtension() {
    PatternsRequestCondition condition = createPatternsCondition("/foo.jpg");
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo.html"));
    PatternsRequestCondition match = condition.getMatchingCondition(exchange);
    assertThat(match).isNull();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 63 with MockServerWebExchange

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

the class PatternsRequestConditionTests method matchTrailingSlash.

@Test
public void matchTrailingSlash() {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/"));
    PatternsRequestCondition condition = createPatternsCondition("/foo");
    PatternsRequestCondition match = condition.getMatchingCondition(exchange);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next().getPatternString()).as("Should match by default").isEqualTo("/foo");
    condition = createPatternsCondition("/foo");
    match = condition.getMatchingCondition(exchange);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next().getPatternString()).as("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)").isEqualTo("/foo");
    PathPatternParser parser = new PathPatternParser();
    parser.setMatchOptionalTrailingSeparator(false);
    condition = new PatternsRequestCondition(parser.parse("/foo"));
    match = condition.getMatchingCondition(MockServerWebExchange.from(get("/foo/")));
    assertThat(match).isNull();
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 64 with MockServerWebExchange

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

the class PatternsRequestConditionTests method matchSortPatterns.

@Test
public void matchSortPatterns() {
    PatternsRequestCondition condition = createPatternsCondition("/*/*", "/foo/bar", "/foo/*");
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/bar"));
    PatternsRequestCondition match = condition.getMatchingCondition(exchange);
    PatternsRequestCondition expected = createPatternsCondition("/foo/bar", "/foo/*", "/*/*");
    assertThat(match).isEqualTo(expected);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 65 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleWithProducibleContentTypeShouldFailWithServerError.

// gh-23287
@Test
public void handleWithProducibleContentTypeShouldFailWithServerError() {
    ResponseEntity<String> value = ResponseEntity.ok().body("<foo/>");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    Set<MediaType> mediaTypes = Collections.singleton(MediaType.APPLICATION_XML);
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    ResponseEntityResultHandler resultHandler = new ResponseEntityResultHandler(Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())), new RequestedContentTypeResolverBuilder().build());
    StepVerifier.create(resultHandler.handleResult(exchange, result)).consumeErrorWith(ex -> assertThat(ex).isInstanceOf(HttpMessageNotWritableException.class).hasMessageContaining("with preset Content-Type")).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) Duration(java.time.Duration) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE(org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) Test(org.junit.jupiter.api.Test) List(java.util.List) Single(io.reactivex.rxjava3.core.Single) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CompletableFuture(java.util.concurrent.CompletableFuture) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ArrayList(java.util.ArrayList) MockServerHttpRequest.get(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResolvableMethod.on(org.springframework.web.testfixture.method.ResolvableMethod.on) LinkedHashSet(java.util.LinkedHashSet) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectUtils(org.springframework.util.ObjectUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) Completable(io.reactivex.rxjava3.core.Completable) Mono(reactor.core.publisher.Mono) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) ResponseEntity.notFound(org.springframework.http.ResponseEntity.notFound) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MediaType(org.springframework.http.MediaType) MethodParameter(org.springframework.core.MethodParameter) 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