@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 modelAttributesConventions() { Set<String> model = Collections.singleton("bar"); Mono<RenderingResponse> result = RenderingResponse.create("foo") .modelAttributes(model).build(); StepVerifier.create(result) .expectNextMatches(response -> "bar".equals(response.model().get("string"))) .expectComplete() .verify(); }
private void verifyWrittenData(Flux<DataBuffer> writeResult) throws IOException { StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobarbazqux", result); }
@Test public void cacheControlTag() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).cacheControl(CacheControl.noCache()).build(); StepVerifier.create(result) .expectNextMatches(response -> "no-cache".equals(response.headers().getCacheControl())) .expectComplete() .verify(); }
@Test public void contentLength() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).contentLength(42).build(); StepVerifier.create(result) .expectNextMatches(response -> Long.valueOf(42).equals(response.headers().getContentLength())) .expectComplete() .verify(); }
@Test public void decodeMultipleXmlRootElement() throws Exception { Mono<DataBuffer> source = stringBuffer(POJO_CHILD); Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap()); StepVerifier.create(output) .expectNext(new Pojo("foo", "bar")) .expectNext(new Pojo("foofoo", "barbar")) .expectComplete() .verify(); }
@Test public void decodeEmptyDataBuffer() { Flux<DataBuffer> input = Flux.just(stringBuffer("")); Flux<String> output = this.decoder.decode(input, TYPE, null, Collections.emptyMap()); StepVerifier.create(output) .expectNext("") .expectComplete().verify(); }
@Test public void readAsynchronousFileChannelPosition() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel( () -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ), 9, this.bufferFactory, 3); StepVerifier.create(flux) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@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 etag() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).eTag("foo").build(); StepVerifier.create(result) .expectNextMatches(response -> "\"foo\"".equals(response.headers().getETag())) .expectComplete() .verify(); }
@Test public void contentType() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).contentType(MediaType.APPLICATION_JSON).build(); StepVerifier.create(result) .expectNextMatches(response -> MediaType.APPLICATION_JSON.equals(response.headers().getContentType())) .expectComplete() .verify(); }
@Test public void allow() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).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 decodeSingleXmlRootElement() throws Exception { Mono<DataBuffer> source = stringBuffer(POJO_ROOT); Mono<Object> output = this.decoder.decodeToMono(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap()); StepVerifier.create(output) .expectNext(new Pojo("foofoo", "barbar")) .expectComplete() .verify(); }
@Test public void transformSkippedForGzippedResource() throws Exception { EncodedResourceResolverTests.createGzippedFile("main.css"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/main.css")); Resource resource = getResource("main.css"); EncodedResource gzipped = new EncodedResource(resource, "gzip", ".gz"); StepVerifier.create(this.transformerChain.transform(exchange, gzipped)) .expectNext(gzipped) .expectComplete() .verify(); }
@Test public void modelAttributeConventions() { Mono<RenderingResponse> result = RenderingResponse.create("foo") .modelAttribute("bar").build(); StepVerifier.create(result) .expectNextMatches(response -> "bar".equals(response.model().get("string"))) .expectComplete() .verify(); }
@Test public void emptyBodyWithRxJava2Observable() throws Exception { ResolvableType type = httpEntityType(io.reactivex.Observable.class, String.class); HttpEntity<io.reactivex.Observable<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody().toFlowable(BackpressureStrategy.BUFFER)) .expectNextCount(0) .expectComplete() .verify(); }
private void assertResponseBody(String responseBody) { StepVerifier.create(this.exchange.getResponse().getBody()) .consumeNextWith(buf -> assertEquals(responseBody, dumpString(buf, StandardCharsets.UTF_8))) .expectComplete() .verify(); }
@Test public void takeUntilByteCount() { Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount( Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
private void verifyReadData(Flux<DataBuffer> buffers) { StepVerifier.create(buffers) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); }
@Test public void readResourcePosition() throws Exception { Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, 9, this.bufferFactory, 3); StepVerifier.create(flux) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(5)); }