Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Sep 27, 2022
1 parent d14477e commit b1ee44f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -84,6 +80,7 @@ public DefaultResponseCreator body(String content) {

/**
* Set the body from a string using the given character set.
* @since 6.0
*/
public DefaultResponseCreator body(String content, Charset charset) {
this.content = content.getBytes(charset);
Expand Down Expand Up @@ -123,22 +120,13 @@ public DefaultResponseCreator location(URI location) {
}

/**
* Add a single header.
* Add a response header with one or more values.
* @since 6.0
*/
public DefaultResponseCreator header(String name, String value) {
// This is really just an alias, but it makes the interface more fluent.
return headers(name, value);
}

/**
* Add one or more headers.
*/
public DefaultResponseCreator headers(String name, String ... value) {
List<String> valueList = Stream.of(value)
.filter(Objects::nonNull)
.collect(Collectors.toList());

this.headers.addAll(name, valueList);
public DefaultResponseCreator header(String name, String ... headerValues) {
for (String headerValue : headerValues) {
this.headers.add(name, headerValue);
}
return this;
}

Expand All @@ -150,34 +138,23 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
return this;
}

/**
* Add a single cookie.
*/
public DefaultResponseCreator cookie(ResponseCookie cookie) {
// This is really just an alias, but it makes the interface more fluent.
return cookies(cookie);
}

/**
* Add one or more cookies.
* @since 6.0
*/
public DefaultResponseCreator cookies(ResponseCookie... cookies) {
for (ResponseCookie cookie : cookies) {
this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString());
}

return this;
}

/**
* Copy all given cookies.
* Copy all cookies from the given {@link MultiValueMap}.
* @since 6.0
*/
public DefaultResponseCreator cookies(MultiValueMap<String, ResponseCookie> cookies) {
cookies.values()
.stream()
.flatMap(List::stream)
.forEach(cookie -> this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString()));

public DefaultResponseCreator cookies(MultiValueMap<String, ResponseCookie> multiValueMap) {
multiValueMap.values().forEach(cookies -> cookies.forEach(this::cookies));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static DefaultResponseCreator withCreatedEntity(URI location) {

/**
* {@code ResponseCreator} for a 202 response (ACCEPTED).
* @since 6.0
*/
public static DefaultResponseCreator withAccepted() {
return new DefaultResponseCreator(HttpStatus.ACCEPTED);
Expand Down Expand Up @@ -113,35 +114,40 @@ public static DefaultResponseCreator withUnauthorizedRequest() {

/**
* {@code ResponseCreator} for a 403 response (FORBIDDEN).
* @since 6.0
*/
public static DefaultResponseCreator withForbiddenRequest() {
return new DefaultResponseCreator(HttpStatus.FORBIDDEN);
}

/**
* {@code ResponseCreator} for a 404 response (NOT_FOUND).
* @since 6.0
*/
public static DefaultResponseCreator withResourceNotFound() {
return new DefaultResponseCreator(HttpStatus.NOT_FOUND);
}

/**
* {@code ResponseCreator} for a 409 response (CONFLICT).
* @since 6.0
*/
public static DefaultResponseCreator withRequestConflict() {
return new DefaultResponseCreator(HttpStatus.CONFLICT);
}

/**
* {@code ResponseCreator} for a 429 ratelimited response (TOO_MANY_REQUESTS).
* @since 6.0
*/
public static DefaultResponseCreator withTooManyRequests() {
return new DefaultResponseCreator(HttpStatus.TOO_MANY_REQUESTS);
}

/**
* {@code ResponseCreator} for a 429 ratelimited response (TOO_MANY_REQUESTS) with a {@code Retry-After} header
* in seconds.
* {@code ResponseCreator} for a 429 rate-limited response (TOO_MANY_REQUESTS)
* with a {@code Retry-After} header in seconds.
* @since 6.0
*/
public static DefaultResponseCreator withTooManyRequests(int retryAfter) {
return new DefaultResponseCreator(HttpStatus.TOO_MANY_REQUESTS)
Expand All @@ -157,20 +163,23 @@ public static DefaultResponseCreator withServerError() {

/**
* {@code ResponseCreator} for a 502 response (BAD_GATEWAY).
* @since 6.0
*/
public static DefaultResponseCreator withBadGateway() {
return new DefaultResponseCreator(HttpStatus.BAD_GATEWAY);
}

/**
* {@code ResponseCreator} for a 503 response (SERVICE_UNAVAILABLE).
* @since 6.0
*/
public static DefaultResponseCreator withServiceUnavailable() {
return new DefaultResponseCreator(HttpStatus.SERVICE_UNAVAILABLE);
}

/**
* {@code ResponseCreator} for a 504 response (GATEWAY_TIMEOUT).
* @since 6.0
*/
public static DefaultResponseCreator withGatewayTimeout() {
return new DefaultResponseCreator(HttpStatus.GATEWAY_TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,23 +52,25 @@
* Tests for the {@link DefaultResponseCreator} factory methods.
*
* @author Ashley Scopes
* @author Rossen Stoyanchev
*/
class DefaultResponseCreatorTests {

@ParameterizedTest(name = "expect status to be set [{0}]")
@ValueSource(ints = {200, 401, 429})
void expectStatus(int statusValue) throws IOException {
HttpStatus status = HttpStatus.valueOf(statusValue);
ClientHttpResponse response = createResponse(new DefaultResponseCreator(status));
assertThat(response.getStatusCode()).isEqualTo(status);
DefaultResponseCreator creator = new DefaultResponseCreator(status);

assertThat(createResponse(creator).getStatusCode()).isEqualTo(status);
}

@Test
void setBodyFromString() throws IOException {
// Use unicode codepoint for "thinking" emoji to help verify correct encoding is used internally.
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
.body("hello, world! \uD83E\uDD14"));
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).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));
}

Expand All @@ -81,68 +83,62 @@ void setBodyFromStringWithCharset(String charset) throws IOException {
.isTrue();

Charset charsetObj = Charset.forName(charset);

String content = "hello! €½$~@><·─";

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
.body(content, charsetObj));
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(content, charsetObj);

ByteBuffer expectBuff = charsetObj.encode(content);
byte[] expect = new byte[expectBuff.remaining()];
expectBuff.get(expect);

assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(expect);
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(expect);
}

@Test
void setBodyFromByteArray() throws IOException {
byte[] body = { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 };
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(body));
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(body);
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body);

assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(body);
}

@Test
void setBodyFromResource() throws IOException {
byte[] resourceContent = {7, 14, 21, 28, 35};

Resource resource = mock(Resource.class);
given(resource.getInputStream()).willReturn(new ByteArrayInputStream(resourceContent));

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(resource));

then(resource).should().getInputStream();

assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(resourceContent);
}

@Test
void setContentType() throws IOException {
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON));
MediaType mediaType = 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
void setLocation() throws IOException {
URI uri = UriComponentsBuilder
.fromUriString("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html")
.build()
.toUri();
URI uri = URI.create("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html");
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).location(uri);

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).location(uri));
assertThat(response.getHeaders().getLocation()).isEqualTo(uri);
assertThat(createResponse(creator).getHeaders().getLocation()).isEqualTo(uri);
}

