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: *
023: */
024: public class ResponseTest extends GWTTestCase {
025: private static final int TEST_FINISH_DELAY = 10000;
026:
027: private static RequestBuilder getHTTPRequestBuilder() {
028: return getHTTPRequestBuilder(getTestBaseURL());
029: }
030:
031: private static RequestBuilder getHTTPRequestBuilder(String testURL) {
032: return new RequestBuilder(RequestBuilder.GET, testURL);
033: }
034:
035: private static String getTestBaseURL() {
036: return GWT.getModuleBaseURL() + "testResponse/";
037: }
038:
039: private static native boolean isSafari() /*-{
040: var ua = navigator.userAgent.toLowerCase();
041: if (ua.indexOf('safari') != -1) {
042: return true;
043: }
044: return false;
045: }-*/;
046:
047: private static void raiseUnexpectedException(Throwable exception) {
048: fail("Unexpected exception: " + exception.toString());
049: }
050:
051: public String getModuleName() {
052: return "com.google.gwt.http.ResponseTest";
053: }
054:
055: /**
056: * Test method for {@link com.google.gwt.http.client.Response#getStatusCode()}.
057: */
058: public void testGetStatusCode() {
059: executeTest(new RequestCallback() {
060: public void onError(Request request, Throwable exception) {
061: fail();
062: }
063:
064: public void onResponseReceived(Request request,
065: Response response) {
066: assertEquals(200, response.getStatusCode());
067: finishTest();
068: }
069: });
070: }
071:
072: /**
073: * Test method for {@link com.google.gwt.http.client.Response#getStatusCode()}.
074: * DISABLED - we use a newer version of webkit in our hosted browser than
075: * Safari's; we can't reliably distinguish between the two so we disabled the
076: * test
077: */
078: public void disabledTestGetStatusCode_Safari() {
079: if (!isSafari()) {
080: // Only test this on Safari
081: return;
082: }
083:
084: executeTest(getHTTPRequestBuilder(getTestBaseURL()
085: + "noResponseText"), new RequestCallback() {
086: public void onError(Request request, Throwable exception) {
087: if (exception instanceof RuntimeException) {
088: finishTest();
089: } else {
090: raiseUnexpectedException(exception);
091: }
092: }
093:
094: public void onResponseReceived(Request request,
095: Response response) {
096: try {
097: int statusCode = response.getStatusCode();
098: fail("Unexpected RuntimeException from getStatusCode()");
099: } catch (RuntimeException ex) {
100: }
101:
102: finishTest();
103: }
104: });
105: }
106:
107: /**
108: * Test method for {@link com.google.gwt.http.client.Response#getStatusText()}.
109: */
110: public void testGetStatusText() {
111: executeTest(new RequestCallback() {
112: public void onError(Request request, Throwable exception) {
113: if (exception instanceof RuntimeException) {
114:
115: } else {
116: raiseUnexpectedException(exception);
117: }
118: }
119:
120: public void onResponseReceived(Request request,
121: Response response) {
122: assertEquals("OK", response.getStatusText());
123: finishTest();
124: }
125: });
126: }
127:
128: /**
129: * Test method for {@link com.google.gwt.http.client.Response#getStatusText()}.
130: */
131: public void disabledTestGetStatusText_Safari() {
132: if (!isSafari()) {
133: // Only test this on Safari
134: return;
135: }
136:
137: executeTest(getHTTPRequestBuilder(getTestBaseURL()
138: + "noResponseText"), new RequestCallback() {
139: public void onError(Request request, Throwable exception) {
140: if (exception instanceof RuntimeException) {
141: finishTest();
142: } else {
143: raiseUnexpectedException(exception);
144: }
145: }
146:
147: public void onResponseReceived(Request request,
148: Response response) {
149: try {
150: String statusText = response.getStatusText();
151: fail("Unexpected RuntimeException from getStatusText()");
152: } catch (RuntimeException ex) {
153: }
154:
155: finishTest();
156: }
157: });
158: }
159:
160: private void executeTest(RequestBuilder builder,
161: RequestCallback callback) {
162: delayTestFinish(TEST_FINISH_DELAY);
163:
164: try {
165: builder.sendRequest(null, callback);
166: } catch (RequestException e) {
167: fail(e.getMessage());
168: }
169: }
170:
171: private void executeTest(RequestCallback callback) {
172: executeTest(getHTTPRequestBuilder(), callback);
173: }
174: }
|