Path: blob/trunk/javascript/webdriver/test/http/xhrclient_test.js
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617goog.require('goog.testing.MockControl');18goog.require('goog.testing.PropertyReplacer');19goog.require('goog.testing.jsunit');20goog.require('goog.userAgent');21goog.require('webdriver.http.Request');22goog.require('webdriver.http.XhrClient');23goog.require('webdriver.test.testutil');2425function shouldRunTests() {26return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);27}2829// Alias for readability.30var callbackHelper = webdriver.test.testutil.callbackHelper;3132function FakeXhr() {}33FakeXhr.prototype.status = 200;34FakeXhr.prototype.responseText = '';35FakeXhr.prototype.open = function() {};36FakeXhr.prototype.send = function() {};37FakeXhr.prototype.getAllResponseHeaders = function() {};38FakeXhr.prototype.setRequestHeader = function() {};3940var URL = 'http://localhost:4444/wd/hub';41var REQUEST = new webdriver.http.Request('GET', '/foo');4243var control = new goog.testing.MockControl();44var stubs = new goog.testing.PropertyReplacer();45var mockClient, mockXhr;4647function setUp() {48mockClient = control.createStrictMock(webdriver.http.Client);49mockXhr = control.createStrictMock(FakeXhr);50stubs.set(goog.global, 'XMLHttpRequest', function() {51return mockXhr;52});53}5455function tearDown() {56control.$tearDown();57stubs.reset();58}5960function setXhr(value) {61stubs.set(goog.global, 'XMLHttpRequest', value);62}6364function expectRequest(mockXhr) {65mockXhr.open(REQUEST.method, URL + REQUEST.path, true);66for (var header in REQUEST.headers) {67mockXhr.setRequestHeader(header, REQUEST.headers[header]);68}69return mockXhr.send(JSON.stringify(REQUEST.data));70}7172function testXhrClient_whenUnableToSendARequest() {73expectRequest(mockXhr).$does(function() {74mockXhr.onerror();75});76control.$replayAll();7778return new webdriver.http.XhrClient(URL)79.send(REQUEST)80.then(fail, function() {81control.$verifyAll();82});83}8485function testXhrClient_parsesResponseHeaders_windows() {86expectRequest(mockXhr).$does(function() {87mockXhr.status = 200;88mockXhr.responseText = '';89mockXhr.onload();90});91mockXhr.getAllResponseHeaders().$returns([92'a:b',93'c: d',94'e :f',95'g : h'96].join('\r\n'));97control.$replayAll();9899return new webdriver.http.XhrClient(URL)100.send(REQUEST)101.then(function(response) {102assertEquals(200, response.status);103assertEquals('', response.body);104105webdriver.test.testutil.assertObjectEquals({106'a': 'b',107'c': 'd',108'e': 'f',109'g': 'h'110}, response.headers);111112control.$verifyAll();113});114}115116function testXhrClient_parsesResponseHeaders_unix() {117expectRequest(mockXhr).$does(function() {118mockXhr.status = 200;119mockXhr.responseText = '';120mockXhr.onload();121});122mockXhr.getAllResponseHeaders().$returns([123'a:b',124'c: d',125'e :f',126'g : h'127].join('\n'));128control.$replayAll();129130return new webdriver.http.XhrClient(URL)131.send(REQUEST)132.then(function(response) {133assertEquals(200, response.status);134assertEquals('', response.body);135136webdriver.test.testutil.assertObjectEquals({137'a': 'b',138'c': 'd',139'e': 'f',140'g': 'h'141}, response.headers);142143control.$verifyAll();144});145}146147function testXhrClient_handlesResponsesWithNoHeaders() {148expectRequest(mockXhr).$does(function() {149mockXhr.status = 200;150mockXhr.responseText = '';151mockXhr.onload();152});153mockXhr.getAllResponseHeaders().$returns('');154control.$replayAll();155156return new webdriver.http.XhrClient(URL)157.send(REQUEST)158.then(function(response) {159assertEquals(200, response.status);160assertEquals('', response.body);161162webdriver.test.testutil.assertObjectEquals({}, response.headers);163164control.$verifyAll();165});166}167168function testXhrClient_stripsNullCharactersFromResponseBody() {169expectRequest(mockXhr).$does(function() {170mockXhr.status = 200;171mockXhr.responseText = '\x00foo\x00\x00bar\x00';172mockXhr.onload();173});174mockXhr.getAllResponseHeaders().$returns('');175control.$replayAll();176177return new webdriver.http.XhrClient(URL)178.send(REQUEST)179.then(function(response) {180assertEquals(200, response.status);181assertEquals('foobar', response.body);182webdriver.test.testutil.assertObjectEquals({}, response.headers);183control.$verifyAll();184});185}186187188