com.meterware.httpunit.WebResponse.getResponseCode()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(15.1k)|赞(0)|评价(0)|浏览(150)

本文整理了Java中com.meterware.httpunit.WebResponse.getResponseCode()方法的一些代码示例,展示了WebResponse.getResponseCode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebResponse.getResponseCode()方法的具体详情如下:
包路径:com.meterware.httpunit.WebResponse
类名称:WebResponse
方法名:getResponseCode

WebResponse.getResponseCode介绍

[英]Returns the response code associated with this response.
[中]返回与此响应关联的响应代码。

代码示例

代码示例来源:origin: org.opencadc/cadc-test-uws

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;
}

代码示例来源:origin: org.opencadc/cadc-test-uws

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;
}

代码示例来源:origin: httpunit/httpunit

/**
 * 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() );
  }
}

代码示例来源:origin: javanettasks/httpunit

/**
 * 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() );
  }
}

代码示例来源:origin: org.kohsuke.httpunit/httpunit

/**
 * 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() );
  }
}

代码示例来源:origin: org.opencadc/cadc-test-uws

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;
}

代码示例来源:origin: org.opencadc/cadc-test-uws

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;
}

代码示例来源:origin: javanettasks/httpunit

/**
 * 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;
}

代码示例来源:origin: javanettasks/javanettasks

/**
 * 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;
}

代码示例来源:origin: org.kohsuke.httpunit/httpunit

/**
 * 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;
}

代码示例来源:origin: httpunit/httpunit

/**
 * 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;
}

代码示例来源:origin: org.opencadc/cadc-test-uws

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;
}

代码示例来源:origin: org.opencadc/cadc-test-uws

/**
 * 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);
  }
}

代码示例来源:origin: apache/cxf

@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());
}

代码示例来源:origin: apache/cxf

@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);
}

代码示例来源:origin: apache/cxf

@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\">"));
}

代码示例来源:origin: apache/cxf

@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);
}

代码示例来源:origin: apache/cxf

@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);
}

代码示例来源:origin: apache/cxf

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());

代码示例来源:origin: apache/cxf

@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

相关文章

微信公众号

最新文章

更多

WebResponse类方法