@@ -25,7 +25,16 @@ describe('Plugin', () => {
2525 } )
2626
2727 describe ( 'without configuration' , ( ) => {
28- before ( ( ) => agent . load ( [ 'koa' , 'http' ] , [ { } , { client : false } ] ) )
28+ before ( ( ) => agent . load (
29+ [ 'koa' , 'http' ] ,
30+ [ { } , { client : false } ] ,
31+ {
32+ // this is needed to test the client IP header configuration and must be done before loading the tracer
33+ // initially since we can't change the tracer config after it's loaded
34+ clientIpEnabled : true ,
35+ clientIpHeader : 'x-custom-client-ip-header'
36+ } )
37+ )
2938
3039 after ( ( ) => agent . close ( { ritmReset : false } ) )
3140
@@ -216,6 +225,58 @@ describe('Plugin', () => {
216225 } )
217226 } )
218227
228+ it ( 'should handle client IP header configuration' , done => {
229+ const app = new Koa ( )
230+
231+ app . use ( async ( ctx ) => {
232+ ctx . body = ''
233+ } )
234+
235+ appListener = app . listen ( 0 , 'localhost' , ( ) => {
236+ const port = appListener . address ( ) . port
237+
238+ agent
239+ . use ( traces => {
240+ const spans = sort ( traces [ 0 ] )
241+ expect ( spans [ 0 ] . meta ) . to . have . property ( 'http.client_ip' , '8.8.8.8' )
242+ } )
243+ . then ( done )
244+ . catch ( done )
245+
246+ axios . get ( `http://localhost:${ port } /user` , {
247+ headers : {
248+ 'x-custom-client-ip-header' : '8.8.8.8'
249+ }
250+ } ) . catch ( done )
251+ } )
252+ } )
253+
254+ it ( 'should not add client IP tag when header is missing or a different header is used' , done => {
255+ const app = new Koa ( )
256+
257+ app . use ( async ( ctx ) => {
258+ ctx . body = ''
259+ } )
260+
261+ appListener = app . listen ( 0 , 'localhost' , ( ) => {
262+ const port = appListener . address ( ) . port
263+
264+ agent
265+ . use ( traces => {
266+ const spans = sort ( traces [ 0 ] )
267+ expect ( spans [ 0 ] . meta ) . to . not . have . property ( 'http.client_ip' )
268+ } )
269+ . then ( done )
270+ . catch ( done )
271+
272+ axios . get ( `http://localhost:${ port } /user` , {
273+ headers : {
274+ 'x-other-custom-client-ip-header' : '8.8.8.8'
275+ }
276+ } ) . catch ( done )
277+ } )
278+ } )
279+
219280 withVersions ( 'koa' , 'koa-route' , routerVersion => {
220281 let koaRouter
221282
0 commit comments