@@ -5,9 +5,11 @@ import express from 'express';
5
5
import * as E from 'fp-ts/Either' ;
6
6
import * as t from 'io-ts' ;
7
7
import { NumberFromString } from 'io-ts-types' ;
8
+ import superagent from 'superagent' ;
8
9
import supertest from 'supertest' ;
10
+ import { URL } from 'url' ;
9
11
10
- import { supertestRequestFactory } from '../src/request' ;
12
+ import { superagentRequestFactory , supertestRequestFactory } from '../src/request' ;
11
13
import { buildApiClient } from '../src/routes' ;
12
14
13
15
const PostTestRoute = h . httpRoute ( {
@@ -184,4 +186,32 @@ describe('request', () => {
184
186
assert . isTrue ( result ) ;
185
187
} ) ;
186
188
} ) ;
189
+
190
+ describe ( 'superagent' , async ( ) => {
191
+ it ( 'does not throw on non-2xx status codes' , async ( ) => {
192
+ // Figure out what host/port supertest set up (the response is just thrown away on purpose)
193
+ const superTestReq = apiClient [ 'api.v1.test' ] . post ( {
194
+ id : 1337 ,
195
+ foo : 'test' ,
196
+ bar : 42 ,
197
+ } ) ;
198
+
199
+ // Construct an api client that uses superagent, with the base url extracted from the supertest
200
+ // request above.
201
+ const url = new URL ( superTestReq . url ) ;
202
+ url . pathname = '/' ;
203
+ const superagentClient = buildApiClient (
204
+ superagentRequestFactory ( superagent , url . toString ( ) ) ,
205
+ TestRoutes ,
206
+ ) ;
207
+
208
+ const req = await superagentClient [ 'api.v1.test' ]
209
+ . post ( { id : 1337 , foo : 'test' , bar : 42 } )
210
+ . set ( 'x-send-unexpected-status-code' , 'true' )
211
+ . decode ( ) ;
212
+
213
+ assert . equal ( req . status , 'decodeError' ) ;
214
+ assert . equal ( req . original . status , 400 ) ;
215
+ } ) ;
216
+ } ) ;
187
217
} ) ;
0 commit comments