Upgrade to Spring Java Format 0.0.38
Closes gh-890
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -88,7 +88,8 @@ class WebTestClientRequestConverter implements RequestConverter<ExchangeResult>
|
||||
parameters.addAll(this.queryStringParser.parse(result.getUrl()));
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(result.getRequestHeaders().getContentType())) {
|
||||
parameters.addAll(this.formDataReader
|
||||
.readMono(FORM_DATA_TYPE, new ExchangeResultReactiveHttpInputMessage(result), null).block());
|
||||
.readMono(FORM_DATA_TYPE, new ExchangeResultReactiveHttpInputMessage(result), null)
|
||||
.block());
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
@@ -99,10 +100,14 @@ class WebTestClientRequestConverter implements RequestConverter<ExchangeResult>
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return new MultipartHttpMessageReader(partHttpMessageReader)
|
||||
.readMono(ResolvableType.forClass(Part.class), new ExchangeResultReactiveHttpInputMessage(result),
|
||||
Collections.emptyMap())
|
||||
.onErrorReturn(new LinkedMultiValueMap<>()).block().values().stream()
|
||||
.flatMap((parts) -> parts.stream().map(this::createOperationRequestPart)).collect(Collectors.toList());
|
||||
.readMono(ResolvableType.forClass(Part.class), new ExchangeResultReactiveHttpInputMessage(result),
|
||||
Collections.emptyMap())
|
||||
.onErrorReturn(new LinkedMultiValueMap<>())
|
||||
.block()
|
||||
.values()
|
||||
.stream()
|
||||
.flatMap((parts) -> parts.stream().map(this::createOperationRequestPart))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -110,7 +115,8 @@ class WebTestClientRequestConverter implements RequestConverter<ExchangeResult>
|
||||
if (ClassUtils.isPresent(DEFAULT_PART_HTTP_MESSAGE_READER, getClass().getClassLoader())) {
|
||||
try {
|
||||
return (HttpMessageReader<Part>) Class
|
||||
.forName(DEFAULT_PART_HTTP_MESSAGE_READER, true, getClass().getClassLoader()).newInstance();
|
||||
.forName(DEFAULT_PART_HTTP_MESSAGE_READER, true, getClass().getClassLoader())
|
||||
.newInstance();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Continue
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -54,8 +54,11 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
|
||||
if (result.getResponseCookies().isEmpty() || headers.containsKey(HttpHeaders.SET_COOKIE)) {
|
||||
return headers;
|
||||
}
|
||||
result.getResponseCookies().values().stream().flatMap(Collection::stream)
|
||||
.forEach((cookie) -> headers.add(HttpHeaders.SET_COOKIE, generateSetCookieHeader(cookie)));
|
||||
result.getResponseCookies()
|
||||
.values()
|
||||
.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.forEach((cookie) -> headers.add(HttpHeaders.SET_COOKIE, generateSetCookieHeader(cookie)));
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -74,7 +74,7 @@ public abstract class WebTestClientRestDocumentation {
|
||||
*/
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER, snippets)
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +91,8 @@ public abstract class WebTestClientRestDocumentation {
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier,
|
||||
OperationRequestPreprocessor requestPreprocessor, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
requestPreprocessor, snippets).handle(result, result, retrieveConfiguration(result));
|
||||
requestPreprocessor, snippets)
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +109,8 @@ public abstract class WebTestClientRestDocumentation {
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier,
|
||||
OperationResponsePreprocessor responsePreprocessor, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
responsePreprocessor, snippets).handle(result, result, retrieveConfiguration(result));
|
||||
responsePreprocessor, snippets)
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,13 +130,13 @@ public abstract class WebTestClientRestDocumentation {
|
||||
OperationRequestPreprocessor requestPreprocessor, OperationResponsePreprocessor responsePreprocessor,
|
||||
Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
requestPreprocessor, responsePreprocessor, snippets).handle(result, result,
|
||||
retrieveConfiguration(result));
|
||||
requestPreprocessor, responsePreprocessor, snippets)
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
}
|
||||
|
||||
private static Map<String, Object> retrieveConfiguration(ExchangeResult result) {
|
||||
Map<String, Object> configuration = WebTestClientRestDocumentationConfigurer
|
||||
.retrieveConfiguration(result.getRequestHeaders());
|
||||
.retrieveConfiguration(result.getRequestHeaders());
|
||||
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, result.getUriTemplate());
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -53,8 +53,14 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void httpRequest() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
@@ -63,8 +69,14 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void httpRequestWithCustomPort() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("http://localhost:8080").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost:8080")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
@@ -73,8 +85,16 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void requestWithHeaders() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/"), (req) -> null))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").header("a", "alpha", "apple")
|
||||
.header("b", "bravo").exchange().expectBody().returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.header("a", "alpha", "apple")
|
||||
.header("b", "bravo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
@@ -85,8 +105,14 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void httpsRequest() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("https://localhost").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("https://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
@@ -95,8 +121,14 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void httpsRequestWithCustomPort() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("https://localhost:8443").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("https://localhost:8443")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost:8443/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
@@ -105,8 +137,14 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void getRequestWithQueryStringPopulatesParameters() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo?a=alpha&b=bravo").exchange()
|
||||
.expectBody().returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo?a=alpha&b=bravo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&b=bravo"));
|
||||
assertThat(request.getParameters()).hasSize(2);
|
||||
@@ -123,8 +161,16 @@ public class WebTestClientRequestConverterTests {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
|
||||
req.body(BodyExtractors.toFormData()).block();
|
||||
return null;
|
||||
})).configureClient().baseUrl("http://localhost").build().post().uri("/foo")
|
||||
.body(BodyInserters.fromFormData(parameters)).exchange().expectBody().returnResult();
|
||||
}))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri("/foo")
|
||||
.body(BodyInserters.fromFormData(parameters))
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -138,9 +184,15 @@ public class WebTestClientRequestConverterTests {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
|
||||
req.body(BodyExtractors.toFormData()).block();
|
||||
return null;
|
||||
})).configureClient().baseUrl("http://localhost").build().post()
|
||||
.uri(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo")).exchange().expectBody()
|
||||
.returnResult();
|
||||
}))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"))
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -156,9 +208,16 @@ public class WebTestClientRequestConverterTests {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
|
||||
req.body(BodyExtractors.toFormData()).block();
|
||||
return null;
|
||||
})).configureClient().baseUrl("http://localhost").build().post()
|
||||
.uri(URI.create("http://localhost/foo?a=alpha&b=br%26vo")).body(BodyInserters.fromFormData(parameters))
|
||||
.exchange().expectBody().returnResult();
|
||||
}))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri(URI.create("http://localhost/foo?a=alpha&b=br%26vo"))
|
||||
.body(BodyInserters.fromFormData(parameters))
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&b=br%26vo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -170,9 +229,15 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void postRequestWithNoContentType() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> ServerResponse.ok().build()))
|
||||
.configureClient().baseUrl("http://localhost").build().post().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> ServerResponse.ok().build()))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -183,14 +248,18 @@ public class WebTestClientRequestConverterTests {
|
||||
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
|
||||
multipartData.add("file", new byte[] { 1, 2, 3, 4 });
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(
|
||||
RouterFunctions
|
||||
.route(POST("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.body(req.body(BodyExtractors.toMultipartData())
|
||||
.map((parts) -> parts.size()), Integer.class)))
|
||||
.configureClient().baseUrl("http://localhost").build().post().uri("/foo")
|
||||
.body(BodyInserters.fromMultipartData(multipartData)).exchange().expectBody().returnResult();
|
||||
.bindToRouterFunction(RouterFunctions.route(POST("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.body(req.body(BodyExtractors.toMultipartData()).map((parts) -> parts.size()), Integer.class)))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri("/foo")
|
||||
.body(BodyInserters.fromMultipartData(multipartData))
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -216,14 +285,18 @@ public class WebTestClientRequestConverterTests {
|
||||
|
||||
});
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(
|
||||
RouterFunctions
|
||||
.route(POST("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.body(req.body(BodyExtractors.toMultipartData())
|
||||
.map((parts) -> parts.size()), Integer.class)))
|
||||
.configureClient().baseUrl("http://localhost").build().post().uri("/foo")
|
||||
.body(BodyInserters.fromMultipartData(multipartData)).exchange().expectBody().returnResult();
|
||||
.bindToRouterFunction(RouterFunctions.route(POST("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.body(req.body(BodyExtractors.toMultipartData()).map((parts) -> parts.size()), Integer.class)))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.post()
|
||||
.uri("/foo")
|
||||
.body(BodyInserters.fromMultipartData(multipartData))
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
@@ -243,9 +316,16 @@ public class WebTestClientRequestConverterTests {
|
||||
@Test
|
||||
public void requestWithCookies() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo")
|
||||
.cookie("cookieName1", "cookieVal1").cookie("cookieName2", "cookieVal2").exchange().expectBody()
|
||||
.returnResult();
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.cookie("cookieName1", "cookieVal1")
|
||||
.cookie("cookieName2", "cookieVal2")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
assertThat(result.getRequestHeaders().get(HttpHeaders.COOKIE)).isNotNull();
|
||||
OperationRequest request = this.converter.convert(result);
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,27 +49,39 @@ public class WebTestClientResponseConverterTests {
|
||||
@Test
|
||||
public void basicResponse() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(
|
||||
RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.ok().syncBody("Hello, World!")))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.bindToRouterFunction(
|
||||
RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.ok().syncBody("Hello, World!")))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationResponse response = this.converter.convert(result);
|
||||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getContentAsString()).isEqualTo("Hello, World!");
|
||||
assertThat(response.getHeaders().getContentType())
|
||||
.isEqualTo(MediaType.parseMediaType("text/plain;charset=UTF-8"));
|
||||
.isEqualTo(MediaType.parseMediaType("text/plain;charset=UTF-8"));
|
||||
assertThat(response.getHeaders().getContentLength()).isEqualTo(13);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithCookie() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.cookie(ResponseCookie.from("name", "value").domain("localhost").httpOnly(true).build())
|
||||
.build()))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
.cookie(ResponseCookie.from("name", "value").domain("localhost").httpOnly(true).build())
|
||||
.build()))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationResponse response = this.converter.convert(result);
|
||||
assertThat(response.getHeaders()).hasSize(1);
|
||||
assertThat(response.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
|
||||
@@ -80,9 +92,15 @@ public class WebTestClientResponseConverterTests {
|
||||
public void responseWithNonStandardStatusCode() {
|
||||
assumeThat(ExchangeResult.class, hasMethod("getRawStatusCode"));
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.status(210).build()))
|
||||
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
|
||||
.returnResult();
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.status(210).build()))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
.build()
|
||||
.get()
|
||||
.uri("/foo")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.returnResult();
|
||||
OperationResponse response = this.converter.convert(result);
|
||||
assertThat(response.getStatusCode()).isEqualTo(210);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,17 +49,19 @@ public class WebTestClientRestDocumentationConfigurerTests {
|
||||
@Test
|
||||
public void configurationCanBeRetrievedButOnlyOnce() {
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
.build();
|
||||
this.configurer.filter(request, mock(ExchangeFunction.class));
|
||||
assertThat(WebTestClientRestDocumentationConfigurer.retrieveConfiguration(request.headers())).isNotNull();
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> WebTestClientRestDocumentationConfigurer.retrieveConfiguration(request.headers()));
|
||||
.isThrownBy(() -> WebTestClientRestDocumentationConfigurer.retrieveConfiguration(request.headers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestUriHasDefaultsAppliedWhenItHasNoHost() {
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test?foo=bar#baz"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
.build();
|
||||
ExchangeFunction exchangeFunction = mock(ExchangeFunction.class);
|
||||
this.configurer.filter(request, exchangeFunction);
|
||||
ArgumentCaptor<ClientRequest> requestCaptor = ArgumentCaptor.forClass(ClientRequest.class);
|
||||
@@ -70,14 +72,15 @@ public class WebTestClientRestDocumentationConfigurerTests {
|
||||
@Test
|
||||
public void requestUriIsNotChangedWhenItHasAHost() {
|
||||
ClientRequest request = ClientRequest
|
||||
.create(HttpMethod.GET, URI.create("https://api.example.com:4567/test?foo=bar#baz"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
|
||||
.create(HttpMethod.GET, URI.create("https://api.example.com:4567/test?foo=bar#baz"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
.build();
|
||||
ExchangeFunction exchangeFunction = mock(ExchangeFunction.class);
|
||||
this.configurer.filter(request, exchangeFunction);
|
||||
ArgumentCaptor<ClientRequest> requestCaptor = ArgumentCaptor.forClass(ClientRequest.class);
|
||||
verify(exchangeFunction).exchange(requestCaptor.capture());
|
||||
assertThat(requestCaptor.getValue().url())
|
||||
.isEqualTo(URI.create("https://api.example.com:4567/test?foo=bar#baz"));
|
||||
.isEqualTo(URI.create("https://api.example.com:4567/test?foo=bar#baz"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -85,51 +85,71 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
RouterFunction<ServerResponse> route = RouterFunctions
|
||||
.route(RequestPredicates.GET("/"),
|
||||
(request) -> ServerResponse.status(HttpStatus.OK).body(fromObject(new Person("Jane", "Doe"))))
|
||||
.andRoute(RequestPredicates.GET("/{foo}/{bar}"),
|
||||
(request) -> ServerResponse.status(HttpStatus.OK).body(fromObject(new Person("Jane", "Doe"))))
|
||||
.andRoute(RequestPredicates.POST("/upload"),
|
||||
(request) -> request.body(BodyExtractors.toMultipartData())
|
||||
.map((parts) -> ServerResponse.status(HttpStatus.OK).build().block()))
|
||||
.andRoute(RequestPredicates.GET("/set-cookie"),
|
||||
(request) -> ServerResponse.ok()
|
||||
.cookie(ResponseCookie.from("name", "value").domain("localhost").httpOnly(true).build())
|
||||
.build());
|
||||
this.webTestClient = WebTestClient.bindToRouterFunction(route).configureClient()
|
||||
.baseUrl("https://api.example.com").filter(documentationConfiguration(this.restDocumentation)).build();
|
||||
.route(RequestPredicates.GET("/"),
|
||||
(request) -> ServerResponse.status(HttpStatus.OK).body(fromObject(new Person("Jane", "Doe"))))
|
||||
.andRoute(RequestPredicates.GET("/{foo}/{bar}"),
|
||||
(request) -> ServerResponse.status(HttpStatus.OK).body(fromObject(new Person("Jane", "Doe"))))
|
||||
.andRoute(RequestPredicates.POST("/upload"),
|
||||
(request) -> request.body(BodyExtractors.toMultipartData())
|
||||
.map((parts) -> ServerResponse.status(HttpStatus.OK).build().block()))
|
||||
.andRoute(RequestPredicates.GET("/set-cookie"),
|
||||
(request) -> ServerResponse.ok()
|
||||
.cookie(ResponseCookie.from("name", "value").domain("localhost").httpOnly(true).build())
|
||||
.build());
|
||||
this.webTestClient = WebTestClient.bindToRouterFunction(route)
|
||||
.configureClient()
|
||||
.baseUrl("https://api.example.com")
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSnippetGeneration() {
|
||||
File outputDir = new File("build/generated-snippets/default-snippets");
|
||||
FileSystemUtils.deleteRecursively(outputDir);
|
||||
this.webTestClient.get().uri("/").exchange().expectStatus().isOk().expectBody()
|
||||
.consumeWith(document("default-snippets"));
|
||||
this.webTestClient.get()
|
||||
.uri("/")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("default-snippets"));
|
||||
assertExpectedSnippetFilesExist(outputDir, "http-request.adoc", "http-response.adoc", "curl-request.adoc",
|
||||
"httpie-request.adoc", "request-body.adoc", "response-body.adoc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathParametersSnippet() {
|
||||
this.webTestClient.get().uri("/{foo}/{bar}", "1", "2").exchange().expectStatus().isOk().expectBody()
|
||||
.consumeWith(document("path-parameters",
|
||||
pathParameters(parameterWithName("foo").description("Foo description"),
|
||||
parameterWithName("bar").description("Bar description"))));
|
||||
this.webTestClient.get()
|
||||
.uri("/{foo}/{bar}", "1", "2")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(
|
||||
document("path-parameters", pathParameters(parameterWithName("foo").description("Foo description"),
|
||||
parameterWithName("bar").description("Bar description"))));
|
||||
assertThat(new File("build/generated-snippets/path-parameters/path-parameters.adoc")).has(content(
|
||||
tableWithTitleAndHeader(TemplateFormats.asciidoctor(), "+/{foo}/{bar}+", "Parameter", "Description")
|
||||
.row("`foo`", "Foo description").row("`bar`", "Bar description")));
|
||||
.row("`foo`", "Foo description")
|
||||
.row("`bar`", "Bar description")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestParametersSnippet() {
|
||||
this.webTestClient.get().uri("/?a=alpha&b=bravo").exchange().expectStatus().isOk().expectBody()
|
||||
.consumeWith(document("request-parameters",
|
||||
requestParameters(parameterWithName("a").description("Alpha description"),
|
||||
parameterWithName("b").description("Bravo description"))));
|
||||
this.webTestClient.get()
|
||||
.uri("/?a=alpha&b=bravo")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("request-parameters",
|
||||
requestParameters(parameterWithName("a").description("Alpha description"),
|
||||
parameterWithName("b").description("Bravo description"))));
|
||||
assertThat(new File("build/generated-snippets/request-parameters/request-parameters.adoc"))
|
||||
.has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Parameter", "Description")
|
||||
.row("`a`", "Alpha description").row("`b`", "Bravo description")));
|
||||
.has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Parameter", "Description")
|
||||
.row("`a`", "Alpha description")
|
||||
.row("`b`", "Bravo description")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,67 +159,101 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
multipartData.add("b", "bravo");
|
||||
Consumer<EntityExchangeResult<byte[]>> documentation = document("multipart",
|
||||
requestParts(partWithName("a").description("Part a"), partWithName("b").description("Part b")));
|
||||
this.webTestClient.post().uri("/upload").body(BodyInserters.fromMultipartData(multipartData)).exchange()
|
||||
.expectStatus().isOk().expectBody().consumeWith(documentation);
|
||||
this.webTestClient.post()
|
||||
.uri("/upload")
|
||||
.body(BodyInserters.fromMultipartData(multipartData))
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(documentation);
|
||||
assertThat(new File("build/generated-snippets/multipart/request-parts.adoc"))
|
||||
.has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Part", "Description").row("`a`", "Part a")
|
||||
.row("`b`", "Part b")));
|
||||
.has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Part", "Description").row("`a`", "Part a")
|
||||
.row("`b`", "Part b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithSetCookie() {
|
||||
this.webTestClient.get().uri("/set-cookie").exchange().expectStatus().isOk().expectBody()
|
||||
.consumeWith(document("set-cookie"));
|
||||
this.webTestClient.get()
|
||||
.uri("/set-cookie")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("set-cookie"));
|
||||
assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
|
||||
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK).header(HttpHeaders.SET_COOKIE,
|
||||
"name=value; Domain=localhost; HttpOnly")));
|
||||
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK).header(HttpHeaders.SET_COOKIE,
|
||||
"name=value; Domain=localhost; HttpOnly")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithCookies() {
|
||||
this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal").accept(MediaType.APPLICATION_JSON)
|
||||
.exchange().expectStatus().isOk().expectBody().consumeWith(document("curl-snippet-with-cookies"));
|
||||
this.webTestClient.get()
|
||||
.uri("/")
|
||||
.cookie("cookieName", "cookieVal")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("curl-snippet-with-cookies"));
|
||||
assertThat(new File("build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
|
||||
.withContent(String.format("$ curl 'https://api.example.com/' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n" + " --cookie 'cookieName=cookieVal'"))));
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
|
||||
.withContent(String.format("$ curl 'https://api.example.com/' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n" + " --cookie 'cookieName=cookieVal'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithEmptyParameterQueryString() {
|
||||
this.webTestClient.get().uri("/?a=").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
|
||||
.expectBody().consumeWith(document("curl-snippet-with-empty-parameter-query-string"));
|
||||
this.webTestClient.get()
|
||||
.uri("/?a=")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("curl-snippet-with-empty-parameter-query-string"));
|
||||
assertThat(
|
||||
new File("build/generated-snippets/curl-snippet-with-empty-parameter-query-string/curl-request.adoc"))
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
|
||||
.withContent(String.format("$ curl 'https://api.example.com/?a=' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json'"))));
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash").withContent(String
|
||||
.format("$ curl 'https://api.example.com/?a=' -i -X GET \\%n" + " -H 'Accept: application/json'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpieSnippetWithCookies() {
|
||||
this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal").accept(MediaType.APPLICATION_JSON)
|
||||
.exchange().expectStatus().isOk().expectBody().consumeWith(document("httpie-snippet-with-cookies"));
|
||||
this.webTestClient.get()
|
||||
.uri("/")
|
||||
.cookie("cookieName", "cookieVal")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.consumeWith(document("httpie-snippet-with-cookies"));
|
||||
assertThat(new File("build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
|
||||
.withContent(String.format("$ http GET 'https://api.example.com/' \\%n"
|
||||
+ " 'Accept:application/json' \\%n" + " 'Cookie:cookieName=cookieVal'"))));
|
||||
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
|
||||
.withContent(String.format("$ http GET 'https://api.example.com/' \\%n"
|
||||
+ " 'Accept:application/json' \\%n" + " 'Cookie:cookieName=cookieVal'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void illegalStateExceptionShouldBeThrownWhenCallDocumentWebClientNotConfigured() {
|
||||
assertThatThrownBy(() -> this.webTestClient
|
||||
.mutateWith((builder, httpHandlerBuilder, connector) -> builder.filters(List::clear).build()).get()
|
||||
.uri("/").exchange().expectBody().consumeWith(document("default-snippets")))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessage("REST Docs configuration not found. Did you forget to register a "
|
||||
+ "WebTestClientRestDocumentationConfigurer as a filter?");
|
||||
.mutateWith((builder, httpHandlerBuilder, connector) -> builder.filters(List::clear).build())
|
||||
.get()
|
||||
.uri("/")
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.consumeWith(document("default-snippets"))).isInstanceOf(IllegalStateException.class)
|
||||
.hasMessage("REST Docs configuration not found. Did you forget to register a "
|
||||
+ "WebTestClientRestDocumentationConfigurer as a filter?");
|
||||
}
|
||||
|
||||
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
|
||||
Set<File> actual = new HashSet<>(Arrays.asList(directory.listFiles()));
|
||||
Set<File> expected = Stream.of(snippets).map((snippet) -> new File(directory, snippet))
|
||||
.collect(Collectors.toSet());
|
||||
Set<File> expected = Stream.of(snippets)
|
||||
.map((snippet) -> new File(directory, snippet))
|
||||
.collect(Collectors.toSet());
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@@ -210,7 +264,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
public boolean matches(File value) {
|
||||
try {
|
||||
return delegate.matches(FileCopyUtils
|
||||
.copyToString(new InputStreamReader(new FileInputStream(value), StandardCharsets.UTF_8)));
|
||||
.copyToString(new InputStreamReader(new FileInputStream(value), StandardCharsets.UTF_8)));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
fail("Failed to read '" + value + "'", ex);
|
||||
|
||||
Reference in New Issue
Block a user