1   package com.diasparsoftware.htmlunitx.test;
2   
3   import java.io.*;
4   import java.net.URL;
5   import java.util.Collections;
6   
7   import junit.framework.TestCase;
8   
9   import com.diasparsoftware.htmlunitx.*;
10  import com.gargoylesoftware.base.testing.EqualsTester;
11  import com.gargoylesoftware.htmlunit.*;
12  import com.gargoylesoftware.htmlunit.html.HtmlPage;
13  
14  public class InputStreamWebResponseTest extends TestCase {
15      public void testEquals() throws Exception {
16          URL foo = new URL("http://foo");
17          InputStream contentStream1 = new ByteArrayInputStream(
18              new byte[0]);
19          InputStream contentStream2 = new ByteArrayInputStream(
20              new byte[] { 0});
21  
22          InputStreamWebResponse a = new InputStreamWebResponse(
23              foo, contentStream1);
24          InputStreamWebResponse b = new InputStreamWebResponse(
25              foo, contentStream1);
26          InputStreamWebResponse c = new InputStreamWebResponse(
27              foo, contentStream2);
28          InputStreamWebResponse d = new InputStreamWebResponse(
29              foo, contentStream1) {
30          };
31  
32          new EqualsTester(a, b, c, d);
33  
34          c = new InputStreamWebResponse(new URL("file://hello"),
35              contentStream1);
36  
37          new EqualsTester(a, b, c, d);
38      }
39  
40      public void testEmptyHtmlTag() throws Exception {
41          HtmlPage responsePage = makeHtmlPageWithContent("<html></html>");
42  
43          assertEquals("<html/>\r\n", responsePage.asXml());
44      }
45  
46      public void testPageWithTitle() throws Exception {
47          HtmlPage responsePage = makeHtmlPageWithContent("<html><head><title>Hello</title></head></html>");
48  
49          assertEquals("Hello", responsePage.getTitleText());
50      }
51  
52      private HtmlPage makeHtmlPageWithContent(
53          String contentAsString) throws Exception {
54  
55          WebClient webClient = new WebClient();
56  
57          InputStream contentAsStream = TextUtil
58              .toInputStream(contentAsString);
59  
60          TestableWebConnection connection = new TestableWebConnection(
61              webClient);
62  
63          URL url = new URL("http://foo");
64          InputStreamWebResponse inputStreamWebResponse = new InputStreamWebResponse(
65              url, contentAsStream);
66          inputStreamWebResponse.setContentType("text/html");
67  
68          connection.setResponse(inputStreamWebResponse);
69  
70          WebResponse response = connection.getResponse(url,
71              SubmitMethod.GET, Collections.EMPTY_LIST,
72              Collections.EMPTY_MAP);
73  
74          InputStreamWebResponse expectedResponse = new InputStreamWebResponse(
75              url, contentAsStream);
76  
77          assertEquals(expectedResponse, response);
78  
79          webClient.setWebConnection(connection);
80          HtmlPage responsePage = (HtmlPage) webClient
81              .getPage(url);
82          return responsePage;
83      }
84  
85      public void testGetResponseBodyWhenStreamBlowsUp()
86          throws Exception {
87  
88          final IOException fakeException = new IOException(
89              "This is a fake exception");
90  
91          InputStreamWebResponse response = new InputStreamWebResponse(
92              new URL("http://foo"), null) {
93              protected byte[] getContentAsBytes()
94                  throws UnsupportedEncodingException,
95                  IOException {
96  
97                  throw fakeException;
98              }
99          };
100 
101         try {
102             response.getResponseBody();
103             fail("Didn't blow up when the content stream blew up?!");
104         } catch (RuntimeException expected) {
105         }
106     }
107 }