@@ -4,21 +4,34 @@ import { Event, Request, User } from '../src';
4
4
import { parseRequest } from '../src/handlers' ;
5
5
6
6
describe ( 'parseRequest' , ( ) => {
7
- const mockReq = {
8
- body : 'foo' ,
9
- cookies : { test : 'test' } ,
10
- headers : {
11
- host : 'mattrobenolt.com' ,
12
- } ,
13
- method : 'POST' ,
14
- url : '/some/path?key=value' ,
15
- user : {
16
- custom_property : 'foo' ,
17
-
18
- id : 123 ,
19
- username : 'tobias' ,
20
- } ,
21
- } ;
7
+ let mockReq : { [ key : string ] : any } ;
8
+
9
+ beforeEach ( ( ) => {
10
+ mockReq = {
11
+ body : 'foo' ,
12
+ cookies : { test : 'test' } ,
13
+ headers : {
14
+ host : 'mattrobenolt.com' ,
15
+ } ,
16
+ method : 'POST' ,
17
+ originalUrl : '/some/originalUrl?key=value' ,
18
+ route : {
19
+ path : '/path' ,
20
+ stack : [
21
+ {
22
+ name : 'routeHandler' ,
23
+ } ,
24
+ ] ,
25
+ } ,
26
+ url : '/some/url?key=value' ,
27
+ user : {
28
+ custom_property : 'foo' ,
29
+
30
+ id : 123 ,
31
+ username : 'tobias' ,
32
+ } ,
33
+ } ;
34
+ } ) ;
22
35
23
36
describe ( 'parseRequest.contexts runtime' , ( ) => {
24
37
test ( 'runtime name must contain node' , ( ) => {
@@ -121,4 +134,34 @@ describe('parseRequest', () => {
121
134
expect ( parseRequest ( { } , { ...mockReq , method : 'HEAD' } , { } ) . request ) . not . toHaveProperty ( 'data' ) ;
122
135
} ) ;
123
136
} ) ;
137
+
138
+ describe ( 'parseRequest.transaction property' , ( ) => {
139
+ test ( 'extracts method and full route path by default from `originalUrl`' , ( ) => {
140
+ const parsedRequest : Event = parseRequest ( { } , mockReq ) ;
141
+ expect ( parsedRequest . transaction ) . toEqual ( 'POST|/some/originalUrl' ) ;
142
+ } ) ;
143
+
144
+ test ( 'extracts method and full route path by default from `url` if `originalUrl` is not present' , ( ) => {
145
+ delete mockReq . originalUrl ;
146
+ const parsedRequest : Event = parseRequest ( { } , mockReq ) ;
147
+ expect ( parsedRequest . transaction ) . toEqual ( 'POST|/some/url' ) ;
148
+ } ) ;
149
+
150
+ test ( 'fallback to method and `route.path` if previous attempts failed' , ( ) => {
151
+ delete mockReq . originalUrl ;
152
+ delete mockReq . url ;
153
+ const parsedRequest : Event = parseRequest ( { } , mockReq ) ;
154
+ expect ( parsedRequest . transaction ) . toEqual ( 'POST|/path' ) ;
155
+ } ) ;
156
+
157
+ test ( 'can extract path only instead if configured' , ( ) => {
158
+ const parsedRequest : Event = parseRequest ( { } , mockReq , { transaction : 'path' } ) ;
159
+ expect ( parsedRequest . transaction ) . toEqual ( '/some/originalUrl' ) ;
160
+ } ) ;
161
+
162
+ test ( 'can extract handler name instead if configured' , ( ) => {
163
+ const parsedRequest : Event = parseRequest ( { } , mockReq , { transaction : 'handler' } ) ;
164
+ expect ( parsedRequest . transaction ) . toEqual ( 'routeHandler' ) ;
165
+ } ) ;
166
+ } ) ;
124
167
} ) ;
0 commit comments