1+
2+ let listener ;
3+ let server ;
4+ let goodUrl ;
5+ let receivedHeaders ;
6+
7+
8+ before ( ( ) => {
9+ // Create an HTTP server to receive requests.
10+ const http = require ( 'http' ) ;
11+ server = http . createServer ( ( req , res ) => {
12+ receivedHeaders = { ...req . headers } ;
13+ // Respond with something
14+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
15+ res . end ( 'Example reply\n' ) ;
16+ } ) ;
17+
18+ listener = server . listen ( ) ;
19+ const address = server . address ( ) ;
20+ const host = address . family === 'IPv6' ? `[${ address . address } ]` : address . address ;
21+ goodUrl = `http://${ host } :${ address . port } /test` ;
22+ } ) ;
23+
24+ after ( ( ) => {
25+ // close http server
26+ listener . close ( ) ;
27+ } ) ;
28+
129describe ( 'Integration tests' , function ( ) {
230 const chai = require ( 'chai' ) ;
331 const sinonChai = require ( 'sinon-chai' ) ;
@@ -16,12 +44,11 @@ describe('Integration tests', function () {
1644 const fetchModule = require ( 'node-fetch' ) ;
1745 const Segment = AWSXray . Segment ;
1846 const Subsegment = AWSXray . Subsegment ;
19- const goodUrl = 'https://example.org' ;
2047 const badUrl = 'http://localhost:1' ;
2148
2249 const hasGlobalFetch = globalThis . fetch !== undefined ;
2350
24- describe ( 'captureFetchModule ' , function ( ) {
51+ describe ( 'captureFetchGlobal ' , function ( ) {
2552
2653 let saveGlobalFetch ;
2754 let mockSegment ;
@@ -43,6 +70,7 @@ describe('Integration tests', function () {
4370 stubAddFetchRequestData = sandbox . stub ( mockSubsegment , 'addFetchRequestData' ) ;
4471 stubAddErrorFlag = sandbox . stub ( mockSubsegment , 'addErrorFlag' ) ;
4572 stubClose = sandbox . stub ( mockSubsegment , 'close' ) ;
73+ receivedHeaders = { } ;
4674 } ) ;
4775
4876 afterEach ( function ( ) {
@@ -65,6 +93,24 @@ describe('Integration tests', function () {
6593 stubClose . should . have . been . calledOnce ;
6694 } ) ;
6795
96+ it ( 'adds header' , async function ( ) {
97+ const spyCallback = sandbox . spy ( ) ;
98+ const fetch = captureFetchGlobal ( true , spyCallback ) ;
99+ const response = await fetch ( goodUrl , { headers : {
100+ 'foo' : 'bar'
101+ } } ) ;
102+ response . status . should . equal ( 200 ) ;
103+ receivedHeaders . should . to . have . property ( 'x-amzn-trace-id' ) ;
104+ receivedHeaders . should . to . have . property ( 'foo' , 'bar' ) ;
105+ ( await response . text ( ) ) . should . contain ( 'Example' ) ;
106+ stubIsAutomaticMode . should . have . been . called ;
107+ stubAddNewSubsegment . should . have . been . calledOnce ;
108+ stubResolveSegment . should . have . been . calledOnce ;
109+ stubAddFetchRequestData . should . have . been . calledOnce ;
110+ stubAddErrorFlag . should . not . have . been . calledOnce ;
111+ stubClose . should . have . been . calledOnce ;
112+ } ) ;
113+
68114 it ( 'sets error flag on failed fetch when global fetch exists' , async function ( ) {
69115 const spyCallback = sandbox . spy ( ) ;
70116 const fetch = captureFetchGlobal ( true , spyCallback ) ;
@@ -116,6 +162,7 @@ describe('Integration tests', function () {
116162 const fetch = captureFetchModule ( fetchModule , true , spyCallback ) ;
117163 const response = await fetch ( goodUrl ) ;
118164 response . status . should . equal ( 200 ) ;
165+ receivedHeaders . should . to . have . property ( 'x-amzn-trace-id' ) ;
119166 ( await response . text ( ) ) . should . contain ( 'Example' ) ;
120167 stubIsAutomaticMode . should . have . been . called ;
121168 stubAddNewSubsegment . should . have . been . calledOnce ;
0 commit comments