source: josm/trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java@ 9669

Last change on this file since 9669 was 9669, checked in by bastiK, 8 years ago

add missing svn:eol-style=native (see #12410)

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