|
| 1 | +/* |
| 2 | + * Copyright 2024 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +// This provides an in-process http server to use in place of |
| 9 | +// collector.newrelic.com. It allows for custom handlers so that test specific |
| 10 | +// assertions can be made. |
| 11 | + |
| 12 | +const https = require('node:https') |
| 13 | +const querystring = require('node:querystring') |
| 14 | +const helper = require('./agent_helper') |
| 15 | +const fakeCert = require('./fake-cert') |
| 16 | + |
| 17 | +class Collector { |
| 18 | + #handlers = new Map() |
| 19 | + #server |
| 20 | + #address |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this.#server = https.createServer({ |
| 24 | + key: fakeCert.privateKey, |
| 25 | + cert: fakeCert.certificate |
| 26 | + }) |
| 27 | + this.#server.on('request', (req, res) => { |
| 28 | + const qs = querystring.decode(req.url.slice(req.url.indexOf('?') + 1)) |
| 29 | + const handler = this.#handlers.get(qs.method) |
| 30 | + if (typeof handler !== 'function') { |
| 31 | + res.writeHead(500) |
| 32 | + return res.end('handler not found: ' + req.url) |
| 33 | + } |
| 34 | + |
| 35 | + res.json = function ({ payload, code = 200 }) { |
| 36 | + this.writeHead(code, { 'content-type': 'application/json' }) |
| 37 | + this.end(JSON.stringify(payload)) |
| 38 | + } |
| 39 | + |
| 40 | + handler.isDone = true |
| 41 | + handler(req, res) |
| 42 | + }) |
| 43 | + |
| 44 | + // We don't need this server keeping the process alive. |
| 45 | + this.#server.unref() |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * A configuration object that can be passed to an "agent" instance so that |
| 50 | + * the agent will communicate with this test server instead of the real |
| 51 | + * server. |
| 52 | + * |
| 53 | + * Important: the `.listen` method must be invoked first in order to have |
| 54 | + * the `host` and `port` defined. |
| 55 | + * |
| 56 | + * @returns {object} |
| 57 | + */ |
| 58 | + get agentConfig() { |
| 59 | + return { |
| 60 | + host: this.host, |
| 61 | + port: this.port, |
| 62 | + license_key: 'testing', |
| 63 | + certificates: [this.cert] |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * The host the server is listening on. |
| 69 | + * |
| 70 | + * @returns {string} |
| 71 | + */ |
| 72 | + get host() { |
| 73 | + return this.#address?.address |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * The port number the server is listening on. |
| 78 | + * |
| 79 | + * @returns {number} |
| 80 | + */ |
| 81 | + get port() { |
| 82 | + return this.#address?.port |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * A copy of the public certificate used to secure the server. Use this |
| 87 | + * like `new Agent({ certificates: [collector.cert] })`. |
| 88 | + * |
| 89 | + * @returns {string} |
| 90 | + */ |
| 91 | + get cert() { |
| 92 | + return fakeCert.certificate |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * The most basic `agent_settings` handler. Useful when you do not need to |
| 97 | + * customize the handler. |
| 98 | + * |
| 99 | + * @returns {function} |
| 100 | + */ |
| 101 | + get agentSettingsHandler() { |
| 102 | + return function (req, res) { |
| 103 | + res.json({ payload: { return_value: [] } }) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * The most basic `preconnect` handler. Useful when you do not need to |
| 109 | + * customize the handler. |
| 110 | + * |
| 111 | + * @returns {function} |
| 112 | + */ |
| 113 | + get preconnectHandler() { |
| 114 | + const host = this.host |
| 115 | + const port = this.port |
| 116 | + return function (req, res) { |
| 117 | + res.json({ |
| 118 | + payload: { |
| 119 | + return_value: { |
| 120 | + redirect_host: `${host}:${port}`, |
| 121 | + security_policies: {} |
| 122 | + } |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Adds a new handler for the provided endpoint. |
| 130 | + * |
| 131 | + * @param {string} endpoint A string like |
| 132 | + * `/agent_listener/invoke_raw_method?method=preconnect`. Notice that a query |
| 133 | + * string with the `method` parameter is present. This is required, as the |
| 134 | + * value of `method` will be used to look up the handler when receiving |
| 135 | + * requests. |
| 136 | + * @param {function} handler A typical `(req, res) => {}` handler. For |
| 137 | + * convenience, `res` is extended with a `json({ payload, code = 200 })` |
| 138 | + * method for easily sending JSON responses. |
| 139 | + */ |
| 140 | + addHandler(endpoint, handler) { |
| 141 | + const qs = querystring.decode(endpoint.slice(endpoint.indexOf('?') + 1)) |
| 142 | + this.#handlers.set(qs.method, handler) |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Shutdown the server and forcefully close all current connections. |
| 147 | + */ |
| 148 | + close() { |
| 149 | + this.#server.closeAllConnections() |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Determine if a handler has been invoked. |
| 154 | + * |
| 155 | + * @param {string} method Name of the method to check, e.g. "preconnect". |
| 156 | + * @returns {boolean} |
| 157 | + */ |
| 158 | + isDone(method) { |
| 159 | + return this.#handlers.get(method)?.isDone === true |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Start the server listening for requests. |
| 164 | + * |
| 165 | + * @returns {Promise<object>} Returns a standard server address object. |
| 166 | + */ |
| 167 | + async listen() { |
| 168 | + let address |
| 169 | + await new Promise((resolve, reject) => { |
| 170 | + this.#server.listen(0, '127.0.0.1', (err) => { |
| 171 | + if (err) { |
| 172 | + return reject(err) |
| 173 | + } |
| 174 | + address = this.#server.address() |
| 175 | + resolve() |
| 176 | + }) |
| 177 | + }) |
| 178 | + |
| 179 | + this.#address = address |
| 180 | + |
| 181 | + // Add handlers for the required agent startup connections. These should |
| 182 | + // be overwritten by tests that exercise the startup phase, but adding these |
| 183 | + // stubs makes it easier to test other connection events. |
| 184 | + this.addHandler(helper.generateCollectorPath('preconnect', 42), this.preconnectHandler) |
| 185 | + this.addHandler(helper.generateCollectorPath('connect', 42), (req, res) => { |
| 186 | + res.json({ payload: { return_value: { agent_run_id: 42 } } }) |
| 187 | + }) |
| 188 | + this.addHandler(helper.generateCollectorPath('agent_settings', 42), this.agentSettingsHandler) |
| 189 | + |
| 190 | + return address |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = Collector |
0 commit comments