1 package com.atlassian.marketplace.client.http;
2
3 import java.io.ByteArrayInputStream;
4 import java.net.URI;
5
6 import com.atlassian.marketplace.client.http.HttpConfiguration.Credentials;
7 import com.atlassian.marketplace.client.impl.CommonsHttpTransport;
8 import com.atlassian.utt.http.TestHttpServer.RequestProperties;
9
10 import com.google.common.collect.ImmutableMap;
11
12 import org.hamcrest.Matcher;
13 import org.hamcrest.Matchers;
14 import org.junit.Test;
15
16 import static com.atlassian.fugue.Option.some;
17 import static com.atlassian.utt.http.TestHttpServer.requests;
18 import static com.atlassian.utt.http.TestHttpServer.status;
19 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.header;
20 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.method;
21 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestBody;
22 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.uri;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.allOf;
25 import static org.hamcrest.Matchers.contains;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.hamcrest.Matchers.not;
28
29 public class CommonsHttpTransportTest extends CommonsHttpTransportTestBase
30 {
31 @Test
32 public void simpleGetRequest() throws Exception
33 {
34 http = makeHttp(baseConfig());
35 resp = http.get(absoluteTestUri());
36 assertThat(server, requests().is(
37 Matchers.<RequestProperties>contains(
38 allOf(
39 method().is(equalTo("GET")),
40 uri().is(equalTo(TEST_URI))
41 )
42 )
43 ));
44 }
45
46 @Test
47 public void simpleGetResponse() throws Exception
48 {
49 http = makeHttp(baseConfig());
50 server.respondIf(
51 allOf(method().is(equalTo("GET")), uri().is(equalTo(TEST_URI))),
52 status(200).body("foo")
53 );
54 resp = http.get(absoluteTestUri());
55 assertThat(resp,
56 allOf(
57 responseStatus().is(equalTo(200)),
58 responseBody().is(equalTo("foo"))
59 )
60 );
61 }
62
63 @Test
64 public void simplePostRequest() throws Exception
65 {
66 http = makeHttp(baseConfig());
67 resp = http.post(absoluteTestUri(),
68 new ByteArrayInputStream(makeContent()),
69 makeContent().length,
70 "application/thing",
71 "application/reply"
72 );
73 assertThat(server, requests().is(
74 contains(
75 allOf(
76 method().is(equalTo("POST")),
77 uri().is(equalTo(TEST_URI)),
78 header("Content-Type").is(contains("application/thing")),
79 header("Accept").is(contains("application/reply")),
80 requestBody().is(equalTo(CONTENT_STRING))
81 )
82 )
83 ));
84 }
85
86 @Test
87 public void simplePostResponse() throws Exception
88 {
89 http = makeHttp(baseConfig());
90 server.respondIf(
91 allOf(method().is(equalTo("POST")), uri().is(equalTo(TEST_URI))),
92 status(200).body("foo")
93 );
94 resp = http.post(absoluteTestUri(),
95 new ByteArrayInputStream(makeContent()),
96 makeContent().length,
97 "application/thing",
98 "application/reply"
99 );
100 assertThat(resp,
101 allOf(
102 responseStatus().is(equalTo(200)),
103 responseBody().is(equalTo("foo"))
104 )
105 );
106 }
107
108 @Test
109 public void postParamsRequest() throws Exception
110 {
111 http = makeHttp(baseConfig());
112 resp = http.postParams(absoluteTestUri(), makeParams());
113 assertThat(server, requests().is(
114 contains(
115 allOf(
116 method().is(equalTo("POST")),
117 uri().is(equalTo(TEST_URI)),
118 hasPostParams()
119 )
120 )
121 ));
122 }
123
124 @Test
125 public void postParamsResponse() throws Exception
126 {
127 http = makeHttp(baseConfig());
128 server.respondIf(
129 allOf(method().is(equalTo("POST")), uri().is(equalTo(TEST_URI))),
130 status(200).body("foo")
131 );
132 resp = http.postParams(absoluteTestUri(), makeParams());
133 assertThat(resp,
134 allOf(
135 responseStatus().is(equalTo(200)),
136 responseBody().is(equalTo("foo"))
137 )
138 );
139 }
140
141 @Test
142 public void simplePutRequest() throws Exception
143 {
144 http = makeHttp(baseConfig());
145 resp = http.put(absoluteTestUri(), makeContent());
146 assertThat(server, requests().is(
147 contains(
148 allOf(
149 method().is(equalTo("PUT")),
150 uri().is(equalTo(TEST_URI)),
151 header("Content-Type").is(contains("application/json; charset=UTF-8")),
152 requestBody().is(equalTo(CONTENT_STRING))
153 )
154 )
155 ));
156 }
157
158 @Test
159 public void simplePutResponse() throws Exception
160 {
161 http = makeHttp(baseConfig());
162 server.respondIf(
163 allOf(method().is(equalTo("PUT")), uri().is(equalTo(TEST_URI))),
164 status(200).body("foo")
165 );
166 resp = http.put(absoluteTestUri(), makeContent());
167 assertThat(resp,
168 allOf(
169 responseStatus().is(equalTo(200)),
170 responseBody().is(equalTo("foo"))
171 )
172 );
173 }
174
175 @Test
176 public void simplePatchRequest() throws Exception
177 {
178 http = makeHttp(baseConfig());
179 resp = http.patch(absoluteTestUri(), makeContent());
180 assertThat(server, requests().is(
181 contains(
182 allOf(
183 method().is(equalTo("PATCH")),
184 uri().is(equalTo(TEST_URI)),
185 header("Content-Type").is(contains("application/json-patch+json; charset=UTF-8")),
186 requestBody().is(equalTo(CONTENT_STRING))
187 )
188 )
189 ));
190 }
191
192 @Test
193 public void simplePatchResponse() throws Exception
194 {
195 http = makeHttp(baseConfig());
196 server.respondIf(
197 allOf(method().is(equalTo("PATCH")), uri().is(equalTo(TEST_URI))),
198 status(200).body("foo")
199 );
200 resp = http.patch(absoluteTestUri(), makeContent());
201 assertThat(resp,
202 allOf(
203 responseStatus().is(equalTo(200)),
204 responseBody().is(equalTo("foo"))
205 )
206 );
207 }
208
209 @Test
210 public void simpleDeleteRequest() throws Exception
211 {
212 http = makeHttp(baseConfig());
213 resp = http.delete(absoluteTestUri());
214 assertThat(server, requests().is(
215 contains(
216 allOf(
217 method().is(equalTo("DELETE")),
218 uri().is(equalTo(TEST_URI))
219 )
220 )
221 ));
222 }
223
224 @Test
225 public void simpleDeleteResponse() throws Exception
226 {
227 http = makeHttp(baseConfig());
228 server.respondIf(
229 allOf(method().is(equalTo("DELETE")), uri().is(equalTo(TEST_URI))),
230 status(204)
231 );
232 resp = http.delete(absoluteTestUri());
233 assertThat(resp,
234 responseStatus().is(equalTo(204))
235 );
236 }
237
238 @Test
239 public void requestDecoratorCanAddHeadersForGet() throws Exception
240 {
241 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
242 resp = http.get(absoluteTestUri());
243
244 assertThat(server, requests().is(
245 Matchers.<RequestProperties>contains(hasRequestDecoratorHeaders())
246 ));
247 }
248
249 @Test
250 public void requestDecoratorCanAddHeadersForSimplePost() throws Exception
251 {
252 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
253 resp = http.post(absoluteTestUri(),
254 new ByteArrayInputStream(makeContent()),
255 makeContent().length,
256 "application/thing",
257 "application/reply"
258 );
259
260 assertThat(server, requests().is(
261 Matchers.<RequestProperties>contains(hasRequestDecoratorHeaders())
262 ));
263 }
264
265 @Test
266 public void requestDecoratorCanAddHeadersForPostParams() throws Exception
267 {
268 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
269 resp = http.postParams(absoluteTestUri(), makeParams());
270
271 assertThat(server, requests().is(
272 Matchers.<RequestProperties>contains(hasRequestDecoratorHeaders())
273 ));
274 }
275
276 @Test
277 public void requestDecoratorCanAddHeadersForPut() throws Exception
278 {
279 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
280 resp = http.put(absoluteTestUri(), makeContent());
281
282 assertThat(server, requests().is(
283 Matchers.<RequestProperties>contains(hasRequestDecoratorHeaders())
284 ));
285 }
286
287 @Test
288 public void requestDecoratorCanAddHeadersForDelete() throws Exception
289 {
290 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
291 resp = http.delete(absoluteTestUri());
292
293 assertThat(server, requests().is(
294 Matchers.<RequestProperties>contains(hasRequestDecoratorHeaders())
295 ));
296 }
297
298 @SuppressWarnings("unchecked")
299 @Test
300 public void canAddAdditionalRequestDecoratorInDerivedHttpOperations() throws Exception
301 {
302 http = makeHttp(withSimpleRequestDecorator(baseConfig()));
303 HttpTransport http2 = http.withRequestDecorator(simpleRequestDecorator(ImmutableMap.of("bar", "baz")));
304
305 Matcher<RequestProperties> fooHeader = header("foo").is(contains("bar"));
306 Matcher<RequestProperties> barHeader = header("bar").is(contains("baz"));
307
308 resp = http.get(absoluteTestUri());
309 SimpleHttpResponse resp2 = http2.get(absoluteTestUri());
310
311 try
312 {
313 assertThat(server, requests().is(
314 Matchers.<RequestProperties>contains(
315 allOf(fooHeader, not(barHeader)),
316 allOf(fooHeader, barHeader)
317 )));
318 }
319 finally
320 {
321 resp2.close();
322 }
323 }
324
325 @Test
326 public void authenticatedRequestUsesPreemptiveBasicAuth() throws Exception
327 {
328 Credentials creds = new Credentials("user", "pass");
329 http = makeHttp(baseConfig().credentials(some(creds)));
330 resp = http.get(absoluteTestUri());
331 assertThat(server, requests().is(
332 Matchers.<RequestProperties>contains(
333 allOf(
334 method().is(equalTo("GET")),
335 uri().is(equalTo(TEST_URI)),
336 header("Authorization").is(contains("Basic dXNlcjpwYXNz"))
337 )
338 )
339 ));
340 }
341
342 private CommonsHttpTransport makeHttp(HttpConfiguration.Builder builder)
343 {
344 return new CommonsHttpTransport(builder.build(), serverUri);
345 }
346
347 private URI absoluteTestUri()
348 {
349 return serverUri.resolve(TEST_URI);
350 }
351 }