|
|
|
|
@ -49,138 +49,138 @@ import java.util.*
@@ -49,138 +49,138 @@ import java.util.*
|
|
|
|
|
*/ |
|
|
|
|
class KotlinRestTemplateHttpServiceProxyTests { |
|
|
|
|
|
|
|
|
|
private lateinit var server: MockWebServer |
|
|
|
|
private lateinit var server: MockWebServer |
|
|
|
|
|
|
|
|
|
private lateinit var testService: TestService |
|
|
|
|
private lateinit var testService: TestService |
|
|
|
|
|
|
|
|
|
private lateinit var anotherServer: MockWebServer |
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
fun setUp() { |
|
|
|
|
server = MockWebServer() |
|
|
|
|
prepareResponse() |
|
|
|
|
@BeforeEach |
|
|
|
|
fun setUp() { |
|
|
|
|
server = MockWebServer() |
|
|
|
|
prepareResponse() |
|
|
|
|
anotherServer = anotherServer() |
|
|
|
|
testService = initTestService() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun initTestService(): TestService { |
|
|
|
|
val restTemplate = RestTemplate() |
|
|
|
|
restTemplate.uriTemplateHandler = DefaultUriBuilderFactory(server.url("/").toString()) |
|
|
|
|
return HttpServiceProxyFactory.builder() |
|
|
|
|
.exchangeAdapter(RestTemplateAdapter.create(restTemplate)) |
|
|
|
|
.build() |
|
|
|
|
.createClient(TestService::class.java) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@AfterEach |
|
|
|
|
fun shutDown() { |
|
|
|
|
server.shutdown() |
|
|
|
|
testService = initTestService() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun initTestService(): TestService { |
|
|
|
|
val restTemplate = RestTemplate() |
|
|
|
|
restTemplate.uriTemplateHandler = DefaultUriBuilderFactory(server.url("/").toString()) |
|
|
|
|
return HttpServiceProxyFactory.builder() |
|
|
|
|
.exchangeAdapter(RestTemplateAdapter.create(restTemplate)) |
|
|
|
|
.build() |
|
|
|
|
.createClient(TestService::class.java) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@AfterEach |
|
|
|
|
fun shutDown() { |
|
|
|
|
server.shutdown() |
|
|
|
|
anotherServer.shutdown() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequest() { |
|
|
|
|
val response = testService.request |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.path).isEqualTo("/test") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequestWithPathVariable() { |
|
|
|
|
val response = testService.getRequestWithPathVariable("456") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response.statusCode).isEqualTo(HttpStatus.OK) |
|
|
|
|
assertThat(response.body).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.path).isEqualTo("/test/456") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequestWithDynamicUri() { |
|
|
|
|
val dynamicUri = server.url("/greeting/123").uri() |
|
|
|
|
|
|
|
|
|
val response = testService.getRequestWithDynamicUri(dynamicUri, "456") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response.orElse("empty")).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.requestUrl.uri()).isEqualTo(dynamicUri) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun postWithRequestHeader() { |
|
|
|
|
testService.postRequestWithHeader("testHeader", "testBody") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("POST") |
|
|
|
|
assertThat(request.path).isEqualTo("/test") |
|
|
|
|
assertThat(request.headers["testHeaderName"]).isEqualTo("testHeader") |
|
|
|
|
assertThat(request.body.readUtf8()).isEqualTo("testBody") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun formData() { |
|
|
|
|
val map: MultiValueMap<String, String> = LinkedMultiValueMap() |
|
|
|
|
map.add("param1", "value 1") |
|
|
|
|
map.add("param2", "value 2") |
|
|
|
|
|
|
|
|
|
testService.postForm(map) |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.headers["Content-Type"]).isEqualTo("application/x-www-form-urlencoded") |
|
|
|
|
assertThat(request.body.readUtf8()).isEqualTo("param1=value+1¶m2=value+2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// gh-30342 |
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun multipart() { |
|
|
|
|
val fileName = "testFileName" |
|
|
|
|
val originalFileName = "originalTestFileName" |
|
|
|
|
val file: MultipartFile = MockMultipartFile(fileName, originalFileName, MediaType.APPLICATION_JSON_VALUE, |
|
|
|
|
"test".toByteArray()) |
|
|
|
|
|
|
|
|
|
testService.postMultipart(file, "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.headers["Content-Type"]).startsWith("multipart/form-data;boundary=") |
|
|
|
|
assertThat(request.body.readUtf8()).containsSubsequence( |
|
|
|
|
"Content-Disposition: form-data; name=\"file\"; filename=\"originalTestFileName\"", |
|
|
|
|
"Content-Type: application/json", "Content-Length: 4", "test", |
|
|
|
|
"Content-Disposition: form-data; name=\"anotherPart\"", "Content-Type: text/plain;charset=UTF-8", |
|
|
|
|
"Content-Length: 5", "test2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun putRequestWithCookies() { |
|
|
|
|
testService.putRequestWithCookies("test1", "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("PUT") |
|
|
|
|
assertThat(request.getHeader("Cookie")) |
|
|
|
|
.isEqualTo("firstCookie=test1; secondCookie=test2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun putRequestWithSameNameCookies() { |
|
|
|
|
testService.putRequestWithSameNameCookies("test1", "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("PUT") |
|
|
|
|
assertThat(request.getHeader("Cookie")) |
|
|
|
|
.isEqualTo("testCookie=test1; testCookie=test2") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequest() { |
|
|
|
|
val response = testService.request |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.path).isEqualTo("/test") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequestWithPathVariable() { |
|
|
|
|
val response = testService.getRequestWithPathVariable("456") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response.statusCode).isEqualTo(HttpStatus.OK) |
|
|
|
|
assertThat(response.body).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.path).isEqualTo("/test/456") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun getRequestWithDynamicUri() { |
|
|
|
|
val dynamicUri = server.url("/greeting/123").uri() |
|
|
|
|
|
|
|
|
|
val response = testService.getRequestWithDynamicUri(dynamicUri, "456") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(response.orElse("empty")).isEqualTo("Hello Spring!") |
|
|
|
|
assertThat(request.method).isEqualTo("GET") |
|
|
|
|
assertThat(request.requestUrl.uri()).isEqualTo(dynamicUri) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun postWithRequestHeader() { |
|
|
|
|
testService.postRequestWithHeader("testHeader", "testBody") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("POST") |
|
|
|
|
assertThat(request.path).isEqualTo("/test") |
|
|
|
|
assertThat(request.headers["testHeaderName"]).isEqualTo("testHeader") |
|
|
|
|
assertThat(request.body.readUtf8()).isEqualTo("testBody") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun formData() { |
|
|
|
|
val map: MultiValueMap<String, String> = LinkedMultiValueMap() |
|
|
|
|
map.add("param1", "value 1") |
|
|
|
|
map.add("param2", "value 2") |
|
|
|
|
|
|
|
|
|
testService.postForm(map) |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.headers["Content-Type"]).isEqualTo("application/x-www-form-urlencoded") |
|
|
|
|
assertThat(request.body.readUtf8()).isEqualTo("param1=value+1¶m2=value+2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// gh-30342 |
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun multipart() { |
|
|
|
|
val fileName = "testFileName" |
|
|
|
|
val originalFileName = "originalTestFileName" |
|
|
|
|
val file: MultipartFile = MockMultipartFile(fileName, originalFileName, MediaType.APPLICATION_JSON_VALUE, |
|
|
|
|
"test".toByteArray()) |
|
|
|
|
|
|
|
|
|
testService.postMultipart(file, "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.headers["Content-Type"]).startsWith("multipart/form-data;boundary=") |
|
|
|
|
assertThat(request.body.readUtf8()).containsSubsequence( |
|
|
|
|
"Content-Disposition: form-data; name=\"file\"; filename=\"originalTestFileName\"", |
|
|
|
|
"Content-Type: application/json", "Content-Length: 4", "test", |
|
|
|
|
"Content-Disposition: form-data; name=\"anotherPart\"", "Content-Type: text/plain;charset=UTF-8", |
|
|
|
|
"Content-Length: 5", "test2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun putRequestWithCookies() { |
|
|
|
|
testService.putRequestWithCookies("test1", "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("PUT") |
|
|
|
|
assertThat(request.getHeader("Cookie")) |
|
|
|
|
.isEqualTo("firstCookie=test1; secondCookie=test2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
fun putRequestWithSameNameCookies() { |
|
|
|
|
testService.putRequestWithSameNameCookies("test1", "test2") |
|
|
|
|
|
|
|
|
|
val request = server.takeRequest() |
|
|
|
|
assertThat(request.method).isEqualTo("PUT") |
|
|
|
|
assertThat(request.getHeader("Cookie")) |
|
|
|
|
.isEqualTo("testCookie=test1; testCookie=test2") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Throws(InterruptedException::class) |
|
|
|
|
@ -236,11 +236,11 @@ class KotlinRestTemplateHttpServiceProxyTests {
@@ -236,11 +236,11 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun prepareResponse() { |
|
|
|
|
val response = MockResponse() |
|
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!") |
|
|
|
|
server.enqueue(response) |
|
|
|
|
} |
|
|
|
|
private fun prepareResponse() { |
|
|
|
|
val response = MockResponse() |
|
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!") |
|
|
|
|
server.enqueue(response) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun anotherServer(): MockWebServer { |
|
|
|
|
val anotherServer = MockWebServer() |
|
|
|
|
@ -250,34 +250,34 @@ class KotlinRestTemplateHttpServiceProxyTests {
@@ -250,34 +250,34 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|
|
|
|
return anotherServer |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private interface TestService { |
|
|
|
|
private interface TestService { |
|
|
|
|
|
|
|
|
|
@get:GetExchange("/test") |
|
|
|
|
val request: String |
|
|
|
|
@get:GetExchange("/test") |
|
|
|
|
val request: String |
|
|
|
|
|
|
|
|
|
@GetExchange("/test/{id}") |
|
|
|
|
fun getRequestWithPathVariable(@PathVariable id: String): ResponseEntity<String> |
|
|
|
|
@GetExchange("/test/{id}") |
|
|
|
|
fun getRequestWithPathVariable(@PathVariable id: String): ResponseEntity<String> |
|
|
|
|
|
|
|
|
|
@GetExchange("/test/{id}") |
|
|
|
|
fun getRequestWithDynamicUri(@Nullable uri: URI, @PathVariable id: String): Optional<String> |
|
|
|
|
@GetExchange("/test/{id}") |
|
|
|
|
fun getRequestWithDynamicUri(@Nullable uri: URI, @PathVariable id: String): Optional<String> |
|
|
|
|
|
|
|
|
|
@PostExchange("/test") |
|
|
|
|
fun postRequestWithHeader(@RequestHeader("testHeaderName") testHeader: String, |
|
|
|
|
@RequestBody requestBody: String) |
|
|
|
|
@PostExchange("/test") |
|
|
|
|
fun postRequestWithHeader(@RequestHeader("testHeaderName") testHeader: String, |
|
|
|
|
@RequestBody requestBody: String) |
|
|
|
|
|
|
|
|
|
@PostExchange(contentType = "application/x-www-form-urlencoded") |
|
|
|
|
fun postForm(@RequestParam params: MultiValueMap<String, String>) |
|
|
|
|
@PostExchange(contentType = "application/x-www-form-urlencoded") |
|
|
|
|
fun postForm(@RequestParam params: MultiValueMap<String, String>) |
|
|
|
|
|
|
|
|
|
@PostExchange |
|
|
|
|
fun postMultipart(file: MultipartFile, @RequestPart anotherPart: String) |
|
|
|
|
@PostExchange |
|
|
|
|
fun postMultipart(file: MultipartFile, @RequestPart anotherPart: String) |
|
|
|
|
|
|
|
|
|
@PutExchange |
|
|
|
|
fun putRequestWithCookies(@CookieValue firstCookie: String, |
|
|
|
|
@CookieValue secondCookie: String) |
|
|
|
|
@PutExchange |
|
|
|
|
fun putRequestWithCookies(@CookieValue firstCookie: String, |
|
|
|
|
@CookieValue secondCookie: String) |
|
|
|
|
|
|
|
|
|
@PutExchange |
|
|
|
|
fun putRequestWithSameNameCookies(@CookieValue("testCookie") firstCookie: String, |
|
|
|
|
@CookieValue("testCookie") secondCookie: String) |
|
|
|
|
@PutExchange |
|
|
|
|
fun putRequestWithSameNameCookies(@CookieValue("testCookie") firstCookie: String, |
|
|
|
|
@CookieValue("testCookie") secondCookie: String) |
|
|
|
|
|
|
|
|
|
@GetExchange("/greeting") |
|
|
|
|
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String> |
|
|
|
|
@ -288,6 +288,6 @@ class KotlinRestTemplateHttpServiceProxyTests {
@@ -288,6 +288,6 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|
|
|
|
|
|
|
|
|
@GetExchange("/greeting") |
|
|
|
|
fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String> |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |