Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/webdriver/test/http/xhrclient_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.Request');
23
goog.require('webdriver.http.XhrClient');
24
goog.require('webdriver.test.testutil');
25
26
function shouldRunTests() {
27
return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);
28
}
29
30
// Alias for readability.
31
var callbackHelper = webdriver.test.testutil.callbackHelper;
32
33
function FakeXhr() {}
34
FakeXhr.prototype.status = 200;
35
FakeXhr.prototype.responseText = '';
36
FakeXhr.prototype.open = function() {};
37
FakeXhr.prototype.send = function() {};
38
FakeXhr.prototype.getAllResponseHeaders = function() {};
39
FakeXhr.prototype.setRequestHeader = function() {};
40
41
var URL = 'http://localhost:4444/wd/hub';
42
var REQUEST = new webdriver.http.Request('GET', '/foo');
43
44
var control = new goog.testing.MockControl();
45
var stubs = new goog.testing.PropertyReplacer();
46
var mockClient, mockXhr;
47
48
function setUp() {
49
mockClient = control.createStrictMock(webdriver.http.Client);
50
mockXhr = control.createStrictMock(FakeXhr);
51
stubs.set(goog.global, 'XMLHttpRequest', function() {
52
return mockXhr;
53
});
54
}
55
56
function tearDown() {
57
control.$tearDown();
58
stubs.reset();
59
}
60
61
function setXhr(value) {
62
stubs.set(goog.global, 'XMLHttpRequest', value);
63
}
64
65
function expectRequest(mockXhr) {
66
mockXhr.open(REQUEST.method, URL + REQUEST.path, true);
67
for (var header in REQUEST.headers) {
68
mockXhr.setRequestHeader(header, REQUEST.headers[header]);
69
}
70
return mockXhr.send(JSON.stringify(REQUEST.data));
71
}
72
73
function testXhrClient_whenUnableToSendARequest() {
74
expectRequest(mockXhr).$does(function() {
75
mockXhr.onerror();
76
});
77
control.$replayAll();
78
79
return new webdriver.http.XhrClient(URL)
80
.send(REQUEST)
81
.then(fail, function() {
82
control.$verifyAll();
83
});
84
}
85
86
function testXhrClient_parsesResponseHeaders_windows() {
87
expectRequest(mockXhr).$does(function() {
88
mockXhr.status = 200;
89
mockXhr.responseText = '';
90
mockXhr.onload();
91
});
92
mockXhr.getAllResponseHeaders().$returns([
93
'a:b',
94
'c: d',
95
'e :f',
96
'g : h'
97
].join('\r\n'));
98
control.$replayAll();
99
100
return new webdriver.http.XhrClient(URL)
101
.send(REQUEST)
102
.then(function(response) {
103
assertEquals(200, response.status);
104
assertEquals('', response.body);
105
106
webdriver.test.testutil.assertObjectEquals({
107
'a': 'b',
108
'c': 'd',
109
'e': 'f',
110
'g': 'h'
111
}, response.headers);
112
113
control.$verifyAll();
114
});
115
}
116
117
function testXhrClient_parsesResponseHeaders_unix() {
118
expectRequest(mockXhr).$does(function() {
119
mockXhr.status = 200;
120
mockXhr.responseText = '';
121
mockXhr.onload();
122
});
123
mockXhr.getAllResponseHeaders().$returns([
124
'a:b',
125
'c: d',
126
'e :f',
127
'g : h'
128
].join('\n'));
129
control.$replayAll();
130
131
return new webdriver.http.XhrClient(URL)
132
.send(REQUEST)
133
.then(function(response) {
134
assertEquals(200, response.status);
135
assertEquals('', response.body);
136
137
webdriver.test.testutil.assertObjectEquals({
138
'a': 'b',
139
'c': 'd',
140
'e': 'f',
141
'g': 'h'
142
}, response.headers);
143
144
control.$verifyAll();
145
});
146
}
147
148
function testXhrClient_handlesResponsesWithNoHeaders() {
149
expectRequest(mockXhr).$does(function() {
150
mockXhr.status = 200;
151
mockXhr.responseText = '';
152
mockXhr.onload();
153
});
154
mockXhr.getAllResponseHeaders().$returns('');
155
control.$replayAll();
156
157
return new webdriver.http.XhrClient(URL)
158
.send(REQUEST)
159
.then(function(response) {
160
assertEquals(200, response.status);
161
assertEquals('', response.body);
162
163
webdriver.test.testutil.assertObjectEquals({}, response.headers);
164
165
control.$verifyAll();
166
});
167
}
168
169
function testXhrClient_stripsNullCharactersFromResponseBody() {
170
expectRequest(mockXhr).$does(function() {
171
mockXhr.status = 200;
172
mockXhr.responseText = '\x00foo\x00\x00bar\x00';
173
mockXhr.onload();
174
});
175
mockXhr.getAllResponseHeaders().$returns('');
176
control.$replayAll();
177
178
return new webdriver.http.XhrClient(URL)
179
.send(REQUEST)
180
.then(function(response) {
181
assertEquals(200, response.status);
182
assertEquals('foobar', response.body);
183
webdriver.test.testutil.assertObjectEquals({}, response.headers);
184
control.$verifyAll();
185
});
186
}
187
188