forked from snowplow/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientAdapterTest.java
More file actions
140 lines (120 loc) · 5.07 KB
/
HttpClientAdapterTest.java
File metadata and controls
140 lines (120 loc) · 5.07 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
/*
* Copyright (c) 2015 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.tracker.http;
// Java
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
// Google
import com.google.common.collect.ImmutableMap;
// SquareUp
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
// Apache
import org.apache.http.impl.client.HttpClients;
// JUnit
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
// This library
import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.payload.TrackerPayload;
@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 ApacheHttpClientAdapter.builder()
.url(url)
.httpClient(HttpClients.createDefault())
.build();
}
}},
{new HttpClientAdapterProvider() {
@Override
public HttpClientAdapter provide(String url) {
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS)
.build();
return OkHttpClientAdapter.builder()
.url(url)
.httpClient(httpClient)
.build();
}
}
}
});
}
public HttpClientAdapterTest(HttpClientAdapterProvider httpClientAdapterProvider) throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
adapter = httpClientAdapterProvider.provide(mockWebServer.url("/").toString());
}
@Test
public void get_withSuccessfulStatusCode_isOk() throws Exception {
// Given
mockWebServer.enqueue(new MockResponse().setResponseCode(200));
// When
TrackerPayload data = new TrackerPayload();
data.add("foo", "bar");
data.add("space", "b a r");
adapter.get(data);
// Then
assertEquals(1, mockWebServer.getRequestCount());
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals("/i?foo=bar&space=b%20a%20r", recordedRequest.getPath());
assertEquals("GET", recordedRequest.getMethod());
}
@Test
public void post_withSuccessfulStatusCode_isOk() throws InterruptedException {
// Given
mockWebServer.enqueue(new MockResponse().setResponseCode(200));
// When
adapter.post(new SelfDescribingJson("schema", ImmutableMap.of("foo", "bar")));
// Then
assertEquals(1, mockWebServer.getRequestCount());
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals("/com.snowplowanalytics.snowplow/tp2", recordedRequest.getPath());
assertEquals("{\"schema\":\"schema\",\"data\":{\"foo\":\"bar\"}}", recordedRequest.getUtf8Body());
assertEquals("POST", recordedRequest.getMethod());
assertEquals("application/json; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
@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);
}
}