11import * as faker from 'faker' ;
2+ import * as SyncTasks from 'synctasks' ;
23import { SimpleWebRequest , SimpleWebRequestOptions , WebErrorResponse , WebRequestPriority } from '../src/SimpleWebRequest' ;
34import { DETAILED_RESPONSE } from './helpers' ;
4- import * as SyncTasks from 'synctasks' ;
55
66describe ( 'SimpleWebRequest' , ( ) => {
77 let catchExceptions = false ;
@@ -79,32 +79,38 @@ describe('SimpleWebRequest', () => {
7979
8080 expect ( request . requestHeaders [ 'X-Requested-With' ] ) . toEqual ( headers [ 'X-Requested-With' ] ) ;
8181 expect ( request . requestHeaders [ 'Max-Forwards' ] ) . toEqual ( headers [ 'Max-Forwards' ] ) ;
82-
82+
8383 request . respondWith ( { status } ) ;
8484 } ) ;
8585
8686 it ( 'forbids to set Accept header' , ( ) => {
87+ spyOn ( console , 'error' ) ;
88+
8789 const headers = {
8890 'Accept' : 'application/xml' ,
8991 } ;
9092 const method = 'GET' ;
9193 const url = faker . internet . url ( ) ;
94+ const error = `Don't set Accept with options.headers -- use it with the options.acceptType property` ;
95+ const request = new SimpleWebRequest < string > ( url , method , { } , ( ) => headers ) ;
9296
93- expect (
94- ( ) => new SimpleWebRequest < string > ( url , method , { } , ( ) => headers ) . start ( )
95- ) . toThrowError ( `Don't set Accept with options.headers -- use it with the options.acceptType property` ) ;
96-
97+ expect ( ( ) => request . start ( ) ) . toThrowError ( error ) ;
98+ expect ( console . error ) . toHaveBeenCalledWith ( error ) ;
9799 } ) ;
98100
99101 it ( 'forbids to set Content-Type header' , ( ) => {
102+ spyOn ( console , 'error' ) ;
103+
100104 const headers = {
101105 'Content-Type' : 'application/xml' ,
102106 } ;
103107 const method = 'GET' ;
104108 const url = faker . internet . url ( ) ;
105- expect (
106- ( ) => new SimpleWebRequest < string > ( url , method , { } , ( ) => headers ) . start ( )
107- ) . toThrowError ( `Don't set Content-Type with options.headers -- use it with the options.contentType property` ) ;
109+ const error = `Don't set Content-Type with options.headers -- use it with the options.contentType property` ;
110+ const request = new SimpleWebRequest < string > ( url , method , { } , ( ) => headers ) ;
111+
112+ expect ( ( ) => request . start ( ) ) . toThrowError ( error ) ;
113+ expect ( console . error ) . toHaveBeenCalledWith ( error ) ;
108114 } ) ;
109115
110116 describe ( 'blocking' , ( ) => {
@@ -115,8 +121,9 @@ describe('SimpleWebRequest', () => {
115121 SimpleWebRequestOptions . MaxSimultaneousRequests = 0 ;
116122 jasmine . clock ( ) . install ( ) ;
117123 } ) ;
124+
118125 afterEach ( ( ) => {
119- SimpleWebRequestOptions . MaxSimultaneousRequests = maxRequests ;
126+ SimpleWebRequestOptions . MaxSimultaneousRequests = maxRequests ;
120127 jasmine . clock ( ) . uninstall ( ) ;
121128 } ) ;
122129
@@ -128,18 +135,20 @@ describe('SimpleWebRequest', () => {
128135 const onSuccessLow2 = jasmine . createSpy ( 'onSuccessLow2' ) ;
129136 const onSuccessCritical2 = jasmine . createSpy ( 'onSuccessCritical2' ) ;
130137 const status = 200 ;
131-
138+
132139 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Low } ) . start ( ) . then ( onSuccessLow1 ) ;
133140 jasmine . clock ( ) . tick ( 10 ) ;
141+
134142 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Critical } ) . start ( ) . then ( onSuccessCritical1 ) ;
135143 jasmine . clock ( ) . tick ( 10 ) ;
144+
136145 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Low } ) . start ( ) . then ( onSuccessLow2 ) ;
137146 jasmine . clock ( ) . tick ( 10 ) ;
138-
147+
139148 SimpleWebRequestOptions . MaxSimultaneousRequests = 1 ;
140149 // add a new request to kick the queue
141150 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Critical } ) . start ( ) . then ( onSuccessCritical2 ) ;
142-
151+
143152 // only one is executed
144153 expect ( jasmine . Ajax . requests . count ( ) ) . toBe ( 1 ) ;
145154 jasmine . Ajax . requests . mostRecent ( ) . respondWith ( { status} ) ;
@@ -155,7 +164,7 @@ describe('SimpleWebRequest', () => {
155164 jasmine . Ajax . requests . mostRecent ( ) . respondWith ( { status} ) ;
156165 expect ( onSuccessLow2 ) . toHaveBeenCalled ( ) ;
157166 } ) ;
158-
167+
159168 it ( 'blocks the request with custom promise' , ( ) => {
160169 SimpleWebRequestOptions . MaxSimultaneousRequests = 1 ;
161170 const url = faker . internet . url ( ) ;
@@ -171,7 +180,7 @@ describe('SimpleWebRequest', () => {
171180 request . respondWith ( { status : 200 } ) ;
172181 expect ( onSuccess1 ) . toHaveBeenCalled ( ) ;
173182 } ) ;
174-
183+
175184 it ( 'after the request is unblocked, it\'s returned to the queue with correct priority' , ( ) => {
176185 const url = faker . internet . url ( ) ;
177186 const method = 'GET' ;
@@ -188,7 +197,7 @@ describe('SimpleWebRequest', () => {
188197 SimpleWebRequestOptions . MaxSimultaneousRequests = 1 ;
189198 // add a new request to kick the queue
190199 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Critical } ) . start ( ) . then ( onSuccessCritical ) ;
191-
200+
192201 // unblock the request
193202 blockDefer . resolve ( void 0 ) ;
194203
@@ -200,11 +209,11 @@ describe('SimpleWebRequest', () => {
200209 jasmine . Ajax . requests . mostRecent ( ) . respondWith ( { status : 200 } ) ;
201210 expect ( onSuccessHigh ) . toHaveBeenCalled ( ) ;
202211
203- // and the low priority one gets sent last
212+ // and the low priority one gets sent last
204213 jasmine . Ajax . requests . mostRecent ( ) . respondWith ( { status : 200 } ) ;
205214 expect ( onSuccessLow ) . toHaveBeenCalled ( ) ;
206215 } ) ;
207-
216+
208217 it ( 'checks the blocked function again, once the request is on top of the queue' , ( ) => {
209218 const url = faker . internet . url ( ) ;
210219 const method = 'GET' ;
@@ -216,13 +225,14 @@ describe('SimpleWebRequest', () => {
216225
217226 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . Critical } , undefined , blockSpy ) . start ( ) . then ( onSuccessCritical ) ;
218227 jasmine . clock ( ) . tick ( 10 ) ;
228+
219229 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . High } ) . start ( ) . then ( onSuccessHigh ) ;
220230 jasmine . clock ( ) . tick ( 10 ) ;
221231
222232 SimpleWebRequestOptions . MaxSimultaneousRequests = 1 ;
223233 // add a new request to kick the queue
224234 new SimpleWebRequest < string > ( url , method , { priority : WebRequestPriority . High } ) . start ( ) . then ( onSuccessHigh2 ) ;
225-
235+
226236 expect ( blockSpy ) . toHaveBeenCalled ( ) ;
227237
228238 jasmine . Ajax . requests . mostRecent ( ) . respondWith ( { status : 200 } ) ;
@@ -253,7 +263,7 @@ describe('SimpleWebRequest', () => {
253263 expect ( err . statusText ) . toBe ( '_blockRequestUntil rejected: ' + errorString ) ;
254264 done ( ) ;
255265 } ) ;
256-
266+
257267 blockDefer . reject ( errorString ) ;
258268 } ) ;
259269 } ) ;
0 commit comments