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: import com.google.gwt.user.client.impl.HTTPRequestImpl;
021:
022: /**
023: * TODO: document me.
024: */
025: public class RequestTest extends GWTTestCase {
026: private static final int TEST_FINISH_DELAY = 10000;
027:
028: private static String getTestBaseURL() {
029: return GWT.getModuleBaseURL() + "testRequest/";
030: }
031:
032: public String getModuleName() {
033: return "com.google.gwt.http.RequestTest";
034: }
035:
036: /**
037: * Test method for {@link com.google.gwt.http.client.Request#cancel()}.
038: */
039: public void testCancel() {
040: delayTestFinish(TEST_FINISH_DELAY);
041:
042: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
043: getTestBaseURL() + "/cancel");
044: try {
045: Request request = builder.sendRequest(null,
046: new RequestCallback() {
047: public void onResponseReceived(Request request,
048: Response response) {
049: fail("Request was canceled - no response should be received");
050: }
051:
052: public void onError(Request request,
053: Throwable exception) {
054: fail("Request was canceled - no timeout should occur");
055: }
056: });
057:
058: assertTrue(request.isPending());
059: request.cancel();
060: assertFalse(request.isPending());
061:
062: finishTest();
063: } catch (RequestException e) {
064: fail(e.getMessage());
065: }
066: }
067:
068: /**
069: * Test method for
070: * {@link com.google.gwt.http.client.Request#Request(com.google.gwt.core.client.JavaScriptObject, int, com.google.gwt.http.client.RequestCallback)}.
071: */
072: public void testRequest() {
073: RequestCallback callback = new RequestCallback() {
074: public void onResponseReceived(Request request,
075: Response response) {
076: }
077:
078: public void onError(Request request, Throwable exception) {
079: }
080: };
081:
082: try {
083: Request request = new Request(null, 0, callback);
084: fail();
085: } catch (NullPointerException ex) {
086: // Success.
087: }
088:
089: HTTPRequestImpl impl = (HTTPRequestImpl) GWT
090: .create(HTTPRequestImpl.class);
091: try {
092: Request request = new Request(impl.createXmlHTTPRequest(),
093: -1, callback);
094: fail();
095: } catch (IllegalArgumentException ex) {
096: // Success.
097: }
098:
099: try {
100: Request request = new Request(impl.createXmlHTTPRequest(),
101: -1, null);
102: fail();
103: } catch (NullPointerException ex) {
104: // Success.
105: }
106:
107: try {
108: Request request = new Request(impl.createXmlHTTPRequest(),
109: 0, callback);
110: } catch (Throwable ex) {
111: fail(ex.getMessage());
112: }
113: }
114:
115: /**
116: * Test method for {@link com.google.gwt.http.client.Request#isPending()}.
117: */
118: public void testIsPending() {
119: // delayTestFinish(TEST_FINISH_DELAY);
120:
121: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
122: getTestBaseURL() + "isPending");
123: try {
124: Request request = builder.sendRequest(null,
125: new RequestCallback() {
126: public void onResponseReceived(Request request,
127: Response response) {
128: finishTest();
129: }
130:
131: public void onError(Request request,
132: Throwable exception) {
133: finishTest();
134: }
135: });
136:
137: assertTrue(request.isPending());
138: // finishTest();
139: } catch (RequestException e) {
140: fail(e.getMessage());
141: }
142: }
143: }
|