-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fixed superagent versioned tests (#2190)
- Loading branch information
1 parent
f14dcb1
commit 6a27d21
Showing
3 changed files
with
79 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const http = require('http') | ||
|
||
module.exports = async function testServer() { | ||
const server = http.createServer() | ||
|
||
server.on('request', (req, res) => { | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end('"ok"') | ||
}) | ||
|
||
let address = await new Promise((resolve, reject) => { | ||
server.listen(0, '127.0.0.1', (error) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
return resolve(server.address()) | ||
}) | ||
}) | ||
|
||
address = `http://${address.address}:${address.port}` | ||
|
||
return { address, server, stopServer } | ||
|
||
async function stopServer() { | ||
await new Promise((resolve, reject) => { | ||
if (server.closeAllConnections) { | ||
// Node.js 16 does not support this method. | ||
server.closeAllConnections() | ||
} | ||
server.close((error) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
return resolve() | ||
}) | ||
}) | ||
} | ||
} |