Ignore:
Timestamp:
2016-01-18T21:42:58+01:00 (8 years ago)
Author:
simon04
Message:

HttpClient: test progress monitor handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r9255 r9529  
    1717import javax.json.spi.JsonProvider;
    1818
     19import org.junit.Before;
    1920import org.junit.BeforeClass;
    2021import org.junit.Test;
    2122import org.openstreetmap.josm.JOSMFixture;
     23import org.openstreetmap.josm.TestUtils;
    2224import org.openstreetmap.josm.data.Version;
     25import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2326
    2427/**
     
    2730public class HttpClientTest {
    2831
     32    private ProgressMonitor progress;
     33
    2934    @BeforeClass
    3035    public static void setUpBeforeClass() {
    3136        JOSMFixture.createFunctionalTestFixture().init();
     37    }
     38
     39    @Before
     40    public void setUp() throws Exception {
     41        progress = TestUtils.newTestProgressMonitor();
    3242    }
    3343
     
    5161    @Test
    5262    public void testGet() throws Exception {
    53         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect();
     63        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect(progress);
    5464        assertThat(response.getRequestMethod(), is("GET"));
    5565        assertThat(response.getResponseCode(), is(200));
     
    7080    @Test
    7181    public void testUserAgent() throws Exception {
    72         try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect().getContent();
     82        try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect(progress).getContent();
    7383             final JsonReader json = JsonProvider.provider().createReader(in)) {
    7484            assertThat(json.readObject().getString("user-agent"), is(Version.getInstance().getFullAgentString()));
     
    7888    @Test
    7989    public void testFetchUtf8Content() throws Exception {
    80         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect();
     90        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect(progress);
    8191        assertThat(response.getResponseCode(), is(200));
    8292        final String content = response.fetchContent();
     
    91101                .setHeader("Content-Type", "text/plain")
    92102                .setRequestBody(text.getBytes(StandardCharsets.UTF_8))
    93                 .connect();
     103                .connect(progress);
    94104        assertThat(response.getResponseCode(), is(200));
    95105        try (final InputStream in = response.getContent();
     
    100110
    101111    @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
    102125    public void testRelativeRedirects() throws Exception {
    103         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect();
     126        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect(progress);
    104127        assertThat(response.getResponseCode(), is(200));
    105128        assertThat(response.getContentLength() > 100, is(true));
     
    108131    @Test
    109132    public void testAbsoluteRedirects() throws Exception {
    110         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect();
     133        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect(progress);
    111134        assertThat(response.getResponseCode(), is(200));
    112135        assertThat(response.getContentLength() > 100, is(true));
     
    115138    @Test(expected = IOException.class)
    116139    public void testTooMuchRedirects() throws Exception {
    117         HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect();
     140        HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect(progress);
    118141    }
    119142
     
    121144    public void test418() throws Exception {
    122145        // https://tools.ietf.org/html/rfc2324
    123         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect();
     146        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect(progress);
    124147        assertThat(response.getResponseCode(), is(418));
    125148        assertThat(response.getResponseMessage(), is("I'M A TEAPOT"));
     
    130153    @Test
    131154    public void testRequestInTime() throws Exception {
    132         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect();
     155        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect(progress);
    133156        assertThat(response.getResponseCode(), is(200));
    134157    }
     
    136159    @Test(expected = IOException.class)
    137160    public void testTakesTooLong() throws Exception {
    138         HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect();
     161        HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect(progress);
    139162    }
    140163}
Note: See TracChangeset for help on using the changeset viewer.