forked from snowplow/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpClientAdapterTest.java
More file actions
148 lines (123 loc) · 5.22 KB
/
HttpClientAdapterTest.java
File metadata and controls
148 lines (123 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.snowplowanalytics.snowplow.tracker;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.snowplowanalytics.snowplow.tracker.http.ApacheHttpClientAdapter;
import com.snowplowanalytics.snowplow.tracker.http.HttpClientAdapter;
import com.snowplowanalytics.snowplow.tracker.http.OkHttpClientAdapter;
import com.snowplowanalytics.snowplow.tracker.payload.SchemaPayload;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class HttpClientAdapterTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private final MockWebServer mockWebServer;
private HttpClientAdapter adapter;
interface HttpClientAdapterProvider {
HttpClientAdapter provide(String uri);
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{new HttpClientAdapterProvider() {
@Override
public HttpClientAdapter provide(String url) {
return new ApacheHttpClientAdapter(url, HttpClients.createDefault(), new ObjectMapper());
}
}},
{new HttpClientAdapterProvider() {
@Override
public HttpClientAdapter provide(String url) {
OkHttpClient httpClient = new OkHttpClient();
httpClient.setConnectTimeout(1, TimeUnit.SECONDS);
httpClient.setReadTimeout(1, TimeUnit.SECONDS);
httpClient.setWriteTimeout(1, TimeUnit.SECONDS);
return new OkHttpClientAdapter(url, httpClient, new ObjectMapper());
}
}
}
});
}
public HttpClientAdapterTest(HttpClientAdapterProvider httpClientAdapterProvider) throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.play();
adapter = httpClientAdapterProvider.provide(mockWebServer.getUrl("").toString());
}
@Test
public void get_withSuccessfulStatusCode_isOk() throws Exception {
// Given
mockWebServer.enqueue(
new MockResponse()
.setResponseCode(200)
);
// When
adapter.get(ImmutableMap.<String, Object>of("foo", "bar"));
// Then
assertEquals(1, mockWebServer.getRequestCount());
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals("/i?foo=bar", recordedRequest.getPath());
assertEquals("GET", recordedRequest.getMethod());
}
@Test
public void post_withSuccessfulStatusCode_isNotOk() throws InterruptedException {
// Given
mockWebServer.enqueue(
new MockResponse()
.setResponseCode(200)
);
// When
adapter.post(new SchemaPayload().setData(ImmutableMap.of("foo", "bar")).setSchema("schema"));
// Then
assertEquals(1, mockWebServer.getRequestCount());
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals("/com.snowplowanalytics.snowplow/tp2", recordedRequest.getPath());
assertEquals("{\"data\":{\"foo\":\"bar\"},\"schema\":\"schema\"}", recordedRequest.getUtf8Body());
assertEquals("POST", recordedRequest.getMethod());
assertEquals("application/json; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
@Test
public void post_withNotSuccessfulStatusCode_throwException() {
// Given
mockWebServer.enqueue(
new MockResponse()
.setResponseCode(400)
);
expectedException.expectMessage("Failed to send event using POST. Got http response 400");
// When
adapter.post(new SchemaPayload().setData(ImmutableMap.of("foo", "bar")).setSchema("schema"));
}
@Test
public void testPostWithNullArgument() throws Exception {
expectedException.expect(NullPointerException.class);
adapter.post(null);
}
@Test
public void testGetWithNullArgument() throws Exception {
expectedException.expect(NullPointerException.class);
adapter.get(null);
}
@Test
public void testPostWithEmptySchemaPayload() throws Exception {
expectedException.expect(IllegalArgumentException.class);
adapter.post(new SchemaPayload());
}
@Test
public void testGetWithEmptySchemaPayload() throws Exception {
expectedException.expect(IllegalArgumentException.class);
adapter.get(new HashMap<String, Object>());
}
}