Path: blob/trunk/javascript/selenium-webdriver/http/util.js
1864 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/**18* @fileoverview Various HTTP utilities.19*/2021'use strict'2223const Executor = require('./index').Executor24const HttpClient = require('./index').HttpClient25const HttpRequest = require('./index').Request26const Command = require('../lib/command').Command27const CommandName = require('../lib/command').Name28const error = require('../lib/error')2930/**31* Queries a WebDriver server for its current status.32* @param {string} url Base URL of the server to query.33* @return {!Promise<!Object>} A promise that resolves with34* a hash of the server status.35*/36function getStatus(url) {37const client = new HttpClient(url)38const executor = new Executor(client)39const command = new Command(CommandName.GET_SERVER_STATUS)40return executor.execute(command)41}4243class CancellationError {}4445/**46* Waits for a WebDriver server to be healthy and accepting requests.47* @param {string} url Base URL of the server to query.48* @param {number} timeout How long to wait for the server.49* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:50* if resolved before the server is ready, the wait will be terminated51* early with a {@link CancellationError}.52* @return {!Promise} A promise that will resolve when the server is ready, or53* if the wait is cancelled.54*/55function waitForServer(url, timeout, opt_cancelToken) {56return new Promise((onResolve, onReject) => {57let start = Date.now()5859let done = false60let resolve = (status) => {61done = true62onResolve(status)63}64let reject = (err) => {65done = true66onReject(err)67}6869if (opt_cancelToken) {70opt_cancelToken.then((_) => reject(new CancellationError()))71}7273checkServerStatus()7475function checkServerStatus() {76return getStatus(url).then((status) => resolve(status), onError)77}7879function onError(e) {80// Some servers don't support the status command. If they are able to81// response with an error, then can consider the server ready.82if (e instanceof error.UnsupportedOperationError) {83resolve({})84return85}8687if (Date.now() - start > timeout) {88reject(Error('Timed out waiting for the WebDriver server at ' + url))89} else {90setTimeout(function () {91if (!done) {92checkServerStatus()93}94}, 50)95}96}97})98}99100/**101* Polls a URL with GET requests until it returns a 2xx response or the102* timeout expires.103* @param {string} url The URL to poll.104* @param {number} timeout How long to wait, in milliseconds.105* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:106* if resolved before the a 2xx response is received, the wait will be107* terminated early with a {@link CancellationError}.108* @return {!Promise} A promise that will resolve when a 2xx is received from109* the given URL, or if the wait is cancelled.110*/111function waitForUrl(url, timeout, opt_cancelToken) {112return new Promise((onResolve, onReject) => {113let client = new HttpClient(url)114let request = new HttpRequest('GET', '')115let start = Date.now()116117let done = false118let resolve = () => {119done = true120onResolve()121}122let reject = (err) => {123done = true124onReject(err)125}126127if (opt_cancelToken) {128opt_cancelToken.then((_) => reject(new CancellationError()))129}130131testUrl()132133function testUrl() {134client.send(request).then(onResponse, onError)135}136137function onError() {138if (Date.now() - start > timeout) {139reject(Error('Timed out waiting for the URL to return 2xx: ' + url))140} else {141setTimeout(function () {142if (!done) {143testUrl()144}145}, 50)146}147}148149function onResponse(response) {150if (done) {151return152}153if (response.status > 199 && response.status < 300) {154resolve()155return156}157onError()158}159})160}161162// PUBLIC API163module.exports.getStatus = getStatus164module.exports.CancellationError = CancellationError165module.exports.waitForServer = waitForServer166module.exports.waitForUrl = waitForUrl167168169