@Test
void setHeader() throws IOException {

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
.header("foo", "bar")
.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("baz")).isNotNull().isEqualTo(Collections.singletonList("bork"));
assertThat(headers.get("lorem")).isNotNull().isEqualTo(Arrays.asList("ipsum", "dolor", "sit", "amet"));
Expand All @@ -158,11 +154,11 @@ void setHeaders() throws IOException {
HttpHeaders secondHeaders = new HttpHeaders();
secondHeaders.setAllow(Collections.singleton(HttpMethod.PUT));

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
.headers(firstHeaders)
.headers(secondHeaders));
.headers(secondHeaders);

HttpHeaders responseHeaders = response.getHeaders();
HttpHeaders responseHeaders = createResponse(creator).getHeaders();

assertThat(responseHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(responseHeaders.getOrigin()).isEqualTo("https://github.com");
Expand All @@ -176,12 +172,12 @@ void setCookie() throws IOException {
ResponseCookie thirdCookie = ResponseCookie.from("cookie-cookie", "cookies").build();
ResponseCookie fourthCookie = ResponseCookie.from("foobar", "bazbork").build();

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
.cookie(firstCookie)
.cookie(secondCookie)
.cookies(thirdCookie, fourthCookie));
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
.cookies(firstCookie)
.cookies(secondCookie)
.cookies(thirdCookie, fourthCookie);

HttpHeaders responseHeaders = response.getHeaders();
HttpHeaders responseHeaders = createResponse(creator).getHeaders();

assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE))
.isNotNull()
Expand All @@ -207,11 +203,11 @@ void setCookies() throws IOException {
firstCookies.add(thirdCookie.getName(), thirdCookie);
firstCookies.add(fourthCookie.getName(), fourthCookie);

ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
.cookies(firstCookies)
.cookies(secondCookies));
.cookies(secondCookies);

HttpHeaders responseHeaders = response.getHeaders();
HttpHeaders responseHeaders = createResponse(creator).getHeaders();

assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE))
.isNotNull()
Expand All @@ -227,4 +223,5 @@ private static ClientHttpResponse createResponse(DefaultResponseCreator creator)
URI uri = UriComponentsBuilder.fromUriString("/foo/bar").build().toUri();
return creator.createResponse(new MockClientHttpRequest(HttpMethod.POST, uri));
}

}

0 comments on commit b1ee44f

Please sign in to comment.