Changeset 11250 in josm for trunk/test/functional


Ignore:
Timestamp:
2016-11-13T13:12:47+01:00 (8 years ago)
Author:
wiktorn
Message:

Add response debug printing on HTTP Status code >= 300

Closes: #13961

File:
1 edited

Legend:

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

    r10986 r11250  
    1313import java.nio.charset.StandardCharsets;
    1414import java.util.Collections;
     15import java.util.logging.Handler;
     16import java.util.logging.LogRecord;
    1517
    1618import javax.json.JsonObject;
     
    4042    private ProgressMonitor progress;
    4143
     44    private LogRecord captured;
     45    private final Handler handler = new Handler() {
     46
     47        @Override
     48        public void publish(LogRecord record) {
     49            captured = record;
     50        }
     51
     52        @Override
     53        public void flush() {
     54        }
     55
     56        @Override
     57        public void close() throws SecurityException {
     58        }
     59    };
     60
    4261    @Before
    4362    public void setUp() {
    4463        progress = TestUtils.newTestProgressMonitor();
     64        captured = null;
     65        Logging.getLogger().addHandler(handler);
     66        Logging.getLogger().setLevel(Logging.LEVEL_DEBUG);
    4567    }
    4668
     
    154176        final String content = response.fetchContent();
    155177        assertThat(content, containsString("-=[ teapot ]=-"));
    156     }
     178        assertThat(captured.getMessage(), containsString("-=[ teapot ]=-"));
     179        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     180    }
     181
     182    @Test()
     183    public void testHttp401() throws IOException {
     184        // https://tools.ietf.org/html/rfc2324
     185        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/401")).connect(progress);
     186        assertThat(response.getResponseCode(), is(401));
     187        assertThat(response.getResponseMessage(), is("UNAUTHORIZED"));
     188        final String content = response.fetchContent();
     189        assertThat(content, is(""));
     190        assertThat(captured.getMessage(), containsString("Server did not return any body"));
     191        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     192    }
     193
     194    @Test
     195    public void testHttp402() throws IOException {
     196        // https://tools.ietf.org/html/rfc2324
     197        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/402")).connect(progress);
     198        assertThat(response.getResponseCode(), is(402));
     199        assertThat(response.getResponseMessage(), is("PAYMENT REQUIRED"));
     200        final String content = response.fetchContent();
     201        assertThat(content, containsString("Fuck you, pay me!"));
     202        assertThat(captured.getMessage(), containsString("Fuck you, pay me!"));
     203        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     204    }
     205
     206    @Test
     207    public void testHttp403() throws IOException {
     208        // https://tools.ietf.org/html/rfc2324
     209        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/403")).connect(progress);
     210        assertThat(response.getResponseCode(), is(403));
     211        assertThat(response.getResponseMessage(), is("FORBIDDEN"));
     212        final String content = response.fetchContent();
     213        assertThat(content, is(""));
     214        assertThat(captured.getMessage(), containsString("Server did not return any body"));
     215        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     216    }
     217
     218    @Test
     219    public void testHttp404() throws IOException {
     220        // https://tools.ietf.org/html/rfc2324
     221        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/404")).connect(progress);
     222        assertThat(response.getResponseCode(), is(404));
     223        assertThat(response.getResponseMessage(), is("NOT FOUND"));
     224        final String content = response.fetchContent();
     225        assertThat(content, is(""));
     226        assertThat(captured.getMessage(), containsString("Server did not return any body"));
     227        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     228    }
     229
     230    @Test
     231    public void testHttp500() throws IOException {
     232        // https://tools.ietf.org/html/rfc2324
     233        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/500")).connect(progress);
     234        assertThat(response.getResponseCode(), is(500));
     235        assertThat(response.getResponseMessage(), is("INTERNAL SERVER ERROR"));
     236        final String content = response.fetchContent();
     237        assertThat(content, containsString(""));
     238        assertThat(captured.getMessage(), containsString("Server did not return any body"));
     239        assertThat(captured.getLevel(), is(Logging.LEVEL_DEBUG));
     240    }
     241
    157242
    158243    @Test
Note: See TracChangeset for help on using the changeset viewer.