Path: blob/trunk/javascript/webdriver/test/http/corsclient_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.CorsClient');22goog.require('webdriver.http.Request');23goog.require('webdriver.test.testutil');2425// Alias for readability.26var callbackHelper = webdriver.test.testutil.callbackHelper;2728function FakeXhr() {}29FakeXhr.prototype.status = 200;30FakeXhr.prototype.responseText = '';31FakeXhr.prototype.withCredentials = false;32FakeXhr.prototype.open = function() {};33FakeXhr.prototype.send = function() {};34FakeXhr.prototype.setRequestHeader = function() {};3536var URL = 'http://localhost:4444/wd/hub';37var REQUEST = new webdriver.http.Request('GET', '/foo');3839var control = new goog.testing.MockControl();40var stubs = new goog.testing.PropertyReplacer();41var mockClient, mockXhr;4243function shouldRunTests() {44return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);45}4647function setUp() {48mockClient = control.createStrictMock(webdriver.http.Client);49mockXhr = control.createStrictMock(FakeXhr);50mockXhr.status = 200;51mockXhr.responseText = '';52mockXhr.withCredentials = false;53setXhr(mockXhr);54}5556function tearDown() {57control.$tearDown();58stubs.reset();59}6061function setXhr(value) {62stubs.set(goog.global, 'XMLHttpRequest', function() {63return value;64});65setXdr();66}6768function setXdr(opt_value) {69stubs.set(goog.global, 'XDomainRequest', opt_value);70}7172function expectRequest(mockXhr) {73mockXhr.open('POST', URL + '/xdrpc', true);74return mockXhr.send(JSON.stringify({75'method': REQUEST.method,76'path': REQUEST.path,77'data': REQUEST.data78}));79}8081function testDetectsWhenCorsIsAvailable() {82setXhr(undefined);83assertFalse(webdriver.http.CorsClient.isAvailable());84setXhr();85assertFalse(webdriver.http.CorsClient.isAvailable());86setXhr({withCredentials: null});87assertFalse(webdriver.http.CorsClient.isAvailable());88setXhr({withCredentials: true});89assertTrue(webdriver.http.CorsClient.isAvailable());90setXhr();91setXdr(goog.nullFunction);92assertTrue(webdriver.http.CorsClient.isAvailable());93}9495function testCorsClient_whenUnableToSendARequest() {96expectRequest(mockXhr).$does(function() {97mockXhr.onerror();98});99control.$replayAll();100101return new webdriver.http.CorsClient(URL)102.send(REQUEST)103.then(fail, function() {104control.$verifyAll();105});106}107108function testCorsClient_handlesResponsesWithNoHeaders() {109expectRequest(mockXhr).$does(function() {110mockXhr.status = 200;111mockXhr.responseText = '';112mockXhr.onload();113});114control.$replayAll();115116return new webdriver.http.CorsClient(URL)117.send(REQUEST)118.then(function(response) {119assertEquals(200, response.status);120assertEquals('', response.body);121122webdriver.test.testutil.assertObjectEquals({}, response.headers);123control.$verifyAll();124});125}126127function testCorsClient_stripsNullCharactersFromResponseBody() {128expectRequest(mockXhr).$does(function() {129mockXhr.status = 200;130mockXhr.responseText = '\x00foo\x00\x00bar\x00';131mockXhr.onload();132});133control.$replayAll();134135return new webdriver.http.CorsClient(URL)136.send(REQUEST)137.then(function(response) {138assertEquals(200, response.status);139assertEquals('foobar', response.body);140webdriver.test.testutil.assertObjectEquals({}, response.headers);141control.$verifyAll();142});143}144145146