1
- import { RxRpcHttpConnection , RxRpcHttpTransport } from './rxrpc-http-transport' ;
1
+ import {
2
+ RxRpcHttpConnection ,
3
+ RxRpcHttpTransport ,
4
+ RxRpcHttpTransportInterceptor ,
5
+ RxRpcHttpTransportRequestConfig
6
+ } from './rxrpc-http-transport' ;
2
7
import axios from 'axios' ;
8
+ import { Observable , of } from "rxjs" ;
9
+ import { HttpAttributes } from "./rxrpc-http-attributes" ;
3
10
4
11
jest . mock ( 'axios' ) ;
5
- const mockedAxios = axios as jest . Mocked < typeof axios >
6
12
7
13
function delay ( ms : number ) {
8
14
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
@@ -14,18 +20,44 @@ describe('RxRpc Http Transport test suite', function () {
14
20
let incomingMessages : any [ ] ;
15
21
let resp : { }
16
22
23
+ let interceptors : RxRpcHttpTransportInterceptor [ ] = [ ] ;
24
+ let headerKey1 = "hk1" ;
25
+ let headerKey2 = "hk2"
26
+ let headerValue1 = "value1" ;
27
+ let headerValue2 = "value2" ;
28
+ let mockedAxios : jest . Mocked < typeof axios >
29
+
17
30
beforeEach ( ( ) => {
18
- transport = new RxRpcHttpTransport ( "https://funnyName/" ) ;
31
+ mockedAxios = axios as jest . Mocked < typeof axios >
32
+ interceptors . push ( new class implements RxRpcHttpTransportInterceptor {
33
+ intercept ( requestConfig : RxRpcHttpTransportRequestConfig ) : Observable < RxRpcHttpTransportRequestConfig > {
34
+ requestConfig . headers [ headerKey1 ] = headerValue1 ;
35
+ return of ( requestConfig ) ;
36
+ }
37
+ } )
38
+ interceptors . push ( new class implements RxRpcHttpTransportInterceptor {
39
+ intercept ( requestConfig : RxRpcHttpTransportRequestConfig ) : Observable < RxRpcHttpTransportRequestConfig > {
40
+ requestConfig . headers [ headerKey2 ] = headerValue2 ;
41
+ return of ( requestConfig ) ;
42
+ }
43
+ } )
44
+
45
+ transport = new RxRpcHttpTransport ( "https://funnyName/" , { interceptors : interceptors } ) ;
46
+
19
47
clientId = "12345678" ;
20
48
incomingMessages = [ ] ;
21
49
resp = {
22
50
headers : { "x-rpc-client-id" : clientId }
23
51
} ;
24
52
} ) ;
25
53
26
- it ( 'Connect' , async ( ) => {
27
- mockedAxios . post . mockImplementation ( ( ) => Promise . resolve ( resp ) ) ;
54
+ afterEach ( ( ) => {
55
+ mockedAxios . post . mockReset ( ) ;
56
+ interceptors = [ ] ;
57
+ } ) ;
28
58
59
+ it ( 'Connect' , async ( ) => {
60
+ mockAndVerifyExpectedHeaders ( ) ;
29
61
transport . connect ( ) . subscribe ( connection => incomingMessages . push ( connection [ 'clientId' ] ) )
30
62
await delay ( 1000 ) ;
31
63
expect ( incomingMessages [ 0 ] ) . toEqual ( clientId )
@@ -35,11 +67,13 @@ describe('RxRpc Http Transport test suite', function () {
35
67
const data1 = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #0\",\"error\":null}}"
36
68
const data2 = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #1\",\"error\":null}}"
37
69
resp [ 'data' ] = JSON . parse ( `[${ data1 } ,\n${ data2 } ]` )
38
- mockedAxios . post . mockImplementation ( ( ) => Promise . resolve ( resp ) ) ;
39
-
40
- transport . connect ( ) . subscribe ( connection => connection . poll ( ) . subscribe ( msg => {
41
- incomingMessages . push ( msg ) ;
42
- } ) )
70
+ mockAndVerifyExpectedHeaders ( ) ;
71
+ transport . connect ( ) . subscribe ( connection => {
72
+ mockAndVerifyExpectedHeadersWithClientId ( ) ;
73
+ connection . poll ( ) . subscribe ( msg => {
74
+ incomingMessages . push ( msg ) ;
75
+ } )
76
+ } )
43
77
await delay ( 1000 ) ;
44
78
expect ( incomingMessages . length ) . toEqual ( 2 ) ;
45
79
expect ( incomingMessages [ 0 ] ) . toEqual ( JSON . parse ( data1 ) ) ;
@@ -49,33 +83,61 @@ describe('RxRpc Http Transport test suite', function () {
49
83
it ( 'Poll with data as JSON' , async ( ) => {
50
84
const data = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #0\",\"error\":null}}"
51
85
resp [ 'data' ] = JSON . parse ( `[${ data } ]` )
52
- mockedAxios . post . mockImplementation ( ( ) => Promise . resolve ( resp ) ) ;
53
-
54
- transport . connect ( ) . subscribe ( connection => connection . poll ( ) . subscribe ( msg => {
55
- incomingMessages . push ( msg ) ;
56
- } ) )
86
+ mockAndVerifyExpectedHeaders ( ) ;
87
+ transport . connect ( ) . subscribe ( connection => {
88
+ mockAndVerifyExpectedHeadersWithClientId ( ) ;
89
+ connection . poll ( ) . subscribe ( msg => {
90
+ incomingMessages . push ( msg ) ;
91
+ } )
92
+ } )
57
93
await delay ( 1000 ) ;
58
94
expect ( incomingMessages . length ) . toEqual ( 1 ) ;
59
95
expect ( incomingMessages [ 0 ] ) . toEqual ( JSON . parse ( data ) ) ;
60
96
} )
61
97
62
98
it ( 'Close' , async ( ) => {
63
- mockedAxios . post . mockImplementation ( ( ) => Promise . resolve ( resp ) ) ;
64
-
99
+ mockAndVerifyExpectedHeaders ( ) ;
65
100
transport . connect ( ) . subscribe ( connection => incomingMessages . push ( connection ) )
66
101
await delay ( 1000 ) ;
67
102
const connection = incomingMessages [ 0 ] as RxRpcHttpConnection ;
103
+ mockAndVerifyExpectedHeadersWithClientId ( ) ;
68
104
connection . close ( )
69
105
expect ( connection [ 'pollingSubscription' ] . closed ) . toEqual ( true )
70
106
} )
71
107
72
108
it ( 'Error' , async ( ) => {
73
- mockedAxios . post . mockImplementation ( ( ) => Promise . resolve ( resp ) ) ;
74
-
109
+ mockAndVerifyExpectedHeaders ( ) ;
75
110
transport . connect ( ) . subscribe ( connection => incomingMessages . push ( connection ) )
76
111
await delay ( 1000 ) ;
77
112
const connection = incomingMessages [ 0 ] as RxRpcHttpConnection ;
113
+ mockAndVerifyExpectedHeadersWithClientId ( ) ;
78
114
connection . error ( null )
79
115
expect ( connection [ 'pollingSubscription' ] . closed ) . toEqual ( true )
80
116
} )
117
+
118
+ function mockAndVerifyExpectedHeaders ( ) {
119
+ mockAndVerifyHeaders ( getHeaders ( ) ) ;
120
+ }
121
+ function mockAndVerifyExpectedHeadersWithClientId ( ) {
122
+ mockAndVerifyHeaders ( getHeadersWithClientId ( ) ) ;
123
+ }
124
+ function mockAndVerifyHeaders ( expectedHeaders : { } ) {
125
+ mockedAxios . post . mockImplementation ( ( url , msg , options ) => {
126
+ expect ( options . headers ) . toEqual ( expectedHeaders ) ;
127
+ return Promise . resolve ( resp ) ;
128
+ } ) ;
129
+ }
130
+
131
+ function getHeadersWithClientId ( ) {
132
+ let expectedHeaders = getHeaders ( ) ;
133
+ expectedHeaders [ HttpAttributes . ClientIdAttribute ] = clientId ;
134
+ return expectedHeaders ;
135
+ }
136
+
137
+ function getHeaders ( ) {
138
+ let expectedHeaders = { } ;
139
+ expectedHeaders [ headerKey1 ] = headerValue1 ;
140
+ expectedHeaders [ headerKey2 ] = headerValue2 ;
141
+ return expectedHeaders ;
142
+ }
81
143
} )
0 commit comments