1
+ import type { EventDeliveryPayload , SessionDeliveryPayload } from '../../src/common'
2
+ import Event from '../../src/event'
1
3
import jsonPayload from '../../src/lib/json-payload'
4
+ import Session from '../../src/session'
2
5
3
6
function makeBigObject ( ) {
4
- var big : Record < string , string > = { }
5
- var i = 0
7
+ const big : Record < string , string > = { }
8
+ let i = 0
6
9
while ( JSON . stringify ( big ) . length < 2 * 10e5 ) {
7
10
big [ 'entry' + i ] = 'long repetitive string' . repeat ( 1000 )
8
11
i ++
@@ -12,91 +15,61 @@ function makeBigObject () {
12
15
13
16
describe ( 'jsonPayload.event' , ( ) => {
14
17
it ( 'safe stringifies the payload and redacts values from certain paths of the supplied keys' , ( ) => {
18
+ const event = new Event ( 'CheckoutError' , 'Failed load tickets' )
19
+ event . setUser ( '123' , '[email protected] ' , 'Jim Bug' )
20
+ event . request = { apiKey : '245b39ebd3cd3992e85bffc81c045924' }
21
+
15
22
expect ( jsonPayload . event ( {
16
- api_key : 'd145b8e5afb56516423bc4d605e45442' ,
17
- events : [
18
- {
19
- errorMessage : 'Failed load tickets' ,
20
- errorClass : 'CheckoutError' ,
21
- user : {
22
- name : 'Jim Bug' ,
23
-
24
- } ,
25
- request : {
26
- api_key : '245b39ebd3cd3992e85bffc81c045924'
27
- }
28
- }
29
- ]
30
- } , [ 'api_key' ] ) ) . toBe ( '{"api_key":"d145b8e5afb56516423bc4d605e45442","events":[{"errorMessage":"Failed load tickets","errorClass":"CheckoutError","user":{"name":"Jim Bug","email":"[email protected] "},"request":{"api_key":"[REDACTED]"}}]}' )
23
+ apiKey : 'd145b8e5afb56516423bc4d605e45442' ,
24
+ notifier : { name : 'Bugsnag' , version : '1.0.0' , url : 'https://bugsnag.com' } ,
25
+ events : [ event ]
26
+ } , [ 'apiKey' ] ) ) . toBe ( '{"apiKey":"d145b8e5afb56516423bc4d605e45442","notifier":{"name":"Bugsnag","version":"1.0.0","url":"https://bugsnag.com"},"events":[{"payloadVersion":"4","exceptions":[{"errorClass":"CheckoutError","errorMessage":"Failed load tickets","type":"browserjs","stacktrace":[],"message":"Failed load tickets"}],"severity":"warning","unhandled":false,"severityReason":{"type":"handledException"},"app":{},"device":{},"request":{"apiKey":"[REDACTED]"},"breadcrumbs":[],"metaData":{},"user":{"id":"123","email":"[email protected] ","name":"Jim Bug"},"featureFlags":[]}]}' )
31
27
} )
32
28
33
29
it ( 'strips the metaData of the first event if the payload is too large' , ( ) => {
34
- const payload = {
35
- api_key : 'd145b8e5afb56516423bc4d605e45442' ,
36
- events : [
37
- {
38
- errorMessage : 'Failed load tickets' ,
39
- errorClass : 'CheckoutError' ,
40
- user : {
41
- name : 'Jim Bug' ,
42
-
43
- } ,
44
- request : {
45
- api_key : '245b39ebd3cd3992e85bffc81c045924'
46
- } ,
47
- _metadata : { }
48
- }
49
- ]
50
- }
30
+ const event = new Event ( 'CheckoutError' , 'Failed load tickets' )
31
+ event . setUser ( '123' , '[email protected] ' , 'Jim Bug' )
32
+ event . request = { apiKey : '245b39ebd3cd3992e85bffc81c045924' }
33
+ event . _metadata = { 'big thing' : makeBigObject ( ) }
51
34
52
- payload . events [ 0 ] . _metadata = { 'big thing' : makeBigObject ( ) }
35
+ const payload : EventDeliveryPayload = {
36
+ apiKey : 'd145b8e5afb56516423bc4d605e45442' ,
37
+ notifier : { name : 'Bugsnag' , version : '1.0.0' , url : 'https://bugsnag.com' } ,
38
+ events : [ event ]
39
+ }
53
40
54
- expect ( jsonPayload . event ( payload ) ) . toBe ( '{"api_key ":"d145b8e5afb56516423bc4d605e45442","events":[{"errorMessage":"Failed load tickets","errorClass ":"CheckoutError ","user":{"name ":"Jim Bug","email ":"[email protected] " },"request":{"api_key ":"245b39ebd3cd3992e85bffc81c045924"},"_metadata": {"notifier":"WARNING!\\nSerialized payload was 2.003435MB (limit = 1MB)\\nmetadata was removed"}}]}' )
41
+ expect ( jsonPayload . event ( payload ) ) . toBe ( '{"apiKey ":"d145b8e5afb56516423bc4d605e45442","notifier":{"name":"Bugsnag","version":"1.0.0","url":"https://bugsnag.com"}," events":[{"payloadVersion":"4","exceptions":[{"errorClass":"CheckoutError"," errorMessage":"Failed load tickets","type ":"browserjs ","stacktrace":[],"message ":"Failed load tickets"}],"severity ":"warning","unhandled":false,"severityReason":{"type":"handledException" },"app":{},"device":{}," request":{"apiKey ":"245b39ebd3cd3992e85bffc81c045924"},"breadcrumbs":[],"metaData": {"notifier":"WARNING!\\nSerialized payload was 2.003764MB (limit = 1MB)\\nmetadata was removed"},"user":{"id":"123","email":"[email protected] ","name":"Jim Bug"},"featureFlags":[] }]}' )
55
42
} )
56
43
57
44
it ( 'does not attempt to strip any other data paths from the payload to reduce the size' , ( ) => {
45
+ const event1 = new Event ( 'CheckoutError' , 'Failed load tickets' )
46
+ event1 . setUser ( '123' , '[email protected] ' , 'Jim Bug' )
47
+ event1 . request = { apiKey : '245b39ebd3cd3992e85bffc81c045924' }
48
+
49
+ // Second event metadata should not be stripped, only the first
50
+ const event2 = new Event ( 'APIError' , 'Request failed' )
51
+ event2 . _metadata = { 'big thing' : makeBigObject ( ) }
52
+
58
53
const payload = {
59
- api_key : 'd145b8e5afb56516423bc4d605e45442' ,
60
- events : [
61
- {
62
- errorMessage : 'Failed load tickets' ,
63
- errorClass : 'CheckoutError' ,
64
- user : {
65
- name : 'Jim Bug' ,
66
-
67
- } ,
68
- _metadata : { }
69
- } ,
70
- {
71
- errorMessage : 'Request failed' ,
72
- errorClass : 'APIError' ,
73
- _metadata : { }
74
- }
75
- ]
54
+ apiKey : 'd145b8e5afb56516423bc4d605e45442' ,
55
+ notifier : { name : 'Bugsnag' , version : '1.0.0' , url : 'https://bugsnag.com' } ,
56
+ events : [ event1 , event2 ]
76
57
}
77
- payload . events [ 1 ] . _metadata = { 'big thing' : makeBigObject ( ) }
78
58
79
59
expect ( jsonPayload . event ( payload ) . length ) . toBeGreaterThan ( 10e5 )
80
60
} )
81
61
} )
82
62
83
63
describe ( 'jsonPayload.session' , ( ) => {
84
64
it ( 'safe stringifies the payload' , ( ) => {
85
- expect ( jsonPayload . session ( {
86
- api_key : 'd145b8e5afb56516423bc4d605e45442' ,
87
- events : [
88
- {
89
- errorMessage : 'Failed load tickets' ,
90
- errorClass : 'CheckoutError' ,
91
- user : {
92
- name : 'Jim Bug' ,
93
-
94
- } ,
95
- request : {
96
- api_key : '245b39ebd3cd3992e85bffc81c045924'
97
- }
98
- }
99
- ]
100
- } , [ 'api_key' ] ) ) . toBe ( '{"api_key":"d145b8e5afb56516423bc4d605e45442","events":[{"errorMessage":"Failed load tickets","errorClass":"CheckoutError","user":{"name":"Jim Bug","email":"[email protected] "},"request":{"api_key":"245b39ebd3cd3992e85bffc81c045924"}}]}' )
65
+ const session = new Session ( '123' , new Date ( '2012-12-21T00:00:00.0000Z' ) )
66
+ const sessionPayload : SessionDeliveryPayload = {
67
+ app : { version : '1.0.0' } ,
68
+ device : { id : '123' } ,
69
+ notifier : { name : 'Bugsnag' , version : '1.0.0' , url : 'https://bugsnag.com' } ,
70
+ sessions : [ session ]
71
+ }
72
+
73
+ expect ( jsonPayload . session ( sessionPayload ) ) . toBe ( '{"app":{"version":"1.0.0"},"device":{"id":"123"},"notifier":{"name":"Bugsnag","version":"1.0.0","url":"https://bugsnag.com"},"sessions":[{"id":"123","startedAt":"2012-12-21T00:00:00.000Z","events":{"handled":0,"unhandled":0}}]}' )
101
74
} )
102
75
} )
0 commit comments