@@ -12,9 +12,12 @@ if (process.env.GITHUB_ACTIONS === 'true' && process.platform === 'win32') {
1212}
1313
1414const agent = require ( '../../../..' ) . start ( {
15+ serviceName : 'test-fastify' ,
1516 captureExceptions : false ,
16- metricsInterval : 0 ,
17- centralConfig : false
17+ metricsInterval : '0s' ,
18+ centralConfig : false ,
19+ apmServerVersion : '8.7.0' ,
20+ captureBody : 'all'
1821} )
1922
2023const isFastifyIncompat = require ( '../../../_is_fastify_incompat' ) ( )
@@ -69,6 +72,61 @@ test('transaction name', function (t) {
6972 } )
7073} )
7174
75+ test ( 'captureBody' , function ( t ) {
76+ t . plan ( 9 )
77+
78+ const postData = JSON . stringify ( { foo : 'bar' } )
79+
80+ resetAgent ( data => {
81+ t . strictEqual ( data . transactions . length , 1 )
82+ var trans = data . transactions [ 0 ]
83+ t . strictEqual ( trans . name , 'POST /postSomeData' , 'transaction.name' )
84+ t . strictEqual ( trans . type , 'request' , 'transaction.type' )
85+ t . strictEqual ( trans . result , 'HTTP 2xx' , 'transaction.result' )
86+ t . strictEqual ( trans . context . request . method , 'POST' , 'transaction.context.request.method' )
87+ t . equal ( trans . context . request . body , postData , 'transaction.context.request.body' )
88+ fastify . close ( )
89+ } )
90+
91+ var fastify = Fastify ( )
92+
93+ fastify . post ( '/postSomeData' , ( request , reply ) => {
94+ reply . send ( 'your data has been posted' )
95+ } )
96+
97+ fastify . listen ( { port : 0 } , function ( err ) {
98+ t . error ( err )
99+
100+ // build the URL manually as older versions of fastify doesn't supply it as
101+ // an argument to the callback
102+ const port = fastify . server . address ( ) . port
103+ const cReq = http . request (
104+ 'http://localhost:' + port + '/postSomeData' ,
105+ {
106+ method : 'POST' ,
107+ hostname : 'localhost' ,
108+ port,
109+ headers : {
110+ 'Content-Type' : 'application/json' ,
111+ 'Content-Length' : Buffer . byteLength ( postData )
112+ }
113+ } ,
114+ function ( res ) {
115+ t . strictEqual ( res . statusCode , 200 )
116+ res . on ( 'data' , function ( chunk ) {
117+ t . strictEqual ( chunk . toString ( ) , 'your data has been posted' )
118+ } )
119+ res . on ( 'end' , function ( ) {
120+ agent . flush ( )
121+ t . end ( )
122+ } )
123+ }
124+ )
125+ cReq . write ( postData )
126+ cReq . end ( )
127+ } )
128+ } )
129+
72130function resetAgent ( cb ) {
73131 agent . _instrumentation . testReset ( )
74132 agent . _transport = mockClient ( 1 , cb )
0 commit comments