|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -52,23 +52,25 @@ import static org.mockito.Mockito.mock; |
|
|
|
* Tests for the {@link DefaultResponseCreator} factory methods. |
|
|
|
* Tests for the {@link DefaultResponseCreator} factory methods. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Ashley Scopes |
|
|
|
* @author Ashley Scopes |
|
|
|
|
|
|
|
* @author Rossen Stoyanchev |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class DefaultResponseCreatorTests { |
|
|
|
class DefaultResponseCreatorTests { |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "expect status to be set [{0}]") |
|
|
|
@ParameterizedTest(name = "expect status to be set [{0}]") |
|
|
|
@ValueSource(ints = {200, 401, 429}) |
|
|
|
@ValueSource(ints = {200, 401, 429}) |
|
|
|
void expectStatus(int statusValue) throws IOException { |
|
|
|
void expectStatus(int statusValue) throws IOException { |
|
|
|
HttpStatus status = HttpStatus.valueOf(statusValue); |
|
|
|
HttpStatus status = HttpStatus.valueOf(statusValue); |
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(status)); |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(status); |
|
|
|
assertThat(response.getStatusCode()).isEqualTo(status); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(createResponse(creator).getStatusCode()).isEqualTo(status); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setBodyFromString() throws IOException { |
|
|
|
void setBodyFromString() throws IOException { |
|
|
|
// Use unicode codepoint for "thinking" emoji to help verify correct encoding is used internally.
|
|
|
|
// Use unicode codepoint for "thinking" emoji to help verify correct encoding is used internally.
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body("hello, world! \uD83E\uDD14"); |
|
|
|
.body("hello, world! \uD83E\uDD14")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(IOUtils.toByteArray(response.getBody())) |
|
|
|
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())) |
|
|
|
.isEqualTo("hello, world! \uD83E\uDD14".getBytes(StandardCharsets.UTF_8)); |
|
|
|
.isEqualTo("hello, world! \uD83E\uDD14".getBytes(StandardCharsets.UTF_8)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -81,68 +83,62 @@ class DefaultResponseCreatorTests { |
|
|
|
.isTrue(); |
|
|
|
.isTrue(); |
|
|
|
|
|
|
|
|
|
|
|
Charset charsetObj = Charset.forName(charset); |
|
|
|
Charset charsetObj = Charset.forName(charset); |
|
|
|
|
|
|
|
|
|
|
|
String content = "hello! €½$~@><·─"; |
|
|
|
String content = "hello! €½$~@><·─"; |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(content, charsetObj); |
|
|
|
.body(content, charsetObj)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ByteBuffer expectBuff = charsetObj.encode(content); |
|
|
|
ByteBuffer expectBuff = charsetObj.encode(content); |
|
|
|
byte[] expect = new byte[expectBuff.remaining()]; |
|
|
|
byte[] expect = new byte[expectBuff.remaining()]; |
|
|
|
expectBuff.get(expect); |
|
|
|
expectBuff.get(expect); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(expect); |
|
|
|
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(expect); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setBodyFromByteArray() throws IOException { |
|
|
|
void setBodyFromByteArray() throws IOException { |
|
|
|
byte[] body = { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 }; |
|
|
|
byte[] body = { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 }; |
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(body)); |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); |
|
|
|
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(body); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(body); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setBodyFromResource() throws IOException { |
|
|
|
void setBodyFromResource() throws IOException { |
|
|
|
byte[] resourceContent = {7, 14, 21, 28, 35}; |
|
|
|
byte[] resourceContent = {7, 14, 21, 28, 35}; |
|
|
|
|
|
|
|
|
|
|
|
Resource resource = mock(Resource.class); |
|
|
|
Resource resource = mock(Resource.class); |
|
|
|
given(resource.getInputStream()).willReturn(new ByteArrayInputStream(resourceContent)); |
|
|
|
given(resource.getInputStream()).willReturn(new ByteArrayInputStream(resourceContent)); |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(resource)); |
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(resource)); |
|
|
|
|
|
|
|
|
|
|
|
then(resource).should().getInputStream(); |
|
|
|
then(resource).should().getInputStream(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(resourceContent); |
|
|
|
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(resourceContent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setContentType() throws IOException { |
|
|
|
void setContentType() throws IOException { |
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
MediaType mediaType = MediaType.APPLICATION_JSON; |
|
|
|
.contentType(MediaType.APPLICATION_JSON)); |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).contentType(mediaType); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON); |
|
|
|
assertThat(createResponse(creator).getHeaders().getContentType()).isEqualTo(mediaType); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setLocation() throws IOException { |
|
|
|
void setLocation() throws IOException { |
|
|
|
URI uri = UriComponentsBuilder |
|
|
|
URI uri = URI.create("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html"); |
|
|
|
.fromUriString("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html") |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).location(uri); |
|
|
|
.build() |
|
|
|
|
|
|
|
.toUri(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).location(uri)); |
|
|
|
assertThat(createResponse(creator).getHeaders().getLocation()).isEqualTo(uri); |
|
|
|
assertThat(response.getHeaders().getLocation()).isEqualTo(uri); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setHeader() throws IOException { |
|
|
|
void setHeader() throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
.header("foo", "bar") |
|
|
|
.header("foo", "bar") |
|
|
|
.header("baz", "bork") |
|
|
|
.header("baz", "bork") |
|
|
|
.headers("lorem", "ipsum", "dolor", "sit", "amet")); |
|
|
|
.header("lorem", "ipsum", "dolor", "sit", "amet"); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = response.getHeaders(); |
|
|
|
HttpHeaders headers = createResponse(creator).getHeaders(); |
|
|
|
assertThat(headers.get("foo")).isNotNull().isEqualTo(Collections.singletonList("bar")); |
|
|
|
assertThat(headers.get("foo")).isNotNull().isEqualTo(Collections.singletonList("bar")); |
|
|
|
assertThat(headers.get("baz")).isNotNull().isEqualTo(Collections.singletonList("bork")); |
|
|
|
assertThat(headers.get("baz")).isNotNull().isEqualTo(Collections.singletonList("bork")); |
|
|
|
assertThat(headers.get("lorem")).isNotNull().isEqualTo(Arrays.asList("ipsum", "dolor", "sit", "amet")); |
|
|
|
assertThat(headers.get("lorem")).isNotNull().isEqualTo(Arrays.asList("ipsum", "dolor", "sit", "amet")); |
|
|
|
@ -158,11 +154,11 @@ class DefaultResponseCreatorTests { |
|
|
|
HttpHeaders secondHeaders = new HttpHeaders(); |
|
|
|
HttpHeaders secondHeaders = new HttpHeaders(); |
|
|
|
secondHeaders.setAllow(Collections.singleton(HttpMethod.PUT)); |
|
|
|
secondHeaders.setAllow(Collections.singleton(HttpMethod.PUT)); |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
.headers(firstHeaders) |
|
|
|
.headers(firstHeaders) |
|
|
|
.headers(secondHeaders)); |
|
|
|
.headers(secondHeaders); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders responseHeaders = response.getHeaders(); |
|
|
|
HttpHeaders responseHeaders = createResponse(creator).getHeaders(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(responseHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON); |
|
|
|
assertThat(responseHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON); |
|
|
|
assertThat(responseHeaders.getOrigin()).isEqualTo("https://github.com"); |
|
|
|
assertThat(responseHeaders.getOrigin()).isEqualTo("https://github.com"); |
|
|
|
@ -176,12 +172,12 @@ class DefaultResponseCreatorTests { |
|
|
|
ResponseCookie thirdCookie = ResponseCookie.from("cookie-cookie", "cookies").build(); |
|
|
|
ResponseCookie thirdCookie = ResponseCookie.from("cookie-cookie", "cookies").build(); |
|
|
|
ResponseCookie fourthCookie = ResponseCookie.from("foobar", "bazbork").build(); |
|
|
|
ResponseCookie fourthCookie = ResponseCookie.from("foobar", "bazbork").build(); |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
.cookie(firstCookie) |
|
|
|
.cookies(firstCookie) |
|
|
|
.cookie(secondCookie) |
|
|
|
.cookies(secondCookie) |
|
|
|
.cookies(thirdCookie, fourthCookie)); |
|
|
|
.cookies(thirdCookie, fourthCookie); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders responseHeaders = response.getHeaders(); |
|
|
|
HttpHeaders responseHeaders = createResponse(creator).getHeaders(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE)) |
|
|
|
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE)) |
|
|
|
.isNotNull() |
|
|
|
.isNotNull() |
|
|
|
@ -207,11 +203,11 @@ class DefaultResponseCreatorTests { |
|
|
|
firstCookies.add(thirdCookie.getName(), thirdCookie); |
|
|
|
firstCookies.add(thirdCookie.getName(), thirdCookie); |
|
|
|
firstCookies.add(fourthCookie.getName(), fourthCookie); |
|
|
|
firstCookies.add(fourthCookie.getName(), fourthCookie); |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK) |
|
|
|
.cookies(firstCookies) |
|
|
|
.cookies(firstCookies) |
|
|
|
.cookies(secondCookies)); |
|
|
|
.cookies(secondCookies); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders responseHeaders = response.getHeaders(); |
|
|
|
HttpHeaders responseHeaders = createResponse(creator).getHeaders(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE)) |
|
|
|
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE)) |
|
|
|
.isNotNull() |
|
|
|
.isNotNull() |
|
|
|
@ -227,4 +223,5 @@ class DefaultResponseCreatorTests { |
|
|
|
URI uri = UriComponentsBuilder.fromUriString("/foo/bar").build().toUri(); |
|
|
|
URI uri = UriComponentsBuilder.fromUriString("/foo/bar").build().toUri(); |
|
|
|
return creator.createResponse(new MockClientHttpRequest(HttpMethod.POST, uri)); |
|
|
|
return creator.createResponse(new MockClientHttpRequest(HttpMethod.POST, uri)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|