/** * This test should only be run after the Servlet container for the UWS service * has been restarted. It expects that the UWS service has no Jobs. */ @Test public void testForbidden() { log.debug("testForbidden"); try { // Request the UWS service. WebConversation conversation = new WebConversation(); WebResponse response = get(conversation, serviceUrl); int code = response.getResponseCode(); Assert.assertEquals(403, code); } catch(HttpException expected) { log.debug("caught expected exception: " + expected); Assert.assertEquals(403, expected.getResponseCode()); } catch (Exception unexpected) { log.error("unexpected exception", unexpected); Assert.fail("unexpected exception: " + unexpected); } }
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }
assertEquals(responseCode.intValue(), ex.getResponseCode());
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }
/** * Here we expect an errorCode other than 200, and look for it checking for * text is omitted as it doesnt work. It would never work on java1.3, but * one may have expected java1.4+ to have access to the error stream in * responses. Clearly not. * * @param request * @param errorCode * @param errorText optional text string to search for * @throws MalformedURLException * @throws IOException * @throws SAXException */ protected void expectErrorCode(WebRequest request, int errorCode, String errorText) throws MalformedURLException, IOException, SAXException { String failureText = "Expected error " + errorCode + " from " + request.getURL(); try { newClient().getResponse(request); fail(errorText + " -got success instead"); } catch (HttpException e) { assertEquals(failureText, errorCode, e.getResponseCode()); /* * checking for text omitted as it doesnt work. if(errorText!=null) { * assertTrue( "Failed to find "+errorText+" in "+ * e.getResponseMessage(), e.getMessage().indexOf(errorText)>=0); } */ } } }
assertEquals(failureText, errorCode, e.getResponseCode());
/** * Here we expect an errorCode other than 200, and look for it checking for * text is omitted as it doesnt work. It would never work on java1.3, but * one may have expected java1.4+ to have access to the error stream in * responses. Clearly not. * * @param request * @param errorCode * @param errorText optional text string to search for * @throws MalformedURLException * @throws IOException * @throws SAXException */ protected void expectErrorCode(WebRequest request, int errorCode, String errorText) throws MalformedURLException, IOException, SAXException { String failureText = "Expected error " + errorCode + " from " + request.getURL(); try { newClient().getResponse(request); fail(errorText + " -got success instead"); } catch (HttpException e) { assertEquals(failureText, errorCode, e.getResponseCode()); /* * checking for text omitted as it doesnt work. if(errorText!=null) { * assertTrue( "Failed to find "+errorText+" in "+ * e.getResponseMessage(), e.getMessage().indexOf(errorText)>=0); } */ } } }