Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/webdriver/test/http/corsclient_test.js
2868 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
goog.require('goog.testing.MockControl');
19
goog.require('goog.testing.PropertyReplacer');
20
goog.require('goog.testing.jsunit');
21
goog.require('goog.userAgent');
22
goog.require('webdriver.http.CorsClient');
23
goog.require('webdriver.http.Request');
24
goog.require('webdriver.test.testutil');
25
26
// Alias for readability.
27
var callbackHelper = webdriver.test.testutil.callbackHelper;
28
29
function FakeXhr() {}
30
FakeXhr.prototype.status = 200;
31
FakeXhr.prototype.responseText = '';
32
FakeXhr.prototype.withCredentials = false;
33
FakeXhr.prototype.open = function() {};
34
FakeXhr.prototype.send = function() {};
35
FakeXhr.prototype.setRequestHeader = function() {};
36
37
var URL = 'http://localhost:4444/wd/hub';
38
var REQUEST = new webdriver.http.Request('GET', '/foo');
39
40
var control = new goog.testing.MockControl();
41
var stubs = new goog.testing.PropertyReplacer();
42
var mockClient, mockXhr;
43
44
function shouldRunTests() {
45
return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);
46
}
47
48
function setUp() {
49
mockClient = control.createStrictMock(webdriver.http.Client);
50
mockXhr = control.createStrictMock(FakeXhr);
51
mockXhr.status = 200;
52
mockXhr.responseText = '';
53
mockXhr.withCredentials = false;
54
setXhr(mockXhr);
55
}
56
57
function tearDown() {
58
control.$tearDown();
59
stubs.reset();
60
}
61
62
function setXhr(value) {
63
stubs.set(goog.global, 'XMLHttpRequest', function() {
64
return value;
65
});
66
setXdr();
67
}
68
69
function setXdr(opt_value) {
70
stubs.set(goog.global, 'XDomainRequest', opt_value);
71
}
72
73
function expectRequest(mockXhr) {
74
mockXhr.open('POST', URL + '/xdrpc', true);
75
return mockXhr.send(JSON.stringify({
76
'method': REQUEST.method,
77
'path': REQUEST.path,
78
'data': REQUEST.data
79
}));
80
}
81
82
function testDetectsWhenCorsIsAvailable() {
83
setXhr(undefined);
84
assertFalse(webdriver.http.CorsClient.isAvailable());
85
setXhr();
86
assertFalse(webdriver.http.CorsClient.isAvailable());
87
setXhr({withCredentials: null});
88
assertFalse(webdriver.http.CorsClient.isAvailable());
89
setXhr({withCredentials: true});
90
assertTrue(webdriver.http.CorsClient.isAvailable());
91
setXhr();
92
setXdr(goog.nullFunction);
93
assertTrue(webdriver.http.CorsClient.isAvailable());
94
}
95
96
function testCorsClient_whenUnableToSendARequest() {
97
expectRequest(mockXhr).$does(function() {
98
mockXhr.onerror();
99
});
100
control.$replayAll();
101
102
return new webdriver.http.CorsClient(URL)
103
.send(REQUEST)
104
.then(fail, function() {
105
control.$verifyAll();
106
});
107
}
108
109
function testCorsClient_handlesResponsesWithNoHeaders() {
110
expectRequest(mockXhr).$does(function() {
111
mockXhr.status = 200;
112
mockXhr.responseText = '';
113
mockXhr.onload();
114
});
115
control.$replayAll();
116
117
return new webdriver.http.CorsClient(URL)
118
.send(REQUEST)
119
.then(function(response) {
120
assertEquals(200, response.status);
121
assertEquals('', response.body);
122
123
webdriver.test.testutil.assertObjectEquals({}, response.headers);
124
control.$verifyAll();
125
});
126
}
127
128
function testCorsClient_stripsNullCharactersFromResponseBody() {
129
expectRequest(mockXhr).$does(function() {
130
mockXhr.status = 200;
131
mockXhr.responseText = '\x00foo\x00\x00bar\x00';
132
mockXhr.onload();
133
});
134
control.$replayAll();
135
136
return new webdriver.http.CorsClient(URL)
137
.send(REQUEST)
138
.then(function(response) {
139
assertEquals(200, response.status);
140
assertEquals('foobar', response.body);
141
webdriver.test.testutil.assertObjectEquals({}, response.headers);
142
control.$verifyAll();
143
});
144
}
145
146