1
+ import { SentryError } from '@sentry/core' ;
2
+ import { TransportOptions } from '@sentry/types' ;
3
+ import * as HttpsProxyAgent from 'https-proxy-agent' ;
4
+ import { HTTPTransport } from '../../src/transports/http' ;
5
+
6
+ const mockSetEncoding = jest . fn ( ) ;
7
+ const dsn = 'http://[email protected] :8989/mysubpath/50622' ;
1
8
let mockReturnCode = 200 ;
2
9
let mockHeaders = { } ;
3
- let mockCheckHeaders : any ;
4
- const mockSetEncoding = jest . fn ( ) ;
5
- jest . mock ( 'http' , ( ) => ( {
6
- Agent : jest . fn ( ) ,
7
- request : ( options : any , callback : any ) => {
8
- expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_version' ) ;
9
- expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_timestamp' ) ;
10
- expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_client' ) ;
11
- expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_key' ) ;
12
- expect ( options . port ) . toEqual ( '8989' ) ;
13
- expect ( options . path ) . toEqual ( '/mysubpath/api/50622/store/' ) ;
14
- expect ( options . hostname ) . toEqual ( 'sentry.io' ) ;
15
- if ( mockCheckHeaders ) {
16
- expect ( options . headers ) . toEqual ( expect . objectContaining ( mockCheckHeaders ) ) ;
17
- }
18
- return {
10
+
11
+ function createTransport ( options : TransportOptions ) : HTTPTransport {
12
+ const transport = new HTTPTransport ( options ) ;
13
+ transport . module = {
14
+ request : jest . fn ( ) . mockImplementation ( ( _options : any , callback : any ) => ( {
19
15
end : ( ) => {
20
16
callback ( {
21
17
headers : mockHeaders ,
@@ -24,63 +20,105 @@ jest.mock('http', () => ({
24
20
} ) ;
25
21
} ,
26
22
on : jest . fn ( ) ,
27
- } ;
28
- } ,
29
- } ) ) ;
23
+ } ) ) ,
24
+ } ;
25
+ return transport ;
26
+ }
30
27
31
- import { Dsn , SentryError } from '@sentry/core' ;
32
- import { getCurrentHub , init , NodeClient } from '../../src' ;
33
-
34
- const dsn = 'http://[email protected] :8989/mysubpath/50622' ;
28
+ function assertBasicOptions ( options : any ) : void {
29
+ expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_version' ) ;
30
+ expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_timestamp' ) ;
31
+ expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_client' ) ;
32
+ expect ( options . headers [ 'X-Sentry-Auth' ] ) . toContain ( 'sentry_key' ) ;
33
+ expect ( options . port ) . toEqual ( '8989' ) ;
34
+ expect ( options . path ) . toEqual ( '/mysubpath/api/50622/store/' ) ;
35
+ expect ( options . hostname ) . toEqual ( 'sentry.io' ) ;
36
+ }
35
37
36
38
describe ( 'HTTPTransport' , ( ) => {
37
39
beforeEach ( ( ) => {
38
- mockHeaders = { } ;
39
40
mockReturnCode = 200 ;
41
+ mockHeaders = { } ;
42
+ jest . clearAllMocks ( ) ;
40
43
} ) ;
41
44
42
45
test ( 'send 200' , async ( ) => {
43
- init ( {
44
- dsn,
46
+ const transport = createTransport ( { dsn } ) ;
47
+ await transport . captureEvent ( {
48
+ message : 'test' ,
45
49
} ) ;
46
- await getCurrentHub ( )
47
- . getClient ( )
48
- . captureMessage ( 'test' ) ;
50
+
51
+ const requestOptions = ( transport . module ! . request as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
52
+ assertBasicOptions ( requestOptions ) ;
49
53
expect ( mockSetEncoding ) . toHaveBeenCalled ( ) ;
50
54
} ) ;
51
55
52
56
test ( 'send 400' , async ( ) => {
53
57
mockReturnCode = 400 ;
54
- const client = new NodeClient ( { dsn } ) ;
55
- client . install ( ) ;
56
- return expect ( client . captureMessage ( 'test' ) ) . rejects . toEqual ( new SentryError ( `HTTP Error (${ mockReturnCode } )` ) ) ;
58
+ const transport = createTransport ( { dsn } ) ;
59
+
60
+ try {
61
+ await transport . captureEvent ( {
62
+ message : 'test' ,
63
+ } ) ;
64
+ } catch ( e ) {
65
+ const requestOptions = ( transport . module ! . request as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
66
+ assertBasicOptions ( requestOptions ) ;
67
+ expect ( e ) . toEqual ( new SentryError ( `HTTP Error (${ mockReturnCode } )` ) ) ;
68
+ }
57
69
} ) ;
58
70
59
71
test ( 'send x-sentry-error header' , async ( ) => {
60
72
mockReturnCode = 429 ;
61
73
mockHeaders = {
62
74
'x-sentry-error' : 'test-failed' ,
63
75
} ;
64
- const client = new NodeClient ( { dsn } ) ;
65
- client . install ( ) ;
66
- return expect ( client . captureMessage ( 'test' ) ) . rejects . toEqual (
67
- new SentryError ( `HTTP Error (${ mockReturnCode } ): test-failed` ) ,
68
- ) ;
76
+ const transport = createTransport ( { dsn } ) ;
77
+
78
+ try {
79
+ await transport . captureEvent ( {
80
+ message : 'test' ,
81
+ } ) ;
82
+ } catch ( e ) {
83
+ const requestOptions = ( transport . module ! . request as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
84
+ assertBasicOptions ( requestOptions ) ;
85
+ expect ( e ) . toEqual ( new SentryError ( `HTTP Error (${ mockReturnCode } ): test-failed` ) ) ;
86
+ }
69
87
} ) ;
70
88
71
89
test ( 'transport options' , async ( ) => {
72
90
mockReturnCode = 200 ;
73
- const client = new NodeClient ( {
91
+ const transport = createTransport ( {
74
92
dsn,
75
- transportOptions : {
76
- dsn : new Dsn ( dsn ) ,
77
- headers : {
78
- a : 'b' ,
79
- } ,
93
+ headers : {
94
+ a : 'b' ,
80
95
} ,
81
96
} ) ;
82
- client . install ( ) ;
83
- mockCheckHeaders = { a : 'b' } ;
84
- await client . captureMessage ( 'test' ) ;
97
+ await transport . captureEvent ( {
98
+ message : 'test' ,
99
+ } ) ;
100
+
101
+ const requestOptions = ( transport . module ! . request as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
102
+ assertBasicOptions ( requestOptions ) ;
103
+ expect ( requestOptions . headers ) . toEqual ( expect . objectContaining ( { a : 'b' } ) ) ;
104
+ } ) ;
105
+
106
+ test ( 'http proxy' , async ( ) => {
107
+ mockReturnCode = 200 ;
108
+ const transport = createTransport ( {
109
+ dsn,
110
+ httpProxy : 'http://example.com:8080' ,
111
+ } ) ;
112
+ await transport . captureEvent ( {
113
+ message : 'test' ,
114
+ } ) ;
115
+
116
+ const requestOptions = ( transport . module ! . request as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
117
+ assertBasicOptions ( requestOptions ) ;
118
+ expect ( requestOptions . agent ) . toBeInstanceOf ( HttpsProxyAgent ) ;
119
+ expect ( requestOptions . agent . secureProxy ) . toEqual ( false ) ;
120
+ expect ( requestOptions . agent . proxy ) . toEqual (
121
+ expect . objectContaining ( { protocol : 'http:' , port : 8080 , host : 'example.com' } ) ,
122
+ ) ;
85
123
} ) ;
86
124
} ) ;
0 commit comments