Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/http/util_test.js
2885 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
'use strict'
19
20
const assert = require('node:assert')
21
const http = require('node:http')
22
23
const error = require('selenium-webdriver/lib/error')
24
const util = require('selenium-webdriver/http/util')
25
26
describe('selenium-webdriver/http/util', function () {
27
let server, baseUrl
28
29
let status, value, responseCode
30
31
function startServer(done) {
32
if (server) return done()
33
34
server = http.createServer(function (_req, res) {
35
const data = JSON.stringify({ status: status, value: value })
36
res.writeHead(responseCode, {
37
'Content-Type': 'application/json; charset=utf-8',
38
'Content-Length': Buffer.byteLength(data, 'utf8'),
39
})
40
res.end(data)
41
})
42
43
server.listen(0, '127.0.0.1', function (e) {
44
if (e) return done(e)
45
46
const addr = server.address()
47
baseUrl = 'http://' + addr.address + ':' + addr.port
48
done()
49
})
50
}
51
52
function killServer(done) {
53
if (!server) return done()
54
server.close(done)
55
server = null
56
}
57
58
after(killServer)
59
60
beforeEach(function (done) {
61
status = 0
62
value = 'abc123'
63
responseCode = 200
64
startServer(done)
65
})
66
67
describe('#getStatus', function () {
68
it('should return value field on success', function () {
69
return util.getStatus(baseUrl).then(function (response) {
70
assert.strictEqual('abc123', response)
71
})
72
})
73
74
it('should fail if response object is not success', function () {
75
status = 1
76
return util.getStatus(baseUrl).then(
77
function () {
78
throw Error('expected a failure')
79
},
80
function (err) {
81
assert.ok(err instanceof error.WebDriverError)
82
assert.strictEqual(err.code, error.WebDriverError.code)
83
assert.strictEqual(err.message, value)
84
},
85
)
86
})
87
88
it('should fail if the server is not listening', function (done) {
89
killServer(function (e) {
90
if (e) return done(e)
91
92
util.getStatus(baseUrl).then(
93
function () {
94
done(Error('expected a failure'))
95
},
96
function () {
97
// Expected.
98
done()
99
},
100
)
101
})
102
})
103
104
it('should fail if HTTP status is not 200', function () {
105
status = 1
106
responseCode = 404
107
return util.getStatus(baseUrl).then(
108
function () {
109
throw Error('expected a failure')
110
},
111
function (err) {
112
assert.ok(err instanceof error.WebDriverError)
113
assert.strictEqual(err.code, error.WebDriverError.code)
114
assert.strictEqual(err.message, value)
115
},
116
)
117
})
118
})
119
120
describe('#waitForServer', function () {
121
it('resolves when server is ready', function () {
122
status = 1
123
setTimeout(function () {
124
status = 0
125
}, 50)
126
return util.waitForServer(baseUrl, 100)
127
})
128
129
it('should fail if server does not become ready', function () {
130
status = 1
131
return util.waitForServer(baseUrl, 50).then(
132
function () {
133
throw Error('Expected to time out')
134
},
135
function () {},
136
)
137
})
138
139
it('can cancel wait', function () {
140
status = 1
141
let cancel = new Promise((resolve) => {
142
setTimeout(() => resolve(), 50)
143
})
144
return util.waitForServer(baseUrl, 200, cancel).then(
145
() => {
146
throw Error('Did not expect to succeed!')
147
},
148
(e) => assert.ok(e instanceof util.CancellationError),
149
)
150
})
151
})
152
153
describe('#waitForUrl', function () {
154
it('succeeds when URL returns 2xx', function () {
155
responseCode = 404
156
setTimeout(function () {
157
responseCode = 200
158
}, 50)
159
160
return util.waitForUrl(baseUrl, 200)
161
})
162
163
it('fails if URL always returns 4xx', function () {
164
responseCode = 404
165
166
return util.waitForUrl(baseUrl, 50).then(
167
() => assert.fail('Expected to time out'),
168
() => true,
169
)
170
})
171
172
it('fails if cannot connect to server', function () {
173
return new Promise((resolve, reject) => {
174
killServer(function (e) {
175
if (e) return reject(e)
176
177
util.waitForUrl(baseUrl, 50).then(
178
function () {
179
reject(Error('Expected to time out'))
180
},
181
function () {
182
resolve()
183
},
184
)
185
})
186
})
187
})
188
189
it('can cancel wait', function () {
190
responseCode = 404
191
let cancel = new Promise((resolve) => {
192
setTimeout(() => resolve(), 50)
193
})
194
return util.waitForUrl(baseUrl, 200, cancel).then(
195
() => {
196
throw Error('Did not expect to succeed!')
197
},
198
(e) => assert.ok(e instanceof util.CancellationError),
199
)
200
})
201
})
202
})
203
204