public DefaultServerResponseBuilder(ServerResponse other) { Assert.notNull(other, "ServerResponse must not be null"); this.statusCode = (other instanceof AbstractServerResponse ? ((AbstractServerResponse) other).statusCode : other.statusCode().value()); this.headers.addAll(other.headers()); }
@Test public void lastModified() { ZonedDateTime now = ZonedDateTime.now(); Mono<ServerResponse> result = ServerResponse.ok().lastModified(now).build(); Long expected = now.toInstant().toEpochMilli() / 1000; StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getLastModified() / 1000)) .expectComplete() .verify(); }
@Test public void varyBy() { Mono<ServerResponse> result = ServerResponse.ok().varyBy("foo").build(); List<String> expected = Collections.singletonList("foo"); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getVary())) .expectComplete() .verify(); }
@Test public void allow() { Mono<ServerResponse> result = ServerResponse.ok().allow(HttpMethod.GET).build(); Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getAllow())) .expectComplete() .verify(); }
@Test public void contentLength() { Mono<ServerResponse> result = ServerResponse.ok().contentLength(42).build(); StepVerifier.create(result) .expectNextMatches(response -> Long.valueOf(42).equals(response.headers().getContentLength())) .expectComplete() .verify(); }
@Test public void temporaryRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.temporaryRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.TEMPORARY_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void permanentRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.permanentRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.PERMANENT_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void created() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.created(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void seeOther() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.seeOther(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.SEE_OTHER.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void headers() { HttpHeaders newHeaders = new HttpHeaders(); newHeaders.set("foo", "bar"); Mono<ServerResponse> result = ServerResponse.ok().headers(headers -> headers.addAll(newHeaders)).build(); StepVerifier.create(result) .expectNextMatches(response -> newHeaders.equals(response.headers())) .expectComplete() .verify(); }
@Test public void eTag() { Mono<ServerResponse> result = ServerResponse.ok().eTag("foo").build(); StepVerifier.create(result) .expectNextMatches(response -> "\"foo\"".equals(response.headers().getETag())) .expectComplete() .verify(); }
@Test public void cacheControlTag() { Mono<ServerResponse> result = ServerResponse.ok().cacheControl(CacheControl.noCache()).build(); StepVerifier.create(result) .expectNextMatches(response -> "no-cache".equals(response.headers().getCacheControl())) .expectComplete() .verify(); }
@Test public void from() { ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block(); Mono<ServerResponse> result = ServerResponse.from(other).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && "bar".equals(response.headers().getFirst("foo"))) .expectComplete() .verify(); }
@Test public void contentType() { Mono<ServerResponse> result = ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build(); StepVerifier.create(result) .expectNextMatches(response -> MediaType.APPLICATION_JSON.equals(response.headers().getContentType())) .expectComplete() .verify(); }
@Test public void options() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("http://localhost")); MockServerHttpResponse mockResponse = exchange.getResponse(); ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults().messageReaders()); Mono<ServerResponse> responseMono = this.handlerFunction.handle(request); Mono<Void> result = responseMono.flatMap(response -> { assertEquals(HttpStatus.OK, response.statusCode()); assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), response.headers().getAllow()); return response.writeTo(exchange, context); }); StepVerifier.create(result) .expectComplete() .verify(); assertEquals(HttpStatus.OK, mockResponse.getStatusCode()); assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), mockResponse.getHeaders().getAllow()); StepVerifier.create(mockResponse.getBody()).expectComplete().verify(); }
/** * Create a builder with the status code and headers of the given response. * * @param other the response to copy the status and headers from * @return the created builder */ static BodyBuilder from(ServerResponse other) { Assert.notNull(other, "'other' must not be null"); DefaultServerResponseBuilder builder = new DefaultServerResponseBuilder(other.statusCode()); return builder.headers(other.headers()); }