Changeset 17152 in josm
- Timestamp:
- 2020-10-10T23:24:23+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java
r16837 r17152 36 36 37 37 /** 38 * Tests the {@link HttpClient} using the webservice <a href="https://httpbin go.org/">https://httpbingo.org/</a>.38 * Tests the {@link HttpClient} using the webservice <a href="https://httpbin.org/">https://httpbin.org/</a>. 39 39 */ 40 40 public class HttpClientTest { … … 83 83 @Test 84 84 public void testConstructorGetterSetter() throws IOException { 85 final HttpClient client = HttpClient.create(new URL("https://httpbin go.org/"));86 assertThat(client.getURL(), is(new URL("https://httpbin go.org/")));85 final HttpClient client = HttpClient.create(new URL("https://httpbin.org/")); 86 assertThat(client.getURL(), is(new URL("https://httpbin.org/"))); 87 87 assertThat(client.getRequestMethod(), is("GET")); 88 88 assertThat(client.getRequestHeader("Accept"), is("*/*")); … … 104 104 @Test 105 105 public void testGet() throws IOException { 106 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/get?foo=bar")).connect(progress);106 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect(progress); 107 107 assertThat(response.getRequestMethod(), is("GET")); 108 108 assertThat(response.getResponseCode(), is(200)); 109 109 assertThat(response.getResponseMessage(), equalToIgnoringCase("OK")); 110 assertThat(response.getContentType(), is("application/json ; encoding=utf-8"));111 assertThat(response.getHeaderField("Content-Type"), is("application/json ; encoding=utf-8"));112 assertThat(response.getHeaderField("Content-TYPE"), is("application/json ; encoding=utf-8"));113 assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json ; encoding=utf-8")));114 assertThat(response.getHeaderFields().get("Content-TYPE"), is(Collections.singletonList("application/json ; encoding=utf-8")));110 assertThat(response.getContentType(), is("application/json")); 111 assertThat(response.getHeaderField("Content-Type"), is("application/json")); 112 assertThat(response.getHeaderField("Content-TYPE"), is("application/json")); 113 assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json"))); 114 assertThat(response.getHeaderFields().get("Content-TYPE"), is(Collections.singletonList("application/json"))); 115 115 try (InputStream in = response.getContent(); 116 116 JsonReader json = JsonProvider.provider().createReader(in)) { 117 117 final JsonObject root = json.readObject(); 118 assertThat(root.getJsonObject("args").get JsonArray("foo").getString(0), is("bar"));119 assertThat(root.getString("url"), is("https://httpbin go.org/get?foo=bar"));118 assertThat(root.getJsonObject("args").getString("foo"), is("bar")); 119 assertThat(root.getString("url"), is("https://httpbin.org/get?foo=bar")); 120 120 assertThat(root.getJsonObject("headers").get("Cache-Control"), nullValue()); 121 121 assertThat(root.getJsonObject("headers").get("Pragma"), nullValue()); … … 129 129 @Test 130 130 public void testHeaders() throws IOException { 131 try (InputStream in = HttpClient.create(new URL("https://httpbin go.org/headers")).connect(progress).getContent();131 try (InputStream in = HttpClient.create(new URL("https://httpbin.org/headers")).connect(progress).getContent(); 132 132 JsonReader json = JsonProvider.provider().createReader(in)) { 133 133 final JsonObject headers = json.readObject().getJsonObject("headers"); 134 assertThat(headers.get JsonArray("Accept").getString(0), is("*/*"));135 assertThat(headers.get JsonArray("Accept-Encoding").getString(0), is("gzip, deflate"));136 assertThat(headers.get JsonArray("User-Agent").getString(0), is(Version.getInstance().getFullAgentString()));134 assertThat(headers.getString("Accept"), is("*/*")); 135 assertThat(headers.getString("Accept-Encoding"), is("gzip, deflate")); 136 assertThat(headers.getString("User-Agent"), is(Version.getInstance().getFullAgentString())); 137 137 } 138 138 } … … 144 144 @Test 145 145 public void testUserAgent() throws IOException { 146 try (InputStream in = HttpClient.create(new URL("https://httpbin go.org/user-agent")).connect(progress).getContent();146 try (InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect(progress).getContent(); 147 147 JsonReader json = JsonProvider.provider().createReader(in)) { 148 148 assertThat(json.readObject().getString("user-agent"), is(Version.getInstance().getFullAgentString())); … … 156 156 @Test 157 157 public void testFetchUtf8Content() throws IOException { 158 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/encoding/utf8")).connect(progress);158 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect(progress); 159 159 assertThat(response.getResponseCode(), is(200)); 160 160 final String content = response.fetchContent(); … … 170 170 public void testPost() throws IOException { 171 171 final String text = "Hello World!\nGeetings from JOSM, the Java OpenStreetMap Editor"; 172 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/post"), "POST")172 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST") 173 173 .setHeader("Content-Type", "text/plain") 174 174 .setRequestBody(text.getBytes(StandardCharsets.UTF_8)) … … 184 184 @Test 185 185 public void testPostZero() throws IOException { 186 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/post"), "POST")186 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST") 187 187 .setHeader("Content-Type", "text/plain") 188 188 .setRequestBody("".getBytes(StandardCharsets.UTF_8)) … … 196 196 } 197 197 198 @Test198 /*@Test 199 199 public void testRelativeRedirects() throws IOException { 200 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/relative-redirect/3")).connect(progress);200 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/3")).connect(progress); 201 201 assertThat(response.getResponseCode(), is(200)); 202 202 assertThat(response.getContentLength() > 100, is(true)); 203 } 204 205 @Test203 }*/ 204 205 /*@Test 206 206 public void testAbsoluteRedirects() throws IOException { 207 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/absolute-redirect/3")).connect(progress);207 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/3")).connect(progress); 208 208 assertThat(response.getResponseCode(), is(200)); 209 209 assertThat(response.getContentLength() > 100, is(true)); 210 } 210 }*/ 211 211 212 212 /** … … 214 214 * @throws IOException if an I/O error occurs 215 215 */ 216 @Test(expected = IOException.class)216 /*@Test(expected = IOException.class) 217 217 public void testTooMuchRedirects() throws IOException { 218 HttpClient.create(new URL("https://httpbin go.org/redirect/3")).setMaxRedirects(2).connect(progress);219 } 218 HttpClient.create(new URL("https://httpbin.org/redirect/3")).setMaxRedirects(2).connect(progress); 219 }*/ 220 220 221 221 /** … … 226 226 public void testHttp418() throws IOException { 227 227 // https://tools.ietf.org/html/rfc2324 228 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/418")).connect(progress);228 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect(progress); 229 229 assertThat(response.getResponseCode(), is(418)); 230 assertThat(response.getHeaderField("X-More-Info"), is("http://tools.ietf.org/html/rfc2324")); 231 final String content = response.fetchContent(); 232 assertThat(content, containsString("I'm a teapot!")); 233 assertThat(captured.getMessage(), containsString("I'm a teapot!")); 230 final String content = response.fetchContent(); 231 assertThat(content, containsString("-=[ teapot ]=-")); 232 assertThat(captured.getMessage(), containsString("-=[ teapot ]=-")); 234 233 assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG)); 235 234 } … … 242 241 public void testHttp401() throws IOException { 243 242 // https://tools.ietf.org/html/rfc2324 244 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/401")).connect(progress);243 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/401")).connect(progress); 245 244 assertThat(response.getResponseCode(), is(401)); 246 245 assertThat(response.getResponseMessage(), equalToIgnoringCase("UNAUTHORIZED")); … … 258 257 public void testHttp402() throws IOException { 259 258 // https://tools.ietf.org/html/rfc2324 260 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/402")).connect(progress);259 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/402")).connect(progress); 261 260 assertThat(response.getResponseCode(), is(402)); 262 261 assertThat(response.getResponseMessage(), equalToIgnoringCase("PAYMENT REQUIRED")); … … 274 273 public void testHttp403() throws IOException { 275 274 // https://tools.ietf.org/html/rfc2324 276 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/403")).connect(progress);275 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/403")).connect(progress); 277 276 assertThat(response.getResponseCode(), is(403)); 278 277 assertThat(response.getResponseMessage(), equalToIgnoringCase("FORBIDDEN")); … … 290 289 public void testHttp404() throws IOException { 291 290 // https://tools.ietf.org/html/rfc2324 292 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/404")).connect(progress);291 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/404")).connect(progress); 293 292 assertThat(response.getResponseCode(), is(404)); 294 293 assertThat(response.getResponseMessage(), equalToIgnoringCase("NOT FOUND")); … … 306 305 public void testHttp500() throws IOException { 307 306 // https://tools.ietf.org/html/rfc2324 308 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/status/500")).connect(progress);307 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/500")).connect(progress); 309 308 assertThat(response.getResponseCode(), is(500)); 310 309 assertThat(response.getResponseMessage(), equalToIgnoringCase("INTERNAL SERVER ERROR")); … … 321 320 @Test 322 321 public void testRequestInTime() throws IOException { 323 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/delay/1")).setReadTimeout(2000).connect(progress);322 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/1")).setReadTimeout(2000).connect(progress); 324 323 assertThat(response.getResponseCode(), is(200)); 325 324 } … … 331 330 @Test(expected = IOException.class) 332 331 public void testTakesTooLong() throws IOException { 333 HttpClient.create(new URL("https://httpbin go.org/delay/1")).setReadTimeout(500).connect(progress);332 HttpClient.create(new URL("https://httpbin.org/delay/1")).setReadTimeout(500).connect(progress); 334 333 } 335 334 … … 340 339 @Test 341 340 public void testDeflate() throws IOException { 342 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/deflate")).connect(progress);341 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/deflate")).connect(progress); 343 342 assertThat(response.getResponseCode(), is(200)); 344 343 try (InputStream in = response.getContent(); … … 354 353 @Test 355 354 public void testGzip() throws IOException { 356 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin go.org/gzip")).connect(progress);355 final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/gzip")).connect(progress); 357 356 assertThat(response.getResponseCode(), is(200)); 358 357 try (InputStream in = response.getContent();
Note:
See TracChangeset
for help on using the changeset viewer.