Changeset 11250 in josm


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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r10420 r11250  
    66import java.io.BufferedOutputStream;
    77import java.io.BufferedReader;
     8import java.io.ByteArrayInputStream;
    89import java.io.IOException;
    910import java.io.InputStream;
     
    190191        private boolean uncompress;
    191192        private boolean uncompressAccordingToContentDisposition;
     193        private String responseData = null;
    192194
    193195        private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException {
     
    198200            this.responseCode = connection.getResponseCode();
    199201            this.responseMessage = connection.getResponseMessage();
     202            if (this.responseCode >= 300) {
     203                String contentType = getContentType();
     204                if (contentType == null || (
     205                        contentType.contains("text") ||
     206                        contentType.contains("html") ||
     207                        contentType.contains("xml"))
     208                        ) {
     209                    String content = this.fetchContent();
     210                    if (content == null || content.isEmpty()) {
     211                        Main.debug("Server did not return any body");
     212                    } else {
     213                        Main.debug("Response body: ");
     214                        Main.debug(this.fetchContent());
     215                    }
     216                } else {
     217                    Main.debug("Server returned content: {0} of length: {1}. Not printing.", contentType, this.getContentLength());
     218                }
     219            }
    200220        }
    201221
     
    264284                Main.debug(ioe);
    265285                in = connection.getErrorStream();
     286                if (in == null) {
     287                    in = new ByteArrayInputStream(new byte[]{});
     288                }
    266289            }
    267290            if (in != null) {
     
    306329         * @throws IOException if any I/O error occurs
    307330         */
    308         @SuppressWarnings("resource")
    309         public String fetchContent() throws IOException {
    310             try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) {
    311                 return scanner.hasNext() ? scanner.next() : "";
    312             }
     331        public synchronized String fetchContent() throws IOException {
     332            if (responseData == null) {
     333                try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) { // \A - beginning of input
     334                    responseData = scanner.hasNext() ? scanner.next() : "";
     335                }
     336            }
     337            return responseData;
    313338        }
    314339
  • 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.