001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.http.client;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.junit.client.GWTTestCase;
020:
021: /**
022: * Test cases for the {@link RequestBuilder} class.
023: */
024: public class RequestBuilderTest extends GWTTestCase {
025: private static final int TEST_FINISH_DELAY = 10000;
026:
027: private static String getTestBaseURL() {
028: return GWT.getModuleBaseURL() + "testRequestBuilder/";
029: }
030:
031: @Override
032: public String getModuleName() {
033: return "com.google.gwt.http.RequestBuilderTest";
034: }
035:
036: /**
037: * Test method for
038: * {@link com.google.gwt.http.client.RequestBuilder#RequestBuilder(java.lang.String, java.lang.String)}.
039: * <p>
040: * NOTE: When running this test against Internet Explorer, the security
041: * settings of IE affect this test. The assumption is that the "Access Data
042: * Sources Across Domains" setting is set to "Disabled". This is the standard
043: * setting for the "Internet" zone, which models the case of a user's browser
044: * sending a request to a foreign website. However, if you are running the
045: * unit tests against a machine running the GWT app which falls into your
046: * "Trusted Sites" or "Local Network" content zone, this setting's value is
047: * different. You will have to change the setting to "Disabled" in these zones
048: * for this test to pass.
049: * <p>
050: * Test Cases:
051: * <ul>
052: * <li>httpMethod == null
053: * <li>httpMethod == ""
054: * <li>url == null
055: * <li>url == ""
056: * <li>url == "www.freebsd.org" - violates same source
057: * </ul>
058: */
059: public void testRequestBuilderStringString()
060: throws RequestException {
061: try {
062: new RequestBuilder((RequestBuilder.Method) null, null);
063: fail("NullPointerException should have been thrown for construction with null method.");
064: } catch (NullPointerException ex) {
065: // purposely ignored
066: }
067:
068: try {
069: new RequestBuilder(RequestBuilder.GET, null);
070: fail("NullPointerException should have been thrown for construction with null URL.");
071: } catch (NullPointerException ex) {
072: // purposely ignored
073: }
074:
075: try {
076: new RequestBuilder(RequestBuilder.GET, "");
077: fail("IllegalArgumentException should have been throw for construction with empty URL.");
078: } catch (IllegalArgumentException ex) {
079: // purposely ignored
080: }
081:
082: try {
083: RequestBuilder builder = new RequestBuilder(
084: RequestBuilder.GET, "http://www.freebsd.org");
085: builder.sendRequest(null, new RequestCallback() {
086: public void onError(Request request, Throwable exception) {
087: // should never get here
088: fail("HTTPRequest timed out");
089: }
090:
091: public void onResponseReceived(Request request,
092: Response response) {
093: // should never get here
094: fail();
095: }
096: });
097: } catch (IllegalArgumentException ex) {
098: // purposely ignored
099: } catch (RequestPermissionException ex) {
100: // this is the type of exception that we expect
101: }
102: }
103:
104: /**
105: * Test method for
106: * {@link com.google.gwt.http.client.RequestBuilder#RequestBuilder(java.lang.String, java.lang.String)}. *
107: */
108: public void testRequestBuilderStringString_HTTPMethodRestrictionOverride() {
109: new RequestBuilder(RequestBuilder.GET, "FOO");
110:
111: class MyRequestBuilder extends RequestBuilder {
112: MyRequestBuilder(String httpMethod, String url) {
113: super (httpMethod, url);
114: }
115: }
116:
117: new MyRequestBuilder("HEAD", "FOO");
118: // should reach here without any exceptions being thrown
119: }
120:
121: /**
122: * Test method for
123: * {@link com.google.gwt.http.client.RequestBuilder#sendRequest(java.lang.String, com.google.gwt.http.client.RequestCallback)}.
124: */
125: public void testSendRequest_GET() throws RequestException {
126: delayTestFinish(TEST_FINISH_DELAY);
127:
128: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
129: getTestBaseURL() + "sendRequest_GET");
130: builder.sendRequest(null, new RequestCallback() {
131: public void onError(Request request, Throwable exception) {
132: fail();
133: }
134:
135: public void onResponseReceived(Request request,
136: Response response) {
137: assertEquals(200, response.getStatusCode());
138: finishTest();
139: }
140: });
141: }
142:
143: /**
144: * Test method for
145: * {@link com.google.gwt.http.client.RequestBuilder#sendRequest(java.lang.String, com.google.gwt.http.client.RequestCallback)}.
146: */
147: public void testSendRequest_POST() throws RequestException {
148: delayTestFinish(TEST_FINISH_DELAY);
149:
150: RequestBuilder builder = new RequestBuilder(
151: RequestBuilder.POST, getTestBaseURL()
152: + "sendRequest_POST");
153: builder.setHeader("Content-Type",
154: "application/x-www-form-urlencoded");
155: builder.sendRequest("method=test+request",
156: new RequestCallback() {
157: public void onError(Request request,
158: Throwable exception) {
159: fail("HTTPRequest timed out");
160: }
161:
162: public void onResponseReceived(Request request,
163: Response response) {
164: assertEquals(200, response.getStatusCode());
165: finishTest();
166: }
167: });
168: }
169:
170: public void testSetPassword() {
171: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
172: getTestBaseURL());
173: try {
174: builder.setPassword(null);
175: } catch (NullPointerException ex) {
176: // Correct behavior, exception was thrown
177: }
178:
179: try {
180: builder.setPassword("");
181: } catch (IllegalArgumentException ex) {
182: // Correct behavior, exception was thrown
183: }
184: }
185:
186: /**
187: * Test method for
188: * {@link com.google.gwt.http.client.RequestBuilder#setHeader(java.lang.String, java.lang.String)}.
189: *
190: * <p>
191: * Test Cases:
192: * <ul>
193: * <li>name == null
194: * <li>name == ""
195: * <li>value == null
196: * <li>value == ""
197: * </ul>
198: */
199: public void testSetRequestHeader() throws RequestException {
200: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
201: getTestBaseURL() + "setRequestHeader");
202:
203: try {
204: builder.setHeader(null, "bar");
205: fail("setRequestHeader(null, \"bar\")");
206: } catch (NullPointerException ex) {
207: // purposely ignored
208: }
209:
210: try {
211: builder.setHeader("", "bar");
212: fail("setRequestHeader(\"\", \"bar\")");
213: } catch (IllegalArgumentException ex) {
214: // purposely ignored
215: }
216:
217: try {
218: builder.setHeader("foo", null);
219: fail("setRequestHeader(\"foo\", null)");
220: } catch (NullPointerException ex) {
221: // purposely ignored
222: }
223:
224: try {
225: builder.setHeader("foo", "");
226: fail("setRequestHeader(\"foo\", \"\")");
227: } catch (IllegalArgumentException ex) {
228: // purposely ignored
229: }
230:
231: delayTestFinish(TEST_FINISH_DELAY);
232:
233: builder = new RequestBuilder(RequestBuilder.GET,
234: getTestBaseURL() + "setRequestHeader");
235: builder.setHeader("Foo", "Bar");
236: builder.setHeader("Foo", "Bar1");
237:
238: builder.sendRequest(null, new RequestCallback() {
239: public void onError(Request request, Throwable exception) {
240: fail("HTTPRequest timed out");
241: }
242:
243: public void onResponseReceived(Request request,
244: Response response) {
245: assertEquals(200, response.getStatusCode());
246: finishTest();
247: }
248: });
249: }
250:
251: /**
252: * Test method for
253: * {@link com.google.gwt.http.client.RequestBuilder#setTimeoutMillis(int)}.
254: *
255: * <p>
256: * Test Cases:
257: * <ul>
258: * <li>Timeout greater than the server's response time
259: * <li>Timeout is less than the server's response time
260: * </ul>
261: */
262: public void testSetTimeout_noTimeout() throws RequestException {
263: delayTestFinish(TEST_FINISH_DELAY);
264:
265: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
266: getTestBaseURL() + "setTimeout/noTimeout");
267: builder.setTimeoutMillis(10000);
268: builder.sendRequest(null, new RequestCallback() {
269: public void onError(Request request, Throwable exception) {
270: fail("Test timed out");
271: }
272:
273: public void onResponseReceived(Request request,
274: Response response) {
275: assertEquals(200, response.getStatusCode());
276: finishTest();
277: }
278: });
279: }
280:
281: /**
282: * Test method for
283: * {@link com.google.gwt.http.client.RequestBuilder#setTimeoutMillis(int)}.
284: *
285: * <p>
286: * Test Cases:
287: * <ul>
288: * <li>Timeout greater than the server's response time
289: * <li>Timeout is less than the server's response time
290: * </ul>
291: */
292: public void testSetTimeout_timeout() throws RequestException {
293: delayTestFinish(TEST_FINISH_DELAY);
294:
295: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
296: getTestBaseURL() + "setTimeout/timeout");
297: builder.setTimeoutMillis(2000);
298: builder.sendRequest(null, new RequestCallback() {
299: public void onError(Request request, Throwable exception) {
300: finishTest();
301: }
302:
303: public void onResponseReceived(Request request,
304: Response response) {
305: assertEquals(200, response.getStatusCode());
306: fail("Test did not timeout");
307: }
308: });
309: }
310:
311: public void testSetUser() {
312: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
313: getTestBaseURL());
314: try {
315: builder.setUser(null);
316: } catch (NullPointerException ex) {
317: // Correct behavior, exception was thrown
318: }
319:
320: try {
321: builder.setUser("");
322: } catch (IllegalArgumentException ex) {
323: // Correct behavior, exception was thrown
324: }
325: }
326: }
|