-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: catch reject from fetch * feat: add property to ResponseError
- Loading branch information
Showing
4 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import createTestServer from 'create-test-server'; | ||
import request, { extend, Onion, fetch } from '../src/index'; | ||
|
||
const debug = require('debug')('afx-request:test'); | ||
|
||
const writeData = (data, res) => { | ||
res.setHeader('access-control-allow-origin', '*'); | ||
res.send(data); | ||
}; | ||
|
||
describe('error handle', () => { | ||
let server; | ||
beforeAll(async () => { | ||
server = await createTestServer(); | ||
}); | ||
afterAll(() => { | ||
server.close(); | ||
}); | ||
|
||
const prefix = api => `${server.url}${api}`; | ||
|
||
it('should catch fetch error and get reponse', async done => { | ||
server.get('/test/reject/302', (req, res) => { | ||
res.setHeader('access-control-allow-origin', '*'); | ||
res.status(302); | ||
res.setHeader({ location: 'https://www.baidu.com' }); | ||
res.send({ errorMsg: 'error response', errorCode: 'B000' }); | ||
}); | ||
|
||
const req = extend({}); | ||
|
||
try { | ||
const res = await req(prefix('/test/reject/302')); | ||
} catch (e) { | ||
expect(e.message).toBe('http error'); | ||
expect(e.type).toBe('HttpError'); | ||
expect(e.response.headers.get('Content-Type')).toBe('text/html; charset=utf-8'); | ||
done(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters