@Override public Mono<ServerResponse> handle(ServerRequest request) { return ServerResponse.ok().build(); }
public Mono<ServerResponse> handle(ServerRequest request) { return ServerResponse.ok().build(); } }
Mono<ServerResponse> string(ServerRequest request) { return ServerResponse.ok() .contentType(MediaType.TEXT_EVENT_STREAM) .body(INTERVAL.map(aLong -> "foo " + aLong), String.class); }
@Test(expected = IllegalArgumentException.class) public void bodyObjectPublisher() { Mono<Void> mono = Mono.empty(); ServerResponse.ok().syncBody(mono); }
public Mono<ServerResponse> mono(ServerRequest request) { Person person = new Person("John"); return ServerResponse.ok().body(fromPublisher(Mono.just(person), Person.class)); }
@Bean public RouterFunction<?> handler() { return RouterFunctions.route() .GET("/sessionClassName", request -> request.session().flatMap(session -> { String className = session.getClass().getSimpleName(); return ServerResponse.ok().syncBody(className); })) .build(); }
@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(); }
Mono<ServerResponse> sse(ServerRequest request) { Flux<ServerSentEvent<String>> body = INTERVAL .map(aLong -> ServerSentEvent.builder("foo").id("" + aLong).comment("bar").build()); return ServerResponse.ok().body(fromServerSentEvents(body)); } }
public Mono<ServerResponse> flux(ServerRequest request) { Person person1 = new Person("John"); Person person2 = new Person("Jane"); return ServerResponse.ok().body( fromPublisher(Flux.just(person1, person2), Person.class)); } }
@Test public void cookies() { MultiValueMap<String, ResponseCookie> newCookies = new LinkedMultiValueMap<>(); newCookies.add("name", ResponseCookie.from("name", "value").build()); Mono<ServerResponse> result = ServerResponse.ok().cookies(cookies -> cookies.addAll(newCookies)).build(); StepVerifier.create(result) .expectNextMatches(response -> newCookies.equals(response.cookies())) .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 buildVoidPublisher() { Mono<Void> mono = Mono.empty(); Mono<ServerResponse> result = ServerResponse.ok().build(mono); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block(); MockServerHttpResponse response = exchange.getResponse(); StepVerifier.create(response.getBody()).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 normal() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build(); RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction); RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction); mapping.setMessageReaders(this.codecConfigurer.getReaders()); Mono<Object> result = mapping.getHandler(this.exchange); StepVerifier.create(result) .expectNext(handlerFunction) .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 ok() { Mono<ServerResponse> result = ServerResponse.ok().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode())) .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(); }
@Before public void start() throws Exception { HttpHandler httpHandler = RouterFunctions.toHttpHandler( route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!"))); this.server = new ReactorHttpServer(); this.server.setHandler(httpHandler); this.server.afterPropertiesSet(); this.server.start(); this.client = WebTestClient.bindToServer() .baseUrl("http://localhost:" + this.server.getPort()) .build(); }