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.util;
20  
21  import javax.servlet.http.Cookie;
22  
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  /**
32   * Establish context for tests (things such as locale, base url for the application, cookies, authorization). The
33   * context can be accessed through the {@link net.sourceforge.jwebunit.WebTestCase}or
34   * {@link net.sourceforge.jwebunit.junit.WebTester}.
35   * 
36   * @author Wilkes Joiner
37   * @author Jim Weaver
38   * @author Julien Henry
39   */
40  public class TestContext {
41      private String user;
42  
43      private String passwd;
44  
45      private String domain;
46  
47      private List<javax.servlet.http.Cookie> cookies;
48  
49      private boolean hasAuth = false;
50  
51      private boolean hasNTLMAuth = false;
52  
53      private Locale locale = Locale.getDefault();
54  
55      private String resourceBundleName;
56  
57      private URL baseUrl;
58  
59      private String userAgent;
60  
61      private Map<String, String> requestHeaders = new HashMap<String, String>();
62  
63      private String proxyUser = null;
64  
65      private String proxyPasswd = null;
66  
67      private String proxyHost = null;
68  
69      private int proxyPort = -1;
70  
71      private boolean hasProxyAuth = false;
72  
73      /**
74       * Construct a test client context.
75       */
76      public TestContext() {
77          cookies = new ArrayList<javax.servlet.http.Cookie>();
78          try {
79              baseUrl = new URL("http://localhost:8080");
80          } catch (MalformedURLException e) {
81              // Should not be invalid
82              e.printStackTrace();
83          }
84      }
85  
86      /**
87       * Clear all authorizations (basic, digest, ntlm, proxy).
88       * 
89       */
90      public void clearAuthorizations() {
91          hasAuth = false;
92          hasNTLMAuth = false;
93          hasProxyAuth = false;
94      }
95  
96      /**
97       * Set basic authentication information for the test context.
98       * 
99       * @param user user name
100      * @param passwd password
101      */
102     public void setAuthorization(String user, String passwd) {
103         this.user = user;
104         this.passwd = passwd;
105         hasAuth = true;
106     }
107 
108     /**
109      * Set NTLM authentication information for the test context.
110      * 
111      * @param user user name
112      * @param passwd password
113      */
114     public void setNTLMAuthorization(String user, String passwd, String domain) {
115         this.user = user;
116         this.passwd = passwd;
117         this.domain = domain;
118         hasNTLMAuth = true;
119     }
120 
121     /**
122      * Set proxy authentication information for the test context.
123      * 
124      * @param user user name (null if none)
125      * @param passwd password (null if none)
126      * @param host proxy host name (null if applicable to any host).
127      * @param port proxy port (negative if applicable to any port).
128      */
129     public void setProxyAuthorization(String user, String passwd, String host,
130             int port) {
131         this.proxyUser = user;
132         this.proxyPasswd = passwd;
133         this.proxyHost = host;
134         this.proxyPort = port;
135         hasProxyAuth = !(null == user && null == passwd);
136     }
137 
138     /**
139      * Add a cookie to the test context. These cookies are set on the conversation when you use a {WebTester#beginAt}.
140      * 
141      * @param name cookie name.
142      * @param value cookie value.
143      * @param domain cookie domain (ie localhost or www.foo.bar).
144      * @param expiry the expiry date in seconds. -1 will delete this cookie, 0 will delete it at the end of the browser session.
145      */
146     public void addCookie(String name, String value, String domain, int expiry) {
147         Cookie c = new Cookie(name, value);
148         c.setDomain(domain);
149         c.setMaxAge(expiry);
150         addCookie(c);
151     }
152     
153     /**
154      * Add a cookie to the test context. These cookies are set on the conversation when you use a {WebTester#beginAt}.
155      * 
156      * @param name cookie name.
157      * @param value cookie value.
158      * @param domain cookie domain (ie localhost or www.foo.bar).
159      */
160     public void addCookie(String name, String value, String domain) {
161         Cookie c = new Cookie(name, value);
162         c.setDomain(domain);
163         addCookie(c);
164     }
165 
166     /**
167      * Add a cookie to the test context. These cookies are set on the conversation when you use a {WebTester#beginAt}.
168      * 
169      * @param cookie a cookie.
170      */
171     public void addCookie(Cookie cookie) {
172         cookies.add(cookie);
173     }
174 
175     /**
176      * Return true if a basic authentication has been set on the context via {@link #setAuthorization}.
177      */
178     public boolean hasAuthorization() {
179         return hasAuth;
180     }
181 
182     /**
183      * Return true if a NTLM authentication has been set on the context via {@link #setNTLMAuthorization}.
184      */
185     public boolean hasNTLMAuthorization() {
186         return hasNTLMAuth;
187     }
188 
189     /**
190      * Return true if a proxy authentication has been set on the context via {@link #setProxyAuthorization}.
191      */
192     public boolean hasProxyAuthorization() {
193         return hasProxyAuth;
194     }
195 
196     /**
197      * Return true if one or more cookies have been added to the test context.
198      */
199     public boolean hasCookies() {
200         return cookies.size() > 0;
201     }
202 
203     /**
204      * Return the authorized user for the test context.
205      */
206     public String getUser() {
207         return user;
208     }
209 
210     /**
211      * Return the user password.
212      */
213     public String getPassword() {
214         return passwd;
215     }
216 
217     /**
218      * Return the user domain.
219      */
220     public String getDomain() {
221         return domain;
222     }
223 
224     /**
225      * Return the cookies which have been added to the test context.
226      */
227     public List<javax.servlet.http.Cookie> getCookies() {
228         return cookies;
229     }
230 
231     public String getUserAgent() {
232         return userAgent;
233     }
234 
235     public void setUserAgent(String userAgent) {
236         this.userAgent = userAgent;
237     }
238 
239     public boolean hasUserAgent() {
240         return userAgent != null;
241     }
242 
243     /**
244      * Return the locale established for the test context. If the locale has not been explicitly set,
245      * Locale.getDefault() will be returned.
246      */
247     public Locale getLocale() {
248         return locale;
249     }
250 
251     /**
252      * Set the locale for the test context.
253      */
254     public void setLocale(Locale locale) {
255         this.locale = locale;
256     }
257 
258     /**
259      * Set a resource bundle to use for the test context (will be used to lookup expected values by key in WebTester).
260      * 
261      * @param name path name of the resource bundle.
262      */
263     public void setResourceBundleName(String name) {
264         resourceBundleName = name;
265     }
266 
267     /**
268      * Return the test context resource bundle for expected value lookups.
269      */
270     public String getResourceBundleName() {
271         return resourceBundleName;
272     }
273 
274     /**
275      * Return the proxy server name
276      */
277     public String getProxyHost() {
278         return proxyHost;
279     }
280 
281     /**
282      * Return the proxy server port
283      */
284     public int getProxyPort() {
285         return proxyPort;
286     }
287 
288     /**
289      * Return the proxy user name
290      */
291     public String getProxyUser() {
292         return proxyUser;
293     }
294 
295     /**
296      * Return the proxy password
297      */
298     public String getProxyPasswd() {
299         return proxyPasswd;
300     }
301 
302     /**
303      * Return the base URL for the test context. The default base URL is port 8080 on localhost.
304      */
305     public URL getBaseUrl() {
306         return baseUrl;
307     }
308 
309     /**
310      * Set the base url for the test context.
311      * 
312      * @param url Base url value - A trailing "/" is appended if not provided.
313      */
314     public void setBaseUrl(String url) {
315         try {
316             baseUrl = new URL(url.endsWith("/") ? url : url + "/");
317         } catch (MalformedURLException e) {
318             throw new RuntimeException(e);
319         }
320     }
321 
322     /**
323      * Set the base url for the test context.
324      * 
325      * @param url Base url value. Anything after trailing "/" will be skipped.
326      */
327     public void setBaseUrl(URL url) {
328         baseUrl = url;
329     }
330 
331     /**
332      * Add a custom request header.
333      * @param name header name.
334      * @param value header value.
335      */
336     public void addRequestHeader(final String name, final String value) {
337         requestHeaders.put(name, value);
338     }
339 
340     /**
341      * Remove a custom request header.
342      * @param name header name.
343      */
344     public void removeRequestHeader(final String name) {
345         requestHeaders.remove(name);
346     }
347 
348     /**
349      * Get custom request headers.
350      * @param name header name.
351      * @param value header value.
352      */
353     public Map<String, String> getRequestHeaders() {
354         return requestHeaders;
355     }
356 
357     /**
358      * Clear custom request headers.
359      *
360      */
361     public void clearRequestHeaders() {
362         requestHeaders = new HashMap<String, String>();
363     }
364 
365 }