public Mono<ServerResponse> handle(ServerRequest request) { return ServerResponse.ok().build(); } }
@Override public Mono<ServerResponse> handle(ServerRequest request) { HttpMethod method = request.method(); if (method != null) { switch (method) { case GET: return EntityResponse.fromObject(this.resource).build() .map(response -> response); case HEAD: Resource headResource = new HeadMethodResource(this.resource); return EntityResponse.fromObject(headResource).build() .map(response -> response); case OPTIONS: return ServerResponse.ok() .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); } } return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); }
@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 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 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 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 ok() { Mono<ServerResponse> result = ServerResponse.ok().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode())) .expectComplete() .verify(); }
@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(); }
@Bean RouterFunction<ServerResponse> routes(final UserService userService) { return route(GET("/api/v1/users/{uuid}"), request -> ok().contentType(APPLICATION_JSON_UTF8) .body(Mono.just(request.pathVariable("uuid")) .map(UUID::fromString) .map(userService::getUser) .subscribeOn(Schedulers.elastic()), User.class)) .andRoute(GET("/api/v1/users"), request -> ok().contentType(APPLICATION_STREAM_JSON) .body(Flux.fromIterable(userService.getAllUsers()) .subscribeOn(Schedulers.elastic()), User.class)) .andRoute(POST("/api/v1/users"), request -> request.bodyToMono(User.class) .map(userService::saveUser) .subscribeOn(Schedulers.elastic()) .map(uuid -> format("%s%s/%s", getBaseUrl(request), "/api/v1/users", uuid)) .map(URI::create) .flatMap(uri -> created(uri).build())) .andRoute(GET("/*"), request -> ok().body(Mono.just(getBaseUrl(request)) .map(baseUrl -> asList(singletonMap("GET", pathTo(baseUrl, "/")), singletonMap("GET", pathTo(baseUrl, "/api/v1/users")), singletonMap("GET", pathTo(baseUrl, "/api/v1/users/{uuid}")), singletonMap("POST", pathTo(baseUrl, "/api/v1/users")))), List.class)) ; }
/** * Create a builder with an * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status. * @return the created builder */ static BodyBuilder unprocessableEntity() { return status(HttpStatus.UNPROCESSABLE_ENTITY); }
public Mono<ServerResponse> create(ServerRequest req) { return req.bodyToMono(Post.class) .flatMap(post -> this.posts.save(post)) .flatMap(p -> ServerResponse.created(URI.create("/posts/" + p.getId())).build()); }
@Test public void status() { Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void notFound() { Mono<ServerResponse> result = ServerResponse.notFound().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.NOT_FOUND.equals(response.statusCode())) .expectComplete() .verify(); }
@Test public void build() { ResponseCookie cookie = ResponseCookie.from("name", "value").build(); Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED) .header("MyKey", "MyValue") .cookie(cookie).build(); 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(); assertEquals(HttpStatus.CREATED, response.getStatusCode()); assertEquals("MyValue", response.getHeaders().getFirst("MyKey")); assertEquals("value", response.getCookies().getFirst("name").getValue()); StepVerifier.create(response.getBody()).expectComplete().verify(); }
public Mono<ServerResponse> delete(ServerRequest req) { return ServerResponse.noContent().build(this.posts.deleteById(req.pathVariable("id"))); }
@Test public void noContent() { Mono<ServerResponse> result = ServerResponse.noContent().build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.NO_CONTENT.equals(response.statusCode())) .expectComplete() .verify(); }