@@ -6,6 +6,7 @@ const http = require('http');
66
77const app = fastify ( ) ;
88const port = 3030 ;
9+ const port2 = 3040 ;
910
1011Sentry . setupFastifyErrorHandler ( app ) ;
1112
@@ -17,20 +18,22 @@ app.get('/test-param/:param', function (req, res) {
1718 res . send ( { paramWas : req . params . param } ) ;
1819} ) ;
1920
20- app . get ( '/test-inbound-headers' , function ( req , res ) {
21+ app . get ( '/test-inbound-headers/:id ' , function ( req , res ) {
2122 const headers = req . headers ;
2223
23- res . send ( { headers } ) ;
24+ res . send ( { headers, id : req . params . id } ) ;
2425} ) ;
2526
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 } ` ) ;
2830
2931 res . send ( data ) ;
3032} ) ;
3133
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 } ` ) ;
3437 const data = await response . json ( ) ;
3538
3639 res . send ( data ) ;
@@ -56,8 +59,48 @@ app.get('/test-exception', async function (req, res) {
5659 throw new Error ( 'This is an exception' ) ;
5760} ) ;
5861
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+
5986app . listen ( { port : port } ) ;
6087
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+
61104function makeHttpRequest ( url ) {
62105 return new Promise ( resolve => {
63106 const data = [ ] ;
@@ -67,9 +110,16 @@ function makeHttpRequest(url) {
67110 httpRes . on ( 'data' , chunk => {
68111 data . push ( chunk ) ;
69112 } ) ;
113+ httpRes . on ( 'error' , error => {
114+ resolve ( { error : error . message , url } ) ;
115+ } ) ;
70116 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+ }
73123 } ) ;
74124 } )
75125 . end ( ) ;
0 commit comments