Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/webdriver/http/xhrclient.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
/** @fileoverview A XHR client. */
19
20
goog.provide('webdriver.http.XhrClient');
21
22
goog.require('goog.Promise');
23
goog.require('goog.net.XmlHttp');
24
goog.require('webdriver.http.Client');
25
goog.require('webdriver.http.Response');
26
27
28
29
/**
30
* A HTTP client that sends requests using XMLHttpRequests.
31
* @param {string} url URL for the WebDriver server to send commands to.
32
* @constructor
33
* @implements {webdriver.http.Client}
34
*/
35
webdriver.http.XhrClient = function(url) {
36
37
/** @private {string} */
38
this.url_ = url;
39
};
40
41
42
/** @override */
43
webdriver.http.XhrClient.prototype.send = function(request) {
44
var url = this.url_ + request.path;
45
return new goog.Promise(function(fulfill, reject) {
46
var xhr = /** @type {!XMLHttpRequest} */ (goog.net.XmlHttp());
47
xhr.open(request.method, url, true);
48
49
xhr.onload = function() {
50
fulfill(webdriver.http.Response.fromXmlHttpRequest(xhr));
51
};
52
53
xhr.onerror = function() {
54
reject(Error([
55
'Unable to send request: ', request.method, ' ', url,
56
'\nOriginal request:\n', request
57
].join('')));
58
};
59
60
for (var header in request.headers) {
61
xhr.setRequestHeader(header, request.headers[header] + '');
62
}
63
64
xhr.send(JSON.stringify(request.data));
65
});
66
};
67
68