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
152 lines (125 loc) · 5.78 KB
/
HttpClientAdapterTest.java
File metadata and controls
152 lines (125 loc) · 5.78 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
149
150
151
152
/*
* Copyright (c) 2014-2022 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;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.apache.http.impl.client.HttpClients;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.payload.TrackerPayload;
@RunWith(Parameterized.class)
public class HttpClientAdapterTest {
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 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 new OkHttpClientAdapter(url, httpClient);
}
}
}
});
}
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);
String eventId = data.getEventId();
String dtm = Long.toString(data.getDeviceCreatedTimestamp());
// Then
assertEquals(1, mockWebServer.getRequestCount());
RecordedRequest recordedRequest = mockWebServer.takeRequest();
String expectedString = "/i?eid=" + eventId + "&dtm=" + dtm + "&foo=bar&space=b%20a%20r";
assertEquals(expectedString, 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", Collections.singletonMap("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.getBody().readUtf8());
assertEquals("POST", recordedRequest.getMethod());
assertEquals("application/json; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
@Test
public void testPostWithNullArgument() {
Assert.assertThrows(NullPointerException.class, () -> adapter.post(null));
}
@Test
public void testGetWithNullArgument() {
Assert.assertThrows(NullPointerException.class, () -> adapter.get(null));
}
@Test
public void testRequestWithCookies() throws IOException, InterruptedException {
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS)
.cookieJar(new CollectorCookieJar())
.build();
adapter = new OkHttpClientAdapter(mockWebServer.url("/").toString(), httpClient);
mockWebServer.enqueue(new MockResponse().addHeader("Set-Cookie", "sp=test"));
SelfDescribingJson payload = new SelfDescribingJson("schema", Collections.singletonMap("foo", "bar"));
adapter.post(payload);
adapter.post(payload);
assertEquals(2, mockWebServer.getRequestCount());
mockWebServer.takeRequest();
RecordedRequest recordedRequest2 = mockWebServer.takeRequest();
assertEquals("sp=test", recordedRequest2.getHeader("Cookie"));
mockWebServer.shutdown();
}
}