Skip to content

Commit

Permalink
Refactoring a test, reducing code duplication, no logical changes
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Mar 3, 2018
1 parent 2c9ffd6 commit 4561d85
Showing 1 changed file with 21 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public void requestShouldIncludeJsonWireProtocolCapabilities() throws IOExceptio

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> json = new Gson()
.fromJson(request.getContentString(), new TypeToken<Map<String, Object>>(){}.getType());
Map<String, Object> json = getRequestPayloadAsMap(client);

assertEquals(ImmutableMap.of(), json.get("desiredCapabilities"));
}
Expand All @@ -88,9 +86,7 @@ public void requestShouldIncludeOlderGeckoDriverCapabilities() throws IOExceptio

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> json = new Gson()
.fromJson(request.getContentString(), new TypeToken<Map<String, Object>>(){}.getType());
Map<String, Object> json = getRequestPayloadAsMap(client);
Map<String, Object> capabilities = (Map<String, Object>) json.get("capabilities");

assertEquals(ImmutableMap.of(), capabilities.get("desiredCapabilities"));
Expand All @@ -109,9 +105,7 @@ public void requestShouldIncludeSpecCompliantW3CCapabilities() throws IOExceptio

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> json = new Gson()
.fromJson(request.getContentString(), new TypeToken<Map<String, Object>>(){}.getType());
Map<String, Object> json = getRequestPayloadAsMap(client);

List<Map<String, Object>> caps = mergeW3C(json);

Expand Down Expand Up @@ -180,10 +174,7 @@ public void shouldAddBothGeckoDriverAndW3CCapabilitiesToRootCapabilitiesProperty

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

Object rawCaps = handshakeRequest.get("capabilities");
assertTrue(rawCaps instanceof Map);
Expand Down Expand Up @@ -215,11 +206,7 @@ public void shouldNotIncludeNonProtocolExtensionKeys() throws IOException {

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();

Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

Object rawCaps = handshakeRequest.get("capabilities");
assertTrue(rawCaps instanceof Map);
Expand Down Expand Up @@ -258,10 +245,7 @@ public void firstMatchSeparatesCapsForDifferentBrowsers() throws IOException {

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

List<Map<String, Object>> capabilities = mergeW3C(handshakeRequest);

Expand All @@ -288,10 +272,7 @@ public void doesNotCreateFirstMatchForNonW3CCaps() throws IOException {

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

List<Map<String, Object>> w3c = mergeW3C(handshakeRequest);

Expand Down Expand Up @@ -320,10 +301,7 @@ public void shouldLowerCaseProxyTypeForW3CRequest() throws IOException {

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

mergeW3C(handshakeRequest).forEach(always -> {
Map<String, ?> seenProxy = (Map<String, ?>) always.get("proxy");
Expand Down Expand Up @@ -353,10 +331,7 @@ public void shouldNotIncludeMappingOfANYPlatform() throws IOException {

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> handshakeRequest = getRequestPayloadAsMap(client);

mergeW3C(handshakeRequest)
.forEach(capabilities -> {
Expand Down Expand Up @@ -389,33 +364,33 @@ private List<Map<String, Object>> mergeW3C(Map<String, Object> caps) {
return allCaps;
}

private Map<String, Object> getRequestPayloadAsMap(RecordingHttpClient client) {
return new Gson().fromJson(
client.getRequestPayload(), new TypeToken<Map<String, Object>>(){}.getType());
}

class RecordingHttpClient implements HttpClient {

private final HttpResponse response;
private HttpRequest request;
private String payload;

public RecordingHttpClient(HttpResponse response) {
RecordingHttpClient(HttpResponse response) {
this.response = response;
}

@Override
public HttpResponse execute(HttpRequest request) throws IOException {
return execute(request, true);
}

private HttpResponse execute(HttpRequest request, boolean followRedirects) throws IOException {
this.request = request;
request.getContentString();
public HttpResponse execute(HttpRequest request) {
payload = request.getContentString();
return response;
}

@Override
public void close() throws IOException {
public void close() {
// Does nothing
}

public HttpRequest getRequest() {
return request;
String getRequestPayload() {
return payload;
}
}
}

0 comments on commit 4561d85

Please sign in to comment.