@Test public void asHttpHeaders() { HttpHeaders httpHeaders = new HttpHeaders(); when(mockHeaders.asHttpHeaders()).thenReturn(httpHeaders); assertSame(httpHeaders, wrapper.asHttpHeaders()); }
public DefaultServerRequestBuilder(ServerRequest other) { Assert.notNull(other, "ServerRequest must not be null"); this.messageReaders = other.messageReaders(); this.exchange = other.exchange(); this.methodName = other.methodName(); this.uri = other.uri(); headers(headers -> headers.addAll(other.headers().asHttpHeaders())); cookies(cookies -> cookies.addAll(other.cookies())); attributes(attributes -> attributes.putAll(other.attributes())); }
@Test public void header() { HttpHeaders httpHeaders = new HttpHeaders(); List<MediaType> accept = Collections.singletonList(MediaType.APPLICATION_JSON); httpHeaders.setAccept(accept); List<Charset> acceptCharset = Collections.singletonList(StandardCharsets.UTF_8); httpHeaders.setAcceptCharset(acceptCharset); long contentLength = 42L; httpHeaders.setContentLength(contentLength); MediaType contentType = MediaType.TEXT_PLAIN; httpHeaders.setContentType(contentType); InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 80); httpHeaders.setHost(host); List<HttpRange> range = Collections.singletonList(HttpRange.createByteRange(0, 42)); httpHeaders.setRange(range); DefaultServerRequest request = new DefaultServerRequest( MockServerWebExchange.from(MockServerHttpRequest .method(HttpMethod.GET, "http://example.com?foo=bar") .headers(httpHeaders)), this.messageReaders); ServerRequest.Headers headers = request.headers(); assertEquals(accept, headers.accept()); assertEquals(acceptCharset, headers.acceptCharset()); assertEquals(OptionalLong.of(contentLength), headers.contentLength()); assertEquals(Optional.of(contentType), headers.contentType()); assertEquals(httpHeaders, headers.asHttpHeaders()); }
@Override public HttpHeaders asHttpHeaders() { return this.headers.asHttpHeaders(); } }
@Bean public RouterFunction<ServerResponse> monoRouterFunction(NameHandler nameHandler, GreetingHandler greetingHandler) { return route(GET("/"), serverRequest -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).body( greetingHandler.getGreetingStringMono(serverRequest.headers().asHttpHeaders(). getAcceptLanguageAsLocales().get(0).toLanguageTag()).concatWith(Mono.just(" ")). concatWith(nameHandler.getNameStringMono()), String.class)); }
@Test public void from() { MockServerHttpRequest request = MockServerHttpRequest.post("http://example.com") .header("foo", "bar") .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders()); Flux<DataBuffer> body = Flux.just("baz") .map(s -> s.getBytes(StandardCharsets.UTF_8)) .map(dataBufferFactory::wrap); ServerRequest result = ServerRequest.from(other) .method(HttpMethod.HEAD) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())) .body(body) .build(); assertEquals(HttpMethod.HEAD, result.method()); assertEquals(1, result.headers().asHttpHeaders().size()); assertEquals("baar", result.headers().asHttpHeaders().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz").getValue()); StepVerifier.create(result.bodyToFlux(String.class)) .expectNext("baz") .verifyComplete(); }
@Override public HttpHeaders asHttpHeaders() { return this.headers.asHttpHeaders(); } }