View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests;
20  
21  import org.junit.Test;
22  
23  import java.net.SocketTimeoutException;
24  
25  import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderEquals;
26  import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderMatches;
27  import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderNotPresent;
28  import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderPresent;
29  import static net.sourceforge.jwebunit.junit.JWebUnit.assertResponseCode;
30  import static net.sourceforge.jwebunit.junit.JWebUnit.assertResponseCodeBetween;
31  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextPresent;
32  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTitleEquals;
33  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
34  import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
35  import static net.sourceforge.jwebunit.junit.JWebUnit.setIgnoreFailingStatusCodes;
36  import static net.sourceforge.jwebunit.junit.JWebUnit.setTextField;
37  import static net.sourceforge.jwebunit.junit.JWebUnit.setTimeout;
38  import static net.sourceforge.jwebunit.junit.JWebUnit.submit;
39  import static org.junit.Assert.assertTrue;
40  import static org.junit.Assert.fail;
41  
42  /**
43   * Test redirection support.
44   *
45   * @author Julien Henry
46   */
47  public class ResponseServletTest extends JWebUnitAPITestCase {
48  
49    public void setUp() throws Exception {
50      super.setUp();
51      setIgnoreFailingStatusCodes(true); // ignore failing status codes
52      setBaseUrl(HOST_PATH + "/ResponseServletTest");
53    }
54  
55    /*
56     * currently we can't get the response code from HtmlUnit unless it is a failing code
57     */
58    @Test
59    public void testDefault() {
60      beginAt("/SimpleForm.html");
61      submit();
62      assertResponseCodeBetween(200, 299);
63  
64      // test the headers
65      assertHeaderPresent("Test");
66      assertHeaderNotPresent("Not-present");
67      assertHeaderEquals("Test", "test2");
68      assertHeaderMatches("Header-Added", "[0-9]{2}");
69    }
70  
71    @Test
72    public void testResponse200() {
73      beginAt("/SimpleForm.html");
74      setTextField("status", "200");
75      submit();
76      assertResponseCode(200);
77    }
78  
79    /*
80     * HtmlUnit cannot handle a 301 without a valid Location: header
81      @Test public void testResponse301() {
82          beginAt("/SimpleForm.html");
83          setTextField("status", "301");
84          submit();
85          assertResponseCode(301);
86      }
87     */
88  
89    @Test
90    public void testResponse404() {
91      beginAt("/SimpleForm.html");
92      assertTitleEquals("response form");
93      setTextField("status", "404");
94      submit();
95      assertResponseCode(404);
96    }
97  
98    @Test
99    public void testResponse501() {
100     beginAt("/SimpleForm.html");
101     assertTitleEquals("response form");
102     setTextField("status", "501");
103     submit();
104     assertResponseCode(501);
105   }
106 
107   /**
108    * Issue 1674646: add support for specifying the timeout of pages
109    */
110   @Test
111   public void testTimeout() {
112 
113     // test that timeout was fired
114     setTimeout(500); // specify a global timeout of 0.5 seconds (must be set before the WebConnection is initialised)
115     beginAt("/SimpleForm.html");
116     assertTitleEquals("response form");
117     setTextField("timeout", "1"); // server wait for 1 seconds
118     try {
119       submit();
120       fail("timeout was not called"); // we should not get here
121     } catch (RuntimeException e) {
122       assertTrue("timeout caused by SocketTimeoutException, but was " + e.getCause().getClass(), e.getCause() instanceof SocketTimeoutException);
123     }
124 
125     // close and reset the browser
126     closeBrowser();
127 
128     // test that timeout wasn't fired
129     setTimeout(2000); // specify a global timeout of 2 seconds (must be set before the WebConnection is initialised)
130     beginAt("/SimpleForm.html");
131     assertTitleEquals("response form");
132     setTextField("timeout", "1"); // server wait for 1 seconds
133     submit();
134     assertTextPresent("hello, world!");
135   }
136 
137 }