Path: blob/trunk/javascript/selenium-webdriver/test/http/util_test.js
2885 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'use strict'1819const assert = require('node:assert')20const http = require('node:http')2122const error = require('selenium-webdriver/lib/error')23const util = require('selenium-webdriver/http/util')2425describe('selenium-webdriver/http/util', function () {26let server, baseUrl2728let status, value, responseCode2930function startServer(done) {31if (server) return done()3233server = http.createServer(function (_req, res) {34const data = JSON.stringify({ status: status, value: value })35res.writeHead(responseCode, {36'Content-Type': 'application/json; charset=utf-8',37'Content-Length': Buffer.byteLength(data, 'utf8'),38})39res.end(data)40})4142server.listen(0, '127.0.0.1', function (e) {43if (e) return done(e)4445const addr = server.address()46baseUrl = 'http://' + addr.address + ':' + addr.port47done()48})49}5051function killServer(done) {52if (!server) return done()53server.close(done)54server = null55}5657after(killServer)5859beforeEach(function (done) {60status = 061value = 'abc123'62responseCode = 20063startServer(done)64})6566describe('#getStatus', function () {67it('should return value field on success', function () {68return util.getStatus(baseUrl).then(function (response) {69assert.strictEqual('abc123', response)70})71})7273it('should fail if response object is not success', function () {74status = 175return util.getStatus(baseUrl).then(76function () {77throw Error('expected a failure')78},79function (err) {80assert.ok(err instanceof error.WebDriverError)81assert.strictEqual(err.code, error.WebDriverError.code)82assert.strictEqual(err.message, value)83},84)85})8687it('should fail if the server is not listening', function (done) {88killServer(function (e) {89if (e) return done(e)9091util.getStatus(baseUrl).then(92function () {93done(Error('expected a failure'))94},95function () {96// Expected.97done()98},99)100})101})102103it('should fail if HTTP status is not 200', function () {104status = 1105responseCode = 404106return util.getStatus(baseUrl).then(107function () {108throw Error('expected a failure')109},110function (err) {111assert.ok(err instanceof error.WebDriverError)112assert.strictEqual(err.code, error.WebDriverError.code)113assert.strictEqual(err.message, value)114},115)116})117})118119describe('#waitForServer', function () {120it('resolves when server is ready', function () {121status = 1122setTimeout(function () {123status = 0124}, 50)125return util.waitForServer(baseUrl, 100)126})127128it('should fail if server does not become ready', function () {129status = 1130return util.waitForServer(baseUrl, 50).then(131function () {132throw Error('Expected to time out')133},134function () {},135)136})137138it('can cancel wait', function () {139status = 1140let cancel = new Promise((resolve) => {141setTimeout(() => resolve(), 50)142})143return util.waitForServer(baseUrl, 200, cancel).then(144() => {145throw Error('Did not expect to succeed!')146},147(e) => assert.ok(e instanceof util.CancellationError),148)149})150})151152describe('#waitForUrl', function () {153it('succeeds when URL returns 2xx', function () {154responseCode = 404155setTimeout(function () {156responseCode = 200157}, 50)158159return util.waitForUrl(baseUrl, 200)160})161162it('fails if URL always returns 4xx', function () {163responseCode = 404164165return util.waitForUrl(baseUrl, 50).then(166() => assert.fail('Expected to time out'),167() => true,168)169})170171it('fails if cannot connect to server', function () {172return new Promise((resolve, reject) => {173killServer(function (e) {174if (e) return reject(e)175176util.waitForUrl(baseUrl, 50).then(177function () {178reject(Error('Expected to time out'))179},180function () {181resolve()182},183)184})185})186})187188it('can cancel wait', function () {189responseCode = 404190let cancel = new Promise((resolve) => {191setTimeout(() => resolve(), 50)192})193return util.waitForUrl(baseUrl, 200, cancel).then(194() => {195throw Error('Did not expect to succeed!')196},197(e) => assert.ok(e instanceof util.CancellationError),198)199})200})201})202203204