@@ -6,6 +6,7 @@ const http = require('http');
6
6
7
7
const app = fastify ( ) ;
8
8
const port = 3030 ;
9
+ const port2 = 3040 ;
9
10
10
11
Sentry . setupFastifyErrorHandler ( app ) ;
11
12
@@ -17,20 +18,22 @@ app.get('/test-param/:param', function (req, res) {
17
18
res . send ( { paramWas : req . params . param } ) ;
18
19
} ) ;
19
20
20
- app . get ( '/test-inbound-headers' , function ( req , res ) {
21
+ app . get ( '/test-inbound-headers/:id ' , function ( req , res ) {
21
22
const headers = req . headers ;
22
23
23
- res . send ( { headers } ) ;
24
+ res . send ( { headers, id : req . params . id } ) ;
24
25
} ) ;
25
26
26
- app . get ( '/test-outgoing-http' , async function ( req , res ) {
27
- const data = await makeHttpRequest ( 'http://localhost:3030/test-inbound-headers' ) ;
27
+ app . get ( '/test-outgoing-http/:id' , async function ( req , res ) {
28
+ const id = req . params . id ;
29
+ const data = await makeHttpRequest ( `http://localhost:3030/test-inbound-headers/${ id } ` ) ;
28
30
29
31
res . send ( data ) ;
30
32
} ) ;
31
33
32
- app . get ( '/test-outgoing-fetch' , async function ( req , res ) {
33
- const response = await fetch ( 'http://localhost:3030/test-inbound-headers' ) ;
34
+ app . get ( '/test-outgoing-fetch/:id' , async function ( req , res ) {
35
+ const id = req . params . id ;
36
+ const response = await fetch ( `http://localhost:3030/test-inbound-headers/${ id } ` ) ;
34
37
const data = await response . json ( ) ;
35
38
36
39
res . send ( data ) ;
@@ -56,8 +59,48 @@ app.get('/test-exception', async function (req, res) {
56
59
throw new Error ( 'This is an exception' ) ;
57
60
} ) ;
58
61
62
+ app . get ( '/test-outgoing-fetch-external-allowed' , async function ( req , res ) {
63
+ const fetchResponse = await fetch ( `http://localhost:${ port2 } /external-allowed` ) ;
64
+ const data = await fetchResponse . json ( ) ;
65
+
66
+ res . send ( data ) ;
67
+ } ) ;
68
+
69
+ app . get ( '/test-outgoing-fetch-external-disallowed' , async function ( req , res ) {
70
+ const fetchResponse = await fetch ( `http://localhost:${ port2 } /external-disallowed` ) ;
71
+ const data = await fetchResponse . json ( ) ;
72
+
73
+ res . send ( data ) ;
74
+ } ) ;
75
+
76
+ app . get ( '/test-outgoing-http-external-allowed' , async function ( req , res ) {
77
+ const data = await makeHttpRequest ( `http://localhost:${ port2 } /external-allowed` ) ;
78
+ res . send ( data ) ;
79
+ } ) ;
80
+
81
+ app . get ( '/test-outgoing-http-external-disallowed' , async function ( req , res ) {
82
+ const data = await makeHttpRequest ( `http://localhost:${ port2 } /external-disallowed` ) ;
83
+ res . send ( data ) ;
84
+ } ) ;
85
+
59
86
app . listen ( { port : port } ) ;
60
87
88
+ // A second app so we can test header propagation between external URLs
89
+ const app2 = fastify ( ) ;
90
+ app2 . get ( '/external-allowed' , function ( req , res ) {
91
+ const headers = req . headers ;
92
+
93
+ res . send ( { headers, route : '/external-allowed' } ) ;
94
+ } ) ;
95
+
96
+ app2 . get ( '/external-disallowed' , function ( req , res ) {
97
+ const headers = req . headers ;
98
+
99
+ res . send ( { headers, route : '/external-disallowed' } ) ;
100
+ } ) ;
101
+
102
+ app2 . listen ( { port : port2 } ) ;
103
+
61
104
function makeHttpRequest ( url ) {
62
105
return new Promise ( resolve => {
63
106
const data = [ ] ;
@@ -67,9 +110,16 @@ function makeHttpRequest(url) {
67
110
httpRes . on ( 'data' , chunk => {
68
111
data . push ( chunk ) ;
69
112
} ) ;
113
+ httpRes . on ( 'error' , error => {
114
+ resolve ( { error : error . message , url } ) ;
115
+ } ) ;
70
116
httpRes . on ( 'end' , ( ) => {
71
- const json = JSON . parse ( Buffer . concat ( data ) . toString ( ) ) ;
72
- resolve ( json ) ;
117
+ try {
118
+ const json = JSON . parse ( Buffer . concat ( data ) . toString ( ) ) ;
119
+ resolve ( json ) ;
120
+ } catch {
121
+ resolve ( { data : Buffer . concat ( data ) . toString ( ) , url } ) ;
122
+ }
73
123
} ) ;
74
124
} )
75
125
. end ( ) ;
0 commit comments