Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/main/java/com/amihaiemil/docker/SocketResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import com.jcabi.http.Request;
import com.jcabi.http.Response;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* HTTP Response comming from the Unix Socket.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #14:30min Implement and test the headers() method.
* @todo #14:30min Refine the body reading logic. Have to take into account
* the Transfer-Encoding header and cover the empty body case.
*/
Expand Down Expand Up @@ -85,7 +87,29 @@ public String reason() {

@Override
public Map<String, List<String>> headers() {
return null;
final int endHeaders;
if(this.response.indexOf("\n\n") != -1) {
endHeaders = this.response.indexOf("\n\n");
} else {
endHeaders = this.response.length();
}
final String hdrs = this.response.substring(
this.response.indexOf("\n"), endHeaders
);
final Map<String, List<String>> headers;
if(hdrs.trim().isEmpty()) {
headers = new HashMap<>();
} else {
headers = Arrays.asList(
hdrs.trim().split("\n")
).stream().collect(
Collectors.toMap(
h -> h.split(":")[0],
h -> Arrays.asList(h.split(":")[1].trim().split(", "))
)
);
}
return headers;
}

@Override
Expand Down
119 changes: 99 additions & 20 deletions src/test/java/com/amihaiemil/docker/SocketResponseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
* Unit tests for SocketResponse.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle RegexpSingleline (200 lines)
* @checkstyle OperatorWrap (200 lines)
*/
public final class SocketResponseTestCase {

Expand All @@ -45,15 +46,15 @@ public final class SocketResponseTestCase {
*/
@Test
public void hasStatus() {
final String responseString = "HTTP/1.1 200 OK\n" +
"Api-Version: 1.35\n" +
"Content-Type: application/json\n" +
"Docker-Experimental: false\n" +
"Ostype: linux\n" +
"Server: Docker/17.12.0-ce (linux)\n" +
"Date: Sun, 04 Feb 2018 09:07:13 GMT\n" +
"Connection: close\n" +
"Content-Length: 0";
final String responseString = "HTTP/1.1 200 OK\n"
+ "Api-Version: 1.35\n"
+ "Content-Type: application/json\n"
+ "Docker-Experimental: false\n"
+ "Ostype: linux\n"
+ "Server: Docker/17.12.0-ce (linux)\n"
+ "Date: Sun, 04 Feb 2018 09:07:13 GMT\n"
+ "Connection: close\n"
+ "Content-Length: 0";
final Response resp = new SocketResponse(null, responseString);
MatcherAssert.assertThat(resp.status(), Matchers.is(200));
}
Expand All @@ -63,16 +64,94 @@ public void hasStatus() {
*/
@Test
public void hasReason() {
final String responseString = "HTTP/1.1 200 OK\n" +
"Api-Version: 1.35\n" +
"Content-Type: application/json\n" +
"Docker-Experimental: false\n" +
"Ostype: linux\n" +
"Server: Docker/17.12.0-ce (linux)\n" +
"Date: Sun, 04 Feb 2018 09:07:13 GMT\n" +
"Connection: close\n" +
"Content-Length: 0";
final String responseString = "HTTP/1.1 200 OK\n"
+ "Api-Version: 1.35\n"
+ "Content-Type: application/json\n"
+ "Docker-Experimental: false\n"
+ "Ostype: linux\n"
+ "Server: Docker/17.12.0-ce (linux)\n"
+ "Date: Sun, 04 Feb 2018 09:07:13 GMT\n"
+ "Connection: close\n"
+ "Content-Length: 0";
final Response resp = new SocketResponse(null, responseString);
MatcherAssert.assertThat(resp.reason(), Matchers.equalTo("OK"));
}

/**
* A SocketResponse can return its headers if there is no body afterwards.
*/
@Test
public void headersWithNoContent() {
final String responseString = "HTTP/1.1 200 OK\n"
+ "Api-Version: 1.35\n"
+ "Content-Type: application/json\n"
+ "Docker-Experimental: false\n"
+ "Ostype: linux\n"
+ "Server: Docker/17.12.0-ce (linux)\n"
+ "Date: Sun, 04 Feb 2018 09:07:13 GMT\n"
+ "Connection: close\n"
+ "Content-Length: 0";
final Response resp = new SocketResponse(null, responseString);
final Map<String, List<String>> headers = resp.headers();
MatcherAssert.assertThat(headers.size(), Matchers.is(8));
headers.forEach((key, value) -> {
MatcherAssert.assertThat(
key, Matchers.not(Matchers.isEmptyOrNullString())
);
MatcherAssert.assertThat(
value.size() > 0, Matchers.is(Boolean.TRUE)
);
});
}

/**
* A SocketResponse can return its headers if there is a body afterwards.
*/
@Test
public void headersWithontent() {
final String responseString = "HTTP/1.1 200 OK\n"
+ "Api-Version: 1.35\n"
+ "Content-Type: application/json\n"
+ "Docker-Experimental: false\n"
+ "Connection: close\n"
+ "Content-Length: 2\n\n"
+ "OK";
final Response resp = new SocketResponse(null, responseString);
final Map<String, List<String>> headers = resp.headers();
MatcherAssert.assertThat(headers.size(), Matchers.is(5));
headers.forEach((key, value) -> {
MatcherAssert.assertThat(
key, Matchers.not(Matchers.isEmptyOrNullString())
);
MatcherAssert.assertThat(
value.size() == 1, Matchers.is(Boolean.TRUE)
);
});
MatcherAssert.assertThat(headers.get(
"Api-Version").get(0), Matchers.equalTo("1.35")
);
MatcherAssert.assertThat(headers.get(
"Content-Type").get(0), Matchers.equalTo("application/json")
);
MatcherAssert.assertThat(headers.get(
"Docker-Experimental").get(0), Matchers.equalTo("false")
);
MatcherAssert.assertThat(headers.get(
"Connection").get(0), Matchers.equalTo("close")
);
MatcherAssert.assertThat(headers.get(
"Content-Length").get(0), Matchers.equalTo("2")
);
}

/**
* A SocketResponse returns an empty Map when there are no headers.
*/
@Test
public void noHeaders() {
final String responseString = "HTTP/1.1 200 OK\n";
final Response resp = new SocketResponse(null, responseString);
final Map<String, List<String>> headers = resp.headers();
MatcherAssert.assertThat(headers.isEmpty(), Matchers.is(Boolean.TRUE));
}
}