1- import sinon from 'sinon ' ;
1+ import nock from 'nock ' ;
22import FakeRequest from '../mocks/fakeNetworkRequest' ;
33import InstabugConstants from '../../src/utils/InstabugConstants' ;
44import Interceptor from '../../src/utils/XhrNetworkInterceptor' ;
55
66const url = 'http://api.instabug.com' ;
77const method = 'GET' ;
88
9- describe ( 'Network Interceptor' , ( ) => {
10- let server ;
11- let requests ;
12-
13- beforeEach ( function ( ) {
14- server = sinon . useFakeXMLHttpRequest ( ) ;
15- requests = [ ] ;
16-
17- server . onCreate = function ( xhr ) {
18- requests . push ( xhr ) ;
19- } ;
20- } ) ;
9+ const request = nock ( url ) . get ( '/' ) ;
2110
22- afterEach ( function ( ) {
23- server . restore ( ) ;
11+ describe ( 'Network Interceptor' , ( ) => {
12+ beforeEach ( ( ) => {
13+ nock . cleanAll ( ) ;
2414 } ) ;
2515
2616 it ( 'should set network object on entering XMLHttpRequest.prototype.open' , done => {
@@ -30,9 +20,9 @@ describe('Network Interceptor', () => {
3020 expect ( network . method ) . toEqual ( method ) ;
3121 done ( ) ;
3222 } ) ;
23+ FakeRequest . mockResponse ( request ) ;
3324 FakeRequest . open ( method , url ) ;
3425 FakeRequest . send ( ) ;
35- FakeRequest . mockResponse ( requests [ 0 ] ) ;
3626 } ) ;
3727
3828 it ( 'should set network object on calling setRequestHeader' , done => {
@@ -44,9 +34,9 @@ describe('Network Interceptor', () => {
4434 done ( ) ;
4535 } ) ;
4636 FakeRequest . open ( method , url ) ;
37+ FakeRequest . mockResponse ( request ) ;
4738 FakeRequest . setRequestHeaders ( requestHeaders ) ;
4839 FakeRequest . send ( ) ;
49- FakeRequest . mockResponse ( requests [ 0 ] ) ;
5040 } ) ;
5141
5242 it ( 'should set requestBody in network object' , done => {
@@ -56,9 +46,9 @@ describe('Network Interceptor', () => {
5646 expect ( network . requestBody ) . toEqual ( JSON . stringify ( requestBody ) ) ;
5747 done ( ) ;
5848 } ) ;
49+ FakeRequest . mockResponse ( request ) ;
5950 FakeRequest . open ( method , url ) ;
6051 FakeRequest . send ( requestBody ) ;
61- FakeRequest . mockResponse ( requests [ 0 ] ) ;
6252 } ) ;
6353
6454 it ( 'should set contentType in network object on receiving response' , done => {
@@ -68,9 +58,9 @@ describe('Network Interceptor', () => {
6858 expect ( network . contentType ) . toEqual ( headers [ 'Content-type' ] ) ;
6959 done ( ) ;
7060 } ) ;
61+ FakeRequest . mockResponse ( request , 200 , 'ok' , headers ) ;
7162 FakeRequest . open ( method , url ) ;
7263 FakeRequest . send ( ) ;
73- FakeRequest . mockResponse ( requests [ 0 ] , headers ) ;
7464 } ) ;
7565
7666 it ( 'should set responseHeaders in network object on receiving response' , done => {
@@ -81,26 +71,13 @@ describe('Network Interceptor', () => {
8171 } ;
8272 Interceptor . enableInterception ( ) ;
8373 Interceptor . setOnDoneCallback ( network => {
84- expect ( network . responseHeaders [ 'Content-type' ] . trim ( ) ) . toEqual ( headers [ 'Content-type' ] ) ;
85- expect ( network . responseHeaders [ 'Accept' ] . trim ( ) ) . toEqual ( headers [ 'Accept' ] ) ;
86- done ( ) ;
87- } ) ;
88- FakeRequest . open ( method , url ) ;
89- FakeRequest . send ( ) ;
90- FakeRequest . mockResponse ( requests [ 0 ] , headers ) ;
91- } ) ;
92-
93- it ( 'should set responseHeaders in network object on receiving response' , done => {
94- const headers = { 'Content-type' : 'application/json' , Accept : 'text/html' } ;
95- Interceptor . enableInterception ( ) ;
96- Interceptor . setOnDoneCallback ( network => {
97- expect ( network . responseHeaders [ 'Content-type' ] . trim ( ) ) . toEqual ( headers [ 'Content-type' ] ) ;
98- expect ( network . responseHeaders [ 'Accept' ] . trim ( ) ) . toEqual ( headers [ 'Accept' ] ) ;
74+ expect ( network . responseHeaders [ 'content-type' ] . trim ( ) ) . toEqual ( headers [ 'Content-type' ] ) ;
75+ expect ( network . responseHeaders . accept . trim ( ) ) . toEqual ( headers [ 'Accept' ] ) ;
9976 done ( ) ;
10077 } ) ;
78+ FakeRequest . mockResponse ( request , 200 , 'ok' , headers ) ;
10179 FakeRequest . open ( method , url ) ;
10280 FakeRequest . send ( ) ;
103- FakeRequest . mockResponse ( requests [ 0 ] , headers ) ;
10481 } ) ;
10582
10683 it ( 'should set responseCode in network object on receiving response' , done => {
@@ -110,10 +87,9 @@ describe('Network Interceptor', () => {
11087 expect ( network . responseCode ) . toEqual ( status ) ;
11188 done ( ) ;
11289 } ) ;
90+ FakeRequest . mockResponse ( request , status ) ;
11391 FakeRequest . open ( method , url ) ;
11492 FakeRequest . send ( ) ;
115- FakeRequest . mockStatus ( requests [ 0 ] , status ) ;
116- FakeRequest . mockResponse ( requests [ 0 ] ) ;
11793 } ) ;
11894
11995 it ( 'should set responseBody in network object on receiving response' , done => {
@@ -124,9 +100,9 @@ describe('Network Interceptor', () => {
124100 done ( ) ;
125101 } ) ;
126102 FakeRequest . open ( method , url ) ;
103+ FakeRequest . mockResponse ( request , 200 , JSON . stringify ( responseBody ) ) ;
127104 FakeRequest . setResponseType ( 'json' ) ;
128105 FakeRequest . send ( ) ;
129- FakeRequest . mockResponse ( requests [ 0 ] , null , JSON . stringify ( responseBody ) ) ;
130106 } ) ;
131107
132108 it ( 'should call onProgressCallback in network object on receiving response' , done => {
@@ -137,19 +113,18 @@ describe('Network Interceptor', () => {
137113 done ( ) ;
138114 } ) ;
139115
116+ FakeRequest . mockResponse ( request , 200 , 'ok' , { 'Content-Length' : 100 } ) ;
140117 FakeRequest . open ( method , url ) ;
141118 FakeRequest . send ( ) ;
142- FakeRequest . mockStatus ( requests [ 0 ] , 200 ) ;
143- FakeRequest . mockResponse ( requests [ 0 ] ) ;
144119 } ) ;
145120
146121 it ( 'should set responseBody in network object on receiving response' , ( ) => {
147122 Interceptor . disableInterception ( ) ;
148123 const callback = jest . fn ( ) ;
149124 Interceptor . setOnDoneCallback ( callback ) ;
125+ FakeRequest . mockResponse ( request ) ;
150126 FakeRequest . open ( method , url ) ;
151127 FakeRequest . send ( ) ;
152- FakeRequest . mockResponse ( requests [ 0 ] ) ;
153128 expect ( callback ) . not . toHaveBeenCalled ( ) ;
154129 } ) ;
155130 it ( 'should set gqlQueryName in network object on receiving response' , done => {
@@ -162,9 +137,9 @@ describe('Network Interceptor', () => {
162137 done ( ) ;
163138 } ) ;
164139 FakeRequest . open ( method , url ) ;
140+ FakeRequest . mockResponse ( request , 200 , JSON . stringify ( responseBody ) ) ;
165141 FakeRequest . setRequestHeaders ( headers ) ;
166142 FakeRequest . send ( ) ;
167- FakeRequest . mockResponse ( requests [ 0 ] , null , JSON . stringify ( responseBody ) ) ;
168143 } ) ;
169144
170145 it ( 'should set gqlQueryName in network object on receiving response with empty string' , done => {
@@ -176,9 +151,9 @@ describe('Network Interceptor', () => {
176151 done ( ) ;
177152 } ) ;
178153 FakeRequest . open ( method , url ) ;
154+ FakeRequest . mockResponse ( request ) ;
179155 FakeRequest . setRequestHeaders ( headers ) ;
180156 FakeRequest . send ( ) ;
181- FakeRequest . mockResponse ( requests [ 0 ] ) ;
182157 } ) ;
183158
184159 it ( 'should set serverErrorMessage in network object on receiving response' , done => {
@@ -192,8 +167,8 @@ describe('Network Interceptor', () => {
192167 } ) ;
193168 FakeRequest . open ( method , url ) ;
194169 FakeRequest . setRequestHeaders ( headers ) ;
170+ FakeRequest . mockResponse ( request , 200 , JSON . stringify ( responseBody ) ) ;
195171 FakeRequest . setResponseType ( 'json' ) ;
196172 FakeRequest . send ( ) ;
197- FakeRequest . mockResponse ( requests [ 0 ] , null , JSON . stringify ( responseBody ) ) ;
198173 } ) ;
199174} ) ;
0 commit comments