Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/lib/test/httpserver.js
2884 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
const url = require('node:url')
23
24
const net = require('selenium-webdriver/net')
25
const portprober = require('selenium-webdriver/net/portprober')
26
const promise = require('selenium-webdriver').promise
27
28
/**
29
* Encapsulates a simple HTTP server for testing. The {@code onrequest}
30
* function should be overridden to define request handling behavior.
31
* @param {function(!http.ServerRequest, !http.ServerResponse)} requestHandler
32
* The request handler for the server.
33
* @constructor
34
*/
35
let Server = function (requestHandler) {
36
let server = http.createServer(function (req, res) {
37
requestHandler(req, res)
38
})
39
40
server.on('connection', function (stream) {
41
stream.setTimeout(4000)
42
})
43
44
/** @typedef {{port: number, address: string, family: string}} */
45
let Host // eslint-disable-line
46
47
/**
48
* Starts the server on the given port. If no port, or 0, is provided,
49
* the server will be started on a random port.
50
* @param {number=} opt_port The port to start on.
51
* @return {!Promise<Host>} A promise that will resolve
52
* with the server host when it has fully started.
53
*/
54
this.start = function (opt_port) {
55
assert(typeof opt_port !== 'function', 'start invoked with function, not port (mocha callback)?')
56
const port = opt_port || portprober.findFreePort('127.0.0.1')
57
return Promise.resolve(port)
58
.then((port) => {
59
return promise.checkedNodeCall(server.listen.bind(server, port, '127.0.0.1'))
60
})
61
.then(function () {
62
return server.address()
63
})
64
}
65
66
/**
67
* Stops the server.
68
* @return {!Promise} A promise that will resolve when the
69
* server has closed all connections.
70
*/
71
this.stop = function () {
72
return new Promise((resolve) => server.close(resolve))
73
}
74
75
/**
76
* @return {Host} This server's host info.
77
* @throws {Error} If the server is not running.
78
*/
79
this.address = function () {
80
const addr = server.address()
81
if (!addr) {
82
throw Error('There server is not running!')
83
}
84
return addr
85
}
86
87
/**
88
* return {string} The host:port of this server.
89
* @throws {Error} If the server is not running.
90
*/
91
this.host = function () {
92
return net.getLoopbackAddress() + ':' + this.address().port
93
}
94
95
/**
96
* Formats a URL for this server.
97
* @param {string=} opt_pathname The desired pathname on the server.
98
* @return {string} The formatted URL.
99
* @throws {Error} If the server is not running.
100
*/
101
this.url = function (opt_pathname) {
102
const addr = this.address()
103
const pathname = opt_pathname || ''
104
return url.format({
105
protocol: 'http',
106
hostname: net.getLoopbackAddress(),
107
port: addr.port,
108
pathname: pathname,
109
})
110
}
111
}
112
113
// PUBLIC API
114
115
exports.Server = Server
116
117