1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.containsString;
|
---|
5 | import static org.hamcrest.CoreMatchers.is;
|
---|
6 | import static org.hamcrest.CoreMatchers.nullValue;
|
---|
7 | import static org.junit.Assert.assertThat;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.util.Collections;
|
---|
14 |
|
---|
15 | import javax.json.JsonObject;
|
---|
16 | import javax.json.JsonReader;
|
---|
17 | import javax.json.spi.JsonProvider;
|
---|
18 |
|
---|
19 | import org.junit.BeforeClass;
|
---|
20 | import org.junit.Test;
|
---|
21 | import org.openstreetmap.josm.JOSMFixture;
|
---|
22 | import org.openstreetmap.josm.data.Version;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Tests the {@link HttpClient} using the webservice <a href="https://httpbin.org/">https://httpbin.org/</a>.
|
---|
26 | */
|
---|
27 | public class HttpClientTest {
|
---|
28 |
|
---|
29 | @BeforeClass
|
---|
30 | public static void setUpBeforeClass() {
|
---|
31 | JOSMFixture.createFunctionalTestFixture().init();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Test
|
---|
35 | public void testConstructorGetterSetter() throws Exception {
|
---|
36 | final HttpClient client = HttpClient.create(new URL("https://httpbin.org/"));
|
---|
37 | assertThat(client.getURL(), is(new URL("https://httpbin.org/")));
|
---|
38 | assertThat(client.getRequestMethod(), is("GET"));
|
---|
39 | assertThat(client.getRequestHeader("Accept"), nullValue());
|
---|
40 | client.setAccept("text/html");
|
---|
41 | assertThat(client.getRequestHeader("Accept"), is("text/html"));
|
---|
42 | assertThat(client.getRequestHeader("ACCEPT"), is("text/html"));
|
---|
43 | client.setHeaders(Collections.singletonMap("foo", "bar"));
|
---|
44 | assertThat(client.getRequestHeader("foo"), is("bar"));
|
---|
45 | client.setHeaders(Collections.singletonMap("foo", "baz"));
|
---|
46 | assertThat(client.getRequestHeader("foo"), is("baz"));
|
---|
47 | client.setHeaders(Collections.singletonMap("foo", (String) null));
|
---|
48 | assertThat(client.getRequestHeader("foo"), nullValue());
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Test
|
---|
52 | public void testGet() throws Exception {
|
---|
53 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect();
|
---|
54 | assertThat(response.getRequestMethod(), is("GET"));
|
---|
55 | assertThat(response.getResponseCode(), is(200));
|
---|
56 | assertThat(response.getResponseMessage(), is("OK"));
|
---|
57 | assertThat(response.getContentType(), is("application/json"));
|
---|
58 | assertThat(response.getHeaderField("Content-Type"), is("application/json"));
|
---|
59 | assertThat(response.getHeaderField("Content-TYPE"), is("application/json"));
|
---|
60 | assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json")));
|
---|
61 | assertThat(response.getHeaderFields().get("Content-TYPE"), nullValue());
|
---|
62 | try (final InputStream in = response.getContent();
|
---|
63 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
64 | final JsonObject root = json.readObject();
|
---|
65 | assertThat(root.getJsonObject("args").getString("foo"), is("bar"));
|
---|
66 | assertThat(root.getString("url"), is("https://httpbin.org/get?foo=bar"));
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | public void testUserAgent() throws Exception {
|
---|
72 | try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect().getContent();
|
---|
73 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
74 | assertThat(json.readObject().getString("user-agent"), is(Version.getInstance().getFullAgentString()));
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void testFetchUtf8Content() throws Exception {
|
---|
80 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect();
|
---|
81 | assertThat(response.getResponseCode(), is(200));
|
---|
82 | final String content = response.fetchContent();
|
---|
83 | assertThat(content, containsString("UTF-8 encoded sample plain-text file"));
|
---|
84 | assertThat(content, containsString("\u2200x\u2208\u211d:"));
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void testPost() throws Exception {
|
---|
89 | final String text = "Hello World!\nGeetings from JOSM, the Java OpenStreetMap Editor";
|
---|
90 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST")
|
---|
91 | .setHeader("Content-Type", "text/plain")
|
---|
92 | .setRequestBody(text.getBytes(StandardCharsets.UTF_8))
|
---|
93 | .connect();
|
---|
94 | assertThat(response.getResponseCode(), is(200));
|
---|
95 | try (final InputStream in = response.getContent();
|
---|
96 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
97 | assertThat(json.readObject().getString("data"), is(text));
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Test
|
---|
102 | public void testRelativeRedirects() throws Exception {
|
---|
103 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect();
|
---|
104 | assertThat(response.getResponseCode(), is(200));
|
---|
105 | assertThat(response.getContentLength() > 100, is(true));
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void testAbsoluteRedirects() throws Exception {
|
---|
110 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect();
|
---|
111 | assertThat(response.getResponseCode(), is(200));
|
---|
112 | assertThat(response.getContentLength() > 100, is(true));
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test(expected = IOException.class)
|
---|
116 | public void testTooMuchRedirects() throws Exception {
|
---|
117 | HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect();
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Test
|
---|
121 | public void test418() throws Exception {
|
---|
122 | // https://tools.ietf.org/html/rfc2324
|
---|
123 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect();
|
---|
124 | assertThat(response.getResponseCode(), is(418));
|
---|
125 | assertThat(response.getResponseMessage(), is("I'M A TEAPOT"));
|
---|
126 | final String content = response.fetchContent();
|
---|
127 | assertThat(content, containsString("-=[ teapot ]=-"));
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void testRequestInTime() throws Exception {
|
---|
132 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect();
|
---|
133 | assertThat(response.getResponseCode(), is(200));
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Test(expected = IOException.class)
|
---|
137 | public void testTakesTooLong() throws Exception {
|
---|
138 | HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect();
|
---|
139 | }
|
---|
140 | }
|
---|