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 static net.sourceforge.jwebunit.junit.JWebUnit.assertTextPresent;
22  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
23  import static net.sourceforge.jwebunit.junit.JWebUnit.getTestContext;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.util.List;
29  import java.util.Locale;
30  
31  import javax.servlet.http.Cookie;
32  
33  import net.sourceforge.jwebunit.util.TestContext;
34  
35  import org.junit.Test;
36  
37  public class TestContextTest extends JWebUnitAPITestCase {
38      private TestContext context;
39  
40      public void setUp() throws Exception {
41          super.setUp();
42          context = new TestContext();
43          context.setAuthorization("user", "pwd");
44          context.addCookie("key", "val", "www.foo.bar");
45          context.setLocale(Locale.CANADA_FRENCH);
46      }
47  
48  
49      @Test
50      public void testInit() {
51          assertTrue(context.hasAuthorization());
52          assertTrue(context.hasCookies());
53          assertEquals(context.getUser(), "user");
54          assertEquals(context.getPassword(), "pwd");
55          List<?> cookies = context.getCookies();
56          Cookie c = (Cookie)cookies.get(0);
57          assertEquals(c.getName(), "key");
58          assertEquals(c.getValue(), "val");
59          assertEquals(c.getDomain(), "www.foo.bar");
60          assertEquals(Locale.CANADA_FRENCH, context.getLocale());
61          assertEquals("http://localhost:8080", context.getBaseUrl().toString());
62          assertNull(context.getResourceBundleName());
63      }
64  
65      @Test
66      public void testResourceBundle() {
67          String name = "/TestContextBundle";
68          context.setResourceBundleName("/TestContextBundle");
69          assertEquals(name, context.getResourceBundleName());
70      }
71  
72      @Test
73      public void testServerSideUserAgentOverride() {
74          String userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3";
75          getTestContext().setUserAgent(userAgent);
76          beginAt("/headers.jsp");
77          assertTextPresent("User-Agent=[" + userAgent + "]");
78      }
79  
80      @Test
81      public void testClientSideUserAgentOverride() {
82          getTestContext().setBaseUrl(HOST_PATH + "/TestContextTest");
83          String userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3";
84          getTestContext().setUserAgent(userAgent);
85          beginAt("/testPage.html");
86          assertTextPresent("Browser user-agent: "+userAgent);
87      }
88  
89  }