Skip to content

Commit 8160f30

Browse files
committed
mock Response.toString() + unit test
1 parent ba7aedb commit 8160f30

File tree

2 files changed

+119
-41
lines changed

2 files changed

+119
-41
lines changed

src/test/java/com/amihaiemil/docker/mock/Response.java

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import java.io.IOException;
3030
import java.nio.ByteBuffer;
3131
import java.nio.channels.WritableByteChannel;
32-
import java.time.Instant;
32+
import java.util.Date;
3333
import java.util.Locale;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
3436
import org.apache.http.Header;
3537
import org.apache.http.HeaderIterator;
3638
import org.apache.http.HttpEntity;
@@ -40,9 +42,10 @@
4042
import org.apache.http.StatusLine;
4143
import org.apache.http.entity.ContentType;
4244
import org.apache.http.entity.StringEntity;
45+
import org.apache.http.impl.client.BasicResponseHandler;
46+
import org.apache.http.message.BasicHttpResponse;
4347
import org.apache.http.message.BasicStatusLine;
4448
import org.apache.http.params.HttpParams;
45-
import org.apache.http.util.EntityUtils;
4649

4750
/**
4851
* An {@link HttpResponse} suitable for tests. Can be configured with
@@ -51,20 +54,14 @@
5154
* @author George Aristy (george.aristy@gmail.com)
5255
* @version $Id$
5356
* @since 0.0.1
54-
* @todo #79:30min The 'asString()' method needs a little more work (fix the
55-
* formatting on the date header value, etc) and then test the 'printTo()'
56-
* method in conjunction with the UnixServer.
5757
*/
5858
public final class Response implements HttpResponse {
59+
5960
/**
60-
* This response's status line.
61+
* Its backbone, holding what we need.
6162
*/
62-
private final StatusLine statusLine;
63-
/**
64-
* This response's payload.
65-
*/
66-
private final HttpEntity payload;
67-
63+
private HttpResponse backbone;
64+
6865
/**
6966
* Ctor.
7067
* <p>
@@ -82,17 +79,26 @@ public Response(final int status) {
8279
* @param jsonPayload The json payload
8380
*/
8481
public Response(final int status, final String jsonPayload) {
85-
this.statusLine = new BasicStatusLine(
86-
new ProtocolVersion("HTTP", 1, 1), status, ""
82+
this.backbone = new BasicHttpResponse(
83+
new BasicStatusLine(
84+
new ProtocolVersion("HTTP", 1, 1), status, "REASON"
85+
)
8786
);
88-
this.payload = new StringEntity(
89-
jsonPayload, ContentType.APPLICATION_JSON
87+
this.backbone.setEntity(
88+
new StringEntity(
89+
jsonPayload, ContentType.APPLICATION_JSON
90+
)
91+
);
92+
this.backbone.setHeader("Date", new Date().toString());
93+
this.backbone.setHeader(
94+
"Content-Length", String.valueOf(jsonPayload.getBytes().length)
9095
);
96+
this.backbone.setHeader("Content-Type", "application/json");
9197
}
9298

9399
@Override
94100
public StatusLine getStatusLine() {
95-
return this.statusLine;
101+
return this.backbone.getStatusLine();
96102
}
97103

98104
@Override
@@ -124,7 +130,7 @@ public void setReasonPhrase(final String reason)
124130

125131
@Override
126132
public HttpEntity getEntity() {
127-
return this.payload;
133+
return this.backbone.getEntity();
128134
}
129135

130136
@Override
@@ -235,38 +241,39 @@ public void setParams(final HttpParams params) {
235241
public void printTo(final WritableByteChannel channel) throws IOException {
236242
channel.write(
237243
ByteBuffer.wrap(
238-
this.asString().getBytes(Charsets.UTF8_CHARSET)
244+
this.toString().getBytes(Charsets.UTF8_CHARSET)
239245
)
240246
);
241247
}
242248

243249
/**
244250
* This response as a string.
245251
* @return String representation of this {@link Response}.
246-
* @throws IOException If an error occurs.
247252
*/
248-
private String asString() throws IOException {
253+
@Override
254+
public String toString() {
249255
final String CRLF = "" + (char) 0x0D + (char) 0x0A;
250-
final StringBuilder builder = new StringBuilder("HTTP/")
251-
.append(this.statusLine.getProtocolVersion())
252-
.append(" ")
253-
.append(this.statusLine.getStatusCode())
254-
.append(" ")
255-
.append(this.statusLine.getReasonPhrase())
256-
.append(CRLF)
257-
.append("Date: ")
258-
.append(Instant.now())
259-
.append(CRLF);
260-
if (this.payload.getContentLength() > 0) {
261-
builder.append("ContentType: ")
262-
.append(this.payload.getContentType().getValue())
263-
.append(CRLF)
264-
.append("Content-Length: ")
265-
.append(this.payload.getContentLength())
266-
.append(CRLF)
267-
.append(CRLF)
268-
.append(EntityUtils.toString(this.payload));
256+
try {
257+
final StringBuilder builder = new StringBuilder()
258+
.append(this.backbone.getStatusLine().toString())
259+
.append(CRLF)
260+
.append(
261+
Stream.of(
262+
this.backbone.getAllHeaders()
263+
).map(header -> header.toString())
264+
.collect(Collectors.joining(CRLF))
265+
)
266+
.append(CRLF).append(CRLF)
267+
.append(
268+
new BasicResponseHandler().handleEntity(
269+
this.backbone.getEntity()
270+
)
271+
);
272+
return builder.toString();
273+
} catch (final IOException ex) {
274+
throw new IllegalStateException(
275+
"IOException when reading the HTTP response. " + ex
276+
);
269277
}
270-
return builder.toString();
271278
}
272279
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker.mock;
27+
28+
import javax.json.Json;
29+
import org.apache.http.HttpStatus;
30+
import org.hamcrest.MatcherAssert;
31+
import org.hamcrest.Matchers;
32+
import org.junit.Test;
33+
34+
/**
35+
* Unit tests for {@link Response}.
36+
* @author Mihai Andronache (amihaiemil@gmail.com)
37+
* @version $Id$
38+
* @since 0.0.2
39+
*/
40+
public final class ResponseTestCase {
41+
42+
/**
43+
* {@link Response} can return its String representation.
44+
*/
45+
@Test
46+
public void toStringWorks() {
47+
MatcherAssert.assertThat(
48+
new Response(
49+
HttpStatus.SC_OK,
50+
Json.createArrayBuilder()
51+
.add(
52+
Json.createObjectBuilder()
53+
.add("Id", "sha256:e216a057b1cb1efc1")
54+
).add(
55+
Json.createObjectBuilder()
56+
.add("Id", "sha256:3e314f95dcace0f5e")
57+
).build().toString()
58+
).toString(),
59+
Matchers.allOf(
60+
Matchers.startsWith("HTTP/1.1 200 REASON"),
61+
Matchers.endsWith(
62+
// @checkstyle LineLength (1 lines)
63+
"[{\"Id\":\"sha256:e216a057b1cb1efc1\"},{\"Id\":\"sha256:3e314f95dcace0f5e\"}]"
64+
),
65+
Matchers.containsString("Content-Length: 69"),
66+
Matchers.containsString("" + (char) 0x0D + (char) 0x0A)
67+
)
68+
);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)