1515 */
1616
1717
18- import { CoreTracer , RootSpan , Span , SpanEventListener , TracerConfig } from '@opencensus/core' ;
19- import { logger } from '@opencensus/core' ;
18+ import { CoreTracer , logger , RootSpan , Span , SpanEventListener } from '@opencensus/core' ;
2019import * as assert from 'assert' ;
2120import * as http2 from 'http2' ;
22- import * as mocha from 'mocha' ;
2321import * as semver from 'semver' ;
24- import * as shimmer from 'shimmer' ;
2522
2623import { plugin } from '../src/' ;
2724import { Http2Plugin } from '../src/' ;
@@ -39,7 +36,7 @@ class RootSpanVerifier implements SpanEventListener {
3936
4037function assertSpanAttributes (
4138 span : Span , httpStatusCode : number , httpMethod : string , hostName : string ,
42- path : string , userAgent : string ) {
39+ path : string , userAgent ? : string ) {
4340 assert . strictEqual (
4441 span . status . code , Http2Plugin . parseResponseStatus ( httpStatusCode ) ) ;
4542 assert . strictEqual (
@@ -48,8 +45,10 @@ function assertSpanAttributes(
4845 span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_METHOD ] , httpMethod ) ;
4946 assert . strictEqual ( span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_PATH ] , path ) ;
5047 assert . strictEqual ( span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_ROUTE ] , path ) ;
51- assert . strictEqual (
52- span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_USER_AGENT ] , userAgent ) ;
48+ if ( userAgent ) {
49+ assert . strictEqual (
50+ span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_USER_AGENT ] , userAgent ) ;
51+ }
5352 assert . strictEqual (
5453 span . attributes [ Http2Plugin . ATTRIBUTE_HTTP_STATUS_CODE ] ,
5554 `${ httpStatusCode } ` ) ;
@@ -100,12 +99,14 @@ describe('Http2Plugin', () => {
10099 before ( ( ) => {
101100 tracer . registerSpanEventListener ( rootSpanVerifier ) ;
102101
103- plugin . enable ( http2 , tracer , VERSION , { } , null ) ;
102+ plugin . enable ( http2 , tracer , VERSION , { } , '' ) ;
104103 server = http2 . createServer ( ) ;
105104 server . on ( 'stream' , ( stream , requestHeaders ) => {
106- const statusCode = requestHeaders [ ':path' ] . length > 1 ?
107- + requestHeaders [ ':path' ] . slice ( 1 ) :
108- 200 ;
105+ const path = requestHeaders [ ':path' ] ;
106+ let statusCode = 200 ;
107+ if ( path && path . length > 1 ) {
108+ statusCode = isNaN ( Number ( path . slice ( 1 ) ) ) ? 200 : Number ( path . slice ( 1 ) ) ;
109+ }
109110 stream . respond ( { ':status' : statusCode , 'content-type' : 'text/plain' } ) ;
110111 stream . end ( `${ statusCode } ` ) ;
111112 } ) ;
@@ -139,7 +140,7 @@ describe('Http2Plugin', () => {
139140 rootSpanVerifier . endedRootSpans [ 1 ] . name . indexOf ( testPath ) >= 0 ) ;
140141
141142 const span = rootSpanVerifier . endedRootSpans [ 1 ] ;
142- assertSpanAttributes ( span , 200 , 'GET' , host , testPath , undefined ) ;
143+ assertSpanAttributes ( span , 200 , 'GET' , host , testPath ) ;
143144 } ) ;
144145 } ) ;
145146
@@ -160,8 +161,7 @@ describe('Http2Plugin', () => {
160161 0 ) ;
161162
162163 const span = rootSpanVerifier . endedRootSpans [ 1 ] ;
163- assertSpanAttributes (
164- span , errorCode , 'GET' , host , testPath , undefined ) ;
164+ assertSpanAttributes ( span , errorCode , 'GET' , host , testPath ) ;
165165 } ) ;
166166 } ) ;
167167 } ) ;
@@ -179,8 +179,7 @@ describe('Http2Plugin', () => {
179179 assert . ok ( root . spans [ 0 ] . name . indexOf ( testPath ) >= 0 ) ;
180180 assert . strictEqual ( root . traceId , root . spans [ 0 ] . traceId ) ;
181181 const span = root . spans [ 0 ] ;
182- assertSpanAttributes (
183- span , statusCode , 'GET' , host , testPath , undefined ) ;
182+ assertSpanAttributes ( span , statusCode , 'GET' , host , testPath ) ;
184183 } ) ;
185184 } ) ;
186185 } ) ;
@@ -201,8 +200,7 @@ describe('Http2Plugin', () => {
201200 assert . strictEqual ( root . traceId , root . spans [ 0 ] . traceId ) ;
202201
203202 const span = root . spans [ 0 ] ;
204- assertSpanAttributes (
205- span , errorCode , 'GET' , host , testPath , undefined ) ;
203+ assertSpanAttributes ( span , errorCode , 'GET' , host , testPath ) ;
206204 } ) ;
207205 } ) ;
208206 } ) ;
0 commit comments