|
| 1 | +/** |
| 2 | + * Copyright 2019 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as fetchTypes from 'node-fetch'; // For types only. |
| 18 | +import * as testTraceModule from '../trace'; |
| 19 | +import * as assert from 'assert'; |
| 20 | +import {describeInterop} from '../utils'; |
| 21 | +import {Express4} from '../web-frameworks/express'; |
| 22 | +import {Express4Secure} from '../web-frameworks/express-secure'; |
| 23 | +import {Agent} from 'https'; |
| 24 | +import {SpanKind} from '../../src/trace'; |
| 25 | + |
| 26 | +// Server abstraction class definitions. These are borrowed from web framework |
| 27 | +// tests -- which are useful because they already expose a Promise API. |
| 28 | +const servers = { |
| 29 | + http: Express4, |
| 30 | + https: Express4Secure, |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * This test is needed because @google-cloud/common uses node-fetch under the |
| 35 | + * covers, so there is a possibility that we miss the opportunity to patch |
| 36 | + * http/https core modules. This occurs when the user requires `node-fetch`, |
| 37 | + * and never transitively requires (one of) `http` or `https` outside of |
| 38 | + * `node-fetch`, because then the plugin loader will never get the chance to |
| 39 | + * hook into a `http` or `https` module require. |
| 40 | + */ |
| 41 | +describeInterop<typeof fetchTypes & typeof fetchTypes.default>( |
| 42 | + 'node-fetch', |
| 43 | + fixture => { |
| 44 | + before(() => { |
| 45 | + testTraceModule.setPluginLoaderForTest(); |
| 46 | + testTraceModule.setCLSForTest(); |
| 47 | + }); |
| 48 | + |
| 49 | + after(() => { |
| 50 | + testTraceModule.setPluginLoaderForTest(testTraceModule.TestPluginLoader); |
| 51 | + testTraceModule.setCLSForTest(testTraceModule.TestCLS); |
| 52 | + }); |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + testTraceModule.clearTraceData(); |
| 56 | + }); |
| 57 | + |
| 58 | + for (const protocol of Object.keys(servers) as Array< |
| 59 | + keyof typeof servers |
| 60 | + >) { |
| 61 | + it(`works with the Trace Agent, ${protocol}`, async () => { |
| 62 | + // Set up a server. To preserve the condition described in the top-level |
| 63 | + // description of this test, we ensure that this constructor is called |
| 64 | + // before the Trace Agent is started, so that the Trace Agent never has |
| 65 | + // an opportunity to patch http or https upon user require. |
| 66 | + const server = new servers[protocol](); |
| 67 | + // Require node-fetch once before starting the Trace Agent. We do this |
| 68 | + // in lieu of letting it be required when the Trace Agent is started, |
| 69 | + // because we've mocked out the Trace Writer instance that would |
| 70 | + // require node-fetch in typical usage. |
| 71 | + fixture.require(); |
| 72 | + const tracer = testTraceModule.start(); |
| 73 | + const fetch = fixture.require(); |
| 74 | + |
| 75 | + // Set up the server. |
| 76 | + server.addHandler({ |
| 77 | + path: '/', |
| 78 | + hasResponse: true, |
| 79 | + fn: async () => ({statusCode: 200, message: 'OK'}), |
| 80 | + }); |
| 81 | + const port = server.listen(0); |
| 82 | + |
| 83 | + // Allow self-signed certificates. |
| 84 | + let agent: Agent | undefined; |
| 85 | + if (protocol === 'https') { |
| 86 | + agent = new Agent({ |
| 87 | + rejectUnauthorized: false, |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + // Make a request against the above server. |
| 93 | + await tracer.runInRootSpan({name: 'outer'}, async span => { |
| 94 | + assert.ok(tracer.isRealSpan(span)); |
| 95 | + const response = await fetch(`${protocol}://localhost:${port}`, { |
| 96 | + agent, |
| 97 | + }); |
| 98 | + assert.strictEqual(await response.text(), 'OK'); |
| 99 | + span.endSpan(); |
| 100 | + }); |
| 101 | + |
| 102 | + // Get the trace that represents the root span from above.. |
| 103 | + const traces = testTraceModule.getOneTrace(trace => |
| 104 | + trace.spans.some(span => span.name === 'outer') |
| 105 | + ); |
| 106 | + // There should be an HTTP client span. |
| 107 | + assert.ok( |
| 108 | + traces.spans.some(span => span.kind === SpanKind.RPC_CLIENT) |
| 109 | + ); |
| 110 | + } finally { |
| 111 | + server.shutdown(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
| 116 | +); |
0 commit comments