/** @throws Exception if the test fails */
  @Test
  public void testConstructorWithWebResponse() throws Exception {
    final List<NameValuePair> emptyList = Collections.emptyList();
    final WebResponseData webResponseData =
        new WebResponseData(
            ArrayUtils.EMPTY_BYTE_ARRAY, HttpStatus.SC_NOT_FOUND, "not found", emptyList);
    final WebResponse webResponse =
        new WebResponseImpl(webResponseData, URL_FIRST, HttpMethod.GET, 10);
    final FailingHttpStatusCodeException e = new FailingHttpStatusCodeException(webResponse);

    assertEquals(webResponse, e.getResponse());
    assertEquals(webResponse.getStatusMessage(), e.getStatusMessage());
    assertEquals(webResponse.getStatusCode(), e.getStatusCode());
    assertTrue(
        "message doesn't contain failing url",
        e.getMessage().indexOf(URL_FIRST.toExternalForm()) > -1);
  }
Exemplo n.º 2
0
  /**
   * Tests creation of a web response.
   *
   * @throws Exception if the test fails
   */
  @Test
  public void makeWebResponse() throws Exception {
    final URL url = new URL("http://htmlunit.sourceforge.net/");
    final String content = "<html><head></head><body></body></html>";
    final DownloadedContent downloadedContent = new DownloadedContent.InMemory(content.getBytes());
    final int httpStatus = HttpStatus.SC_OK;
    final long loadTime = 500L;

    final ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 0);
    final StatusLine statusLine = new BasicStatusLine(protocolVersion, HttpStatus.SC_OK, null);
    final HttpResponse httpResponse = new BasicHttpResponse(statusLine);

    final HttpEntity responseEntity = new StringEntity(content);
    httpResponse.setEntity(responseEntity);

    final HttpWebConnection connection = new HttpWebConnection(getWebClient());
    final Method method =
        connection
            .getClass()
            .getDeclaredMethod(
                "makeWebResponse",
                HttpResponse.class,
                WebRequest.class,
                DownloadedContent.class,
                long.class);
    method.setAccessible(true);
    final WebResponse response =
        (WebResponse)
            method.invoke(
                connection,
                httpResponse,
                new WebRequest(url),
                downloadedContent,
                new Long(loadTime));

    Assert.assertEquals(httpStatus, response.getStatusCode());
    Assert.assertEquals(url, response.getWebRequest().getUrl());
    Assert.assertEquals(loadTime, response.getLoadTime());
    Assert.assertEquals(content, response.getContentAsString());
    assertEquals(content.getBytes(), IOUtils.toByteArray(response.getContentAsStream()));
    assertEquals(new ByteArrayInputStream(content.getBytes()), response.getContentAsStream());
  }