1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 function testGet200() {
29 '/testcases/test_file_1.txt', 'GET', null, null, // url, method, data, reqHeaders[]
30 200, '1', null); // expected status, responseText, responseHeaders[]
33 function testPost200() {
37 var headers = [["Name1", "Value1"],
39 var expectedHeaders = getExpectedEchoHeaders(headers);
41 doRequest('testcases/cgi/echo_request.py', 'POST', data, headers, 200, data,
45 function testPost302_200() {
46 // A POST that gets redirected should GET the new location
50 var expectedHeaders = [["echo-Method", "GET"]];
52 doRequest('/testcases/cgi/server_redirect.py?location=/testcases/cgi/echo_request.py', 'POST', data,
53 null, 200, null, expectedHeaders);
56 function testGet404() {
58 doRequest('nosuchfile___', 'GET', null, null, 404, null, null);
61 function testGet302_200() {
63 doRequest('testcases/cgi/server_redirect.py?location=/testcases/test_file_1.txt', 'GET', null,
64 null, 200, '1', null);
67 function testGet302_404() {
69 doRequest('testcases/cgi/server_redirect.py?location=nosuchfile___', 'GET', null, null,
73 function testGetNoCrossOrigin() {
74 assertError(function() {
75 doRequest('http://www.google.com/', 'GET', null, null, 0, null, null);
79 function testGet302NoCrossOrigin() {
81 var headers = [["location", "http://www.google.com/ncr"]];
82 // "www.google.com/ncr" means "no country redirect"
83 doRequest('testcases/cgi/server_redirect.py?location=http://www.google.com/ncr',
84 'GET', null, null, 302, "", headers);
87 function testRequestDisallowedHeaders() {
88 var headers = [["Referer", "http://somewhere.else.com/"]];
89 assertError(function() {
90 doRequest('should_fail', 'GET', null, headers, null, null, null);
94 function testRequestReuse() {
97 var reusedRequest = google.gears.factory.create('beta.httprequest');
104 var url = 'testcases/cgi/echo_request.py?' + numGot;
105 reusedRequest.onreadystatechange = function() {
106 if (reusedRequest.readyState == 4) {
107 assertEqual(200, reusedRequest.status);
110 if (numGot == numToGet) {
117 reusedRequest.open('GET', url, true);
118 reusedRequest.send(null);
122 function testGetCapturedResource() {
125 var myLocalServer = google.gears.factory.create('beta.localserver');
126 // We don't delete and recreate the store or captured url to avoid
127 // interfering with this same test running in the other thread.
128 var storeName = 'testGet_CapturedResource';
129 myLocalServer.removeStore(storeName);
130 var myStore = myLocalServer.createStore(storeName);
131 var url = 'testcases/cgi/echo_request.py?httprequest_a_captured_url';
134 myStore.capture(url, function(url, success, id) {
135 assert(success, 'Expected captured to succeed');
136 doRequest(url, 'GET', null, null, 200, null, null);
140 function testGet_BinaryResponse() {
141 // TODO(michaeln): do something reasonable with binary responses
144 function testNullOnReadyStateChange() {
145 var nullHandlerRequest = google.gears.factory.create('beta.httprequest');
146 nullHandlerRequest.onreadystatechange = function() {};
147 nullHandlerRequest.onreadystatechange = null;
148 nullHandlerRequest.open('GET', 'nosuchfile___');
149 nullHandlerRequest.send();
151 var unsetHandlerRequest = google.gears.factory.create('beta.httprequest');
152 unsetHandlerRequest.open('GET', 'nosuchfile___');
153 unsetHandlerRequest.send();
156 // Generates header name value pairs testcases/cgi/echo_requests.py will respond
157 // with for the given request headers.
158 function getExpectedEchoHeaders(requestHeaders) {
159 var echoHeaders = [];
160 for (var i = 0; i < requestHeaders.length; ++i) {
161 var name = 'echo-' + requestHeaders[i][0];
162 var value = requestHeaders[i][1];
163 echoHeaders.push([name, value]);
168 // A helper that initiates a request and examines the response.
169 function doRequest(url, method, data, requestHeaders, expectedStatus,
170 expectedResponse, expectedHeaders) {
171 var request = google.gears.factory.create('beta.httprequest');
173 request.onreadystatechange = handleReadyStateChange;
174 request.open(method, url, true);
176 if (requestHeaders) {
177 for (var i = 0; i < requestHeaders.length; ++i) {
178 request.setRequestHeader(requestHeaders[i][0],
179 requestHeaders[i][1]);
186 function handleReadyStateChange() {
187 var state = request.readyState;
188 assert(state >= 0 && state <= 4, 'Invalid readyState value, ' + state);
195 'onreadystatechange called multiple times with readyState == 4');
198 // Make sure we can fetch all properties
199 assert(isNumber(request.status),
200 'Should be able to get status after request');
201 assert(isString(request.statusText),
202 'Should be able to get statusText after request');
203 assert(isString(request.getAllResponseHeaders()),
204 'Should be able to call getAllResponseHeaders() after request');
205 assert(isString(request.responseText),
206 'Should be able to get responseText after request');
208 // see if we got what we expected to get
209 if (expectedStatus != null) {
210 assertEqual(expectedStatus, request.status,
211 'Wrong value for status property');
214 if (expectedHeaders != null) {
215 for (var i = 0; i < expectedHeaders.length; ++i) {
216 var name = expectedHeaders[i][0];
217 var expectedValue = expectedHeaders[i][1];
218 var actualValue = request.getResponseHeader(name);
219 assertEqual(expectedValue, actualValue,
220 'Wrong value for header "%s"'.subs(name));
224 if (expectedResponse != null) {
225 assertEqual(expectedResponse, request.responseText, 'Wrong responseText');