protected WebResponse head(WebConversation conversation, String resourceUrl) throws IOException, SAXException { log.debug("**************************************************"); log.debug("HTTP HEAD: " + resourceUrl); WebRequest getRequest = new HeadMethodWebRequest(resourceUrl); conversation.clearContents(); WebResponse response = conversation.getResponse(getRequest); assertNotNull("HEAD response to " + resourceUrl + " is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("Response code: " + response.getResponseCode()); assertEquals("Non-200 GET response code to " + resourceUrl, 200, response.getResponseCode()); return response; }
protected WebResponse execute(WebConversation conversation, final WebRequest request) throws IOException, SAXException { // execute the operation WebResponse response = conversation.getResponse(request); assertNotNull("POST response to " + request.getURL().toString() + " is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("Response code: " + response.getResponseCode()); // handle potential redirects response = getRedirectResponse(conversation, request, response); return response; }
/** * 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() ); } }
/** * 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() ); } }
private WebResponse deleteJob(WebConversation conversation, WebRequest request, String resourceUrl) throws IOException, SAXException, JDOMException { log.debug(Util.getRequestParameters(request)); conversation.clearContents(); WebResponse response = conversation.getResponse(request); assertNotNull("Response to request is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("response code: " + response.getResponseCode()); assertEquals("Response code should be 303", 303, response.getResponseCode()); // Get the redirect. String location = response.getHeaderField("Location"); log.debug("Location: " + location); assertNotNull("Response location header not set", location); assertEquals("Response to location header incorrect", serviceUrl, location); return response; }
protected WebResponse post(WebConversation conversation, WebRequest request) throws IOException, SAXException { // POST request to the phase resource. log.debug("**************************************************"); log.debug("HTTP POST: " + request.getURL().toString()); log.debug(Util.getRequestParameters(request)); conversation.clearContents(); WebResponse response = conversation.getResponse(request); assertNotNull("POST response to " + request.getURL().toString() + " is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("Response code: " + response.getResponseCode()); assertEquals("POST response code to " + request.getURL().toString() + " should be 303", 303, response.getResponseCode()); // Get the redirect. String location = response.getHeaderField("Location"); log.debug("Location: " + location); assertNotNull("POST response to " + request.getURL().toString() + " location header not set", location); //assertEquals(POST response to " + resourceUrl + " location header incorrect", baseUrl + "/" + jobId, location); return response; }
/** * check whether redirect is configured * @param response * @return */ private boolean redirectConfigured( WebResponse response ) { boolean isAutoredirect=getClient().getClientProperties().isAutoRedirect(); boolean hasLocation=response.getHeaderField( "Location" ) != null; int responseCode=response.getResponseCode(); boolean result=isAutoredirect && responseCode >= HttpURLConnection.HTTP_MOVED_PERM && responseCode <= HttpURLConnection.HTTP_MOVED_TEMP && hasLocation; return result; }
/** * Checks if the response contains any error message. */ protected final WebResponse checkError(WebResponse resp) throws SAXException, ProcessingException, IOException { if(resp.getResponseCode()!=200) throw new ProcessingException("request failed "+resp.getResponseMessage()); Document dom = Util.getDom4j(resp); org.dom4j.Node errorNode = dom.selectSingleNode("//DIV[@class='errormessage']"); if(errorNode!=null) { // this happens for example when you request "http://nosuchproject.dev.java.net/" throw new ProcessingException(errorNode.getStringValue().trim()); } return resp; }
/** * check whether redirect is configured * @param response * @return */ private boolean redirectConfigured( WebResponse response ) { boolean isAutoredirect=getClient().getClientProperties().isAutoRedirect(); boolean hasLocation=response.getHeaderField( "Location" ) != null; int responseCode=response.getResponseCode(); boolean result=isAutoredirect && responseCode >= HttpURLConnection.HTTP_MOVED_PERM && responseCode <= HttpURLConnection.HTTP_MOVED_TEMP && hasLocation; return result; }
/** * check whether redirect is configured * @param response * @return */ private boolean redirectConfigured( WebResponse response ) { boolean isAutoredirect=getClient().getClientProperties().isAutoRedirect(); boolean hasLocation=response.getHeaderField( "Location" ) != null; int responseCode=response.getResponseCode(); boolean result=isAutoredirect && responseCode >= HttpURLConnection.HTTP_MOVED_PERM && responseCode <= HttpURLConnection.HTTP_MOVED_TEMP && hasLocation; return result; }
protected WebResponse get(WebConversation conversation, String resourceUrl, String expectedContentType) throws IOException, SAXException { log.debug("**************************************************"); log.debug("HTTP GET: " + resourceUrl); WebRequest getRequest = new GetMethodWebRequest(resourceUrl); conversation.clearContents(); WebResponse response = conversation.getResponse(getRequest); assertNotNull("GET response to " + resourceUrl + " is null", response); log.debug(Util.getResponseHeaders(response)); int numRedir = 0; int rcode = response.getResponseCode(); while (rcode == 302 || rcode == 303) { log.debug("Response code: " + rcode); String loc = response.getHeaderField("Location"); Assert.assertNotNull("Location", loc); getRequest = new GetMethodWebRequest(loc); response = conversation.getResponse(getRequest); rcode = response.getResponseCode(); } log.debug("Content-Type: " + response.getContentType()); assertEquals("GET response Content-Type header to " + resourceUrl + " is incorrect", expectedContentType, response.getContentType()); return response; }
/** * 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); } }
@Test public void testInvalidServiceUrl() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(false); WebResponse res = client.getResponse(CONTEXT_URL + "/services/NoSuchService"); assertEquals(404, res.getResponseCode()); assertEquals("text/html", res.getContentType()); }
@Test public void testGetWSDLWithXMLBinding() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(true); WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter2?wsdl"); WebResponse res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); Document doc = StaxUtils.read(res.getInputStream()); assertNotNull(doc); addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/"); assertValid("//wsdl:operation[@name='greetMe']", doc); NodeList addresses = assertValid("//http:address/@location", doc); boolean found = true; for (int i = 0; i < addresses.getLength(); i++) { String address = addresses.item(i).getLocalName(); if (address.startsWith("http://localhost") && address.endsWith("/services/greeter2")) { found = true; break; } } assertTrue(found); }
@Test public void testGetImportedXSD() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(true); WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter?wsdl"); WebResponse res = client.getResponse(req); assertEquals(200, res.getResponseCode()); String text = res.getText(); assertEquals("text/xml", res.getContentType()); assertTrue(text.contains(CONTEXT_URL + "/services/greeter?wsdl=test_import.xsd")); req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter?wsdl=test_import.xsd"); res = client.getResponse(req); assertEquals(200, res.getResponseCode()); text = res.getText(); assertEquals("text/xml", res.getContentType()); assertTrue("the xsd should contain the completType SimpleStruct", text.contains("<complexType name=\"SimpleStruct\">")); }
@Test public void testGetWSDLWithIncludes() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(true); WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter3?wsdl"); WebResponse res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); Document doc = StaxUtils.read(res.getInputStream()); assertNotNull(doc); assertXPathEquals("//xsd:include/@schemaLocation", "http://localhost/mycontext/services/greeter3?xsd=hello_world_includes2.xsd", doc.getDocumentElement()); req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter3?xsd=hello_world_includes2.xsd"); res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); doc = StaxUtils.read(res.getInputStream()); assertNotNull(doc); assertValid("//xsd:complexType[@name='ErrorCode']", doc); }
@Test public void testGetWSDL() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(true); WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter?wsdl"); WebResponse res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); Document doc = StaxUtils.read(res.getInputStream()); assertNotNull(doc); assertValid("//wsdl:operation[@name='greetMe']", doc); assertValid("//wsdlsoap:address[@location='" + CONTEXT_URL + "/services/greeter']", doc); }
assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); new GetMethodQueryWebRequest(CONTEXT_URL + "/services/DerivedGreeterService?wsdl"); res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType());
@Test public void testGetWSDLWithMultiplePublishedEndpointUrl() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(true); WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/greeter5?wsdl"); WebResponse res = client.getResponse(req); assertEquals(200, res.getResponseCode()); assertEquals("text/xml", res.getContentType()); Document doc = StaxUtils.read(res.getInputStream()); assertNotNull(doc); WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader(); wsdlReader.setFeature("javax.wsdl.verbose", false); assertValid("//wsdl:service[@name='SOAPService']/wsdl:port[@name='SoapPort']/wsdlsoap:address[@location='" + "http://cxf.apache.org/publishedEndpointUrl1']", doc); assertValid("//wsdl:service[@name='SOAPService']/wsdl:port[@name='SoapPort1']/wsdlsoap:address[@location='" + "http://cxf.apache.org/publishedEndpointUrl2']", doc); } @Test