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.BufferedReader;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 | import java.util.Collections;
|
---|
15 |
|
---|
16 | import javax.json.JsonObject;
|
---|
17 | import javax.json.JsonReader;
|
---|
18 | import javax.json.spi.JsonProvider;
|
---|
19 |
|
---|
20 | import org.junit.Assert;
|
---|
21 | import org.junit.Before;
|
---|
22 | import org.junit.BeforeClass;
|
---|
23 | import org.junit.Test;
|
---|
24 | import org.openstreetmap.josm.JOSMFixture;
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.TestUtils;
|
---|
27 | import org.openstreetmap.josm.data.Version;
|
---|
28 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Tests the {@link HttpClient} using the webservice <a href="https://httpbin.org/">https://httpbin.org/</a>.
|
---|
32 | */
|
---|
33 | public class HttpClientTest {
|
---|
34 |
|
---|
35 | private ProgressMonitor progress;
|
---|
36 |
|
---|
37 | @BeforeClass
|
---|
38 | public static void setUpBeforeClass() {
|
---|
39 | JOSMFixture.createFunctionalTestFixture().init();
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Before
|
---|
43 | public void setUp() {
|
---|
44 | progress = TestUtils.newTestProgressMonitor();
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void testConstructorGetterSetter() throws IOException {
|
---|
49 | final HttpClient client = HttpClient.create(new URL("https://httpbin.org/"));
|
---|
50 | assertThat(client.getURL(), is(new URL("https://httpbin.org/")));
|
---|
51 | assertThat(client.getRequestMethod(), is("GET"));
|
---|
52 | assertThat(client.getRequestHeader("Accept"), nullValue());
|
---|
53 | client.setAccept("text/html");
|
---|
54 | assertThat(client.getRequestHeader("Accept"), is("text/html"));
|
---|
55 | assertThat(client.getRequestHeader("ACCEPT"), is("text/html"));
|
---|
56 | client.setHeaders(Collections.singletonMap("foo", "bar"));
|
---|
57 | assertThat(client.getRequestHeader("foo"), is("bar"));
|
---|
58 | client.setHeaders(Collections.singletonMap("foo", "baz"));
|
---|
59 | assertThat(client.getRequestHeader("foo"), is("baz"));
|
---|
60 | client.setHeaders(Collections.singletonMap("foo", (String) null));
|
---|
61 | assertThat(client.getRequestHeader("foo"), nullValue());
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testGet() throws IOException {
|
---|
66 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect(progress);
|
---|
67 | assertThat(response.getRequestMethod(), is("GET"));
|
---|
68 | assertThat(response.getResponseCode(), is(200));
|
---|
69 | assertThat(response.getResponseMessage(), is("OK"));
|
---|
70 | assertThat(response.getContentType(), is("application/json"));
|
---|
71 | assertThat(response.getHeaderField("Content-Type"), is("application/json"));
|
---|
72 | assertThat(response.getHeaderField("Content-TYPE"), is("application/json"));
|
---|
73 | assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json")));
|
---|
74 | assertThat(response.getHeaderFields().get("Content-TYPE"), is(Collections.singletonList("application/json")));
|
---|
75 | try (final InputStream in = response.getContent();
|
---|
76 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
77 | final JsonObject root = json.readObject();
|
---|
78 | assertThat(root.getJsonObject("args").getString("foo"), is("bar"));
|
---|
79 | assertThat(root.getString("url"), is("https://httpbin.org/get?foo=bar"));
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void testUserAgent() throws IOException {
|
---|
85 | try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect(progress).getContent();
|
---|
86 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
87 | assertThat(json.readObject().getString("user-agent"), is(Version.getInstance().getFullAgentString()));
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Test
|
---|
92 | public void testFetchUtf8Content() throws IOException {
|
---|
93 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect(progress);
|
---|
94 | assertThat(response.getResponseCode(), is(200));
|
---|
95 | final String content = response.fetchContent();
|
---|
96 | assertThat(content, containsString("UTF-8 encoded sample plain-text file"));
|
---|
97 | assertThat(content, containsString("\u2200x\u2208\u211d:"));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void testPost() throws IOException {
|
---|
102 | final String text = "Hello World!\nGeetings from JOSM, the Java OpenStreetMap Editor";
|
---|
103 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST")
|
---|
104 | .setHeader("Content-Type", "text/plain")
|
---|
105 | .setRequestBody(text.getBytes(StandardCharsets.UTF_8))
|
---|
106 | .setFinishOnCloseOutput(false) // to fix #12583, not sure if it's the best way to do it
|
---|
107 | .connect(progress);
|
---|
108 | assertThat(response.getResponseCode(), is(200));
|
---|
109 | try (final InputStream in = response.getContent();
|
---|
110 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
111 | assertThat(json.readObject().getString("data"), is(text));
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | public void testPostZero() throws IOException {
|
---|
117 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST")
|
---|
118 | .setHeader("Content-Type", "text/plain")
|
---|
119 | .setRequestBody("".getBytes(StandardCharsets.UTF_8))
|
---|
120 | .setFinishOnCloseOutput(false) // to fix #12583, not sure if it's the best way to do it
|
---|
121 | .connect(progress);
|
---|
122 | assertThat(response.getResponseCode(), is(200));
|
---|
123 | try (final InputStream in = response.getContent();
|
---|
124 | final JsonReader json = JsonProvider.provider().createReader(in)) {
|
---|
125 | assertThat(json.readObject().getString("data"), is(""));
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void testRelativeRedirects() throws IOException {
|
---|
131 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect(progress);
|
---|
132 | assertThat(response.getResponseCode(), is(200));
|
---|
133 | assertThat(response.getContentLength() > 100, is(true));
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Test
|
---|
137 | public void testAbsoluteRedirects() throws IOException {
|
---|
138 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect(progress);
|
---|
139 | assertThat(response.getResponseCode(), is(200));
|
---|
140 | assertThat(response.getContentLength() > 100, is(true));
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Test(expected = IOException.class)
|
---|
144 | public void testTooMuchRedirects() throws IOException {
|
---|
145 | HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect(progress);
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Test
|
---|
149 | public void test418() throws IOException {
|
---|
150 | // https://tools.ietf.org/html/rfc2324
|
---|
151 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect(progress);
|
---|
152 | assertThat(response.getResponseCode(), is(418));
|
---|
153 | assertThat(response.getResponseMessage(), is("I'M A TEAPOT"));
|
---|
154 | final String content = response.fetchContent();
|
---|
155 | assertThat(content, containsString("-=[ teapot ]=-"));
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Test
|
---|
159 | public void testRequestInTime() throws IOException {
|
---|
160 | final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect(progress);
|
---|
161 | assertThat(response.getResponseCode(), is(200));
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Test(expected = IOException.class)
|
---|
165 | public void testTakesTooLong() throws IOException {
|
---|
166 | HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect(progress);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Test of {@link HttpClient.Response#uncompress} method with Gzip compression.
|
---|
171 | * @throws IOException if any I/O error occurs
|
---|
172 | */
|
---|
173 | @Test
|
---|
174 | public void testOpenUrlGzip() throws IOException {
|
---|
175 | Main.initApplicationPreferences();
|
---|
176 | final URL url = new URL("https://www.openstreetmap.org/trace/1613906/data");
|
---|
177 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
|
---|
178 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Test of {@link HttpClient.Response#uncompress} method with Bzip compression.
|
---|
184 | * @throws IOException if any I/O error occurs
|
---|
185 | */
|
---|
186 | @Test
|
---|
187 | public void testOpenUrlBzip() throws IOException {
|
---|
188 | Main.initApplicationPreferences();
|
---|
189 | final URL url = new URL("https://www.openstreetmap.org/trace/785544/data");
|
---|
190 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
|
---|
191 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Test of {@link HttpClient.Response#uncompress} method with Bzip compression.
|
---|
197 | * @throws IOException if any I/O error occurs
|
---|
198 | */
|
---|
199 | @Test
|
---|
200 | public void testTicket9660() throws IOException {
|
---|
201 | Main.initApplicationPreferences();
|
---|
202 | final URL url = new URL("http://www.openstreetmap.org/trace/1350010/data");
|
---|
203 | try (BufferedReader x = HttpClient.create(url).connect()
|
---|
204 | .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) {
|
---|
205 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|