Path: blob/trunk/javascript/webdriver/http/xhrclient.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.1617/** @fileoverview A XHR client. */1819goog.provide('webdriver.http.XhrClient');2021goog.require('goog.Promise');22goog.require('goog.net.XmlHttp');23goog.require('webdriver.http.Client');24goog.require('webdriver.http.Response');25262728/**29* A HTTP client that sends requests using XMLHttpRequests.30* @param {string} url URL for the WebDriver server to send commands to.31* @constructor32* @implements {webdriver.http.Client}33*/34webdriver.http.XhrClient = function(url) {3536/** @private {string} */37this.url_ = url;38};394041/** @override */42webdriver.http.XhrClient.prototype.send = function(request) {43var url = this.url_ + request.path;44return new goog.Promise(function(fulfill, reject) {45var xhr = /** @type {!XMLHttpRequest} */ (goog.net.XmlHttp());46xhr.open(request.method, url, true);4748xhr.onload = function() {49fulfill(webdriver.http.Response.fromXmlHttpRequest(xhr));50};5152xhr.onerror = function() {53reject(Error([54'Unable to send request: ', request.method, ' ', url,55'\nOriginal request:\n', request56].join('')));57};5859for (var header in request.headers) {60xhr.setRequestHeader(header, request.headers[header] + '');61}6263xhr.send(JSON.stringify(request.data));64});65};666768