1 |
|
2 | "use strict";
|
3 |
|
4 | require('../fetch-npm-node');
|
5 | var expect = require('chai').expect;
|
6 | var nock = require('nock');
|
7 | var good = 'hello world. 你好世界。';
|
8 | var bad = 'good bye cruel world. 再见残酷的世界。';
|
9 |
|
10 | function responseToText(response) {
|
11 | if (response.status >= 400) throw new Error("Bad server response");
|
12 | return response.text();
|
13 | }
|
14 |
|
15 | describe('fetch', function() {
|
16 |
|
17 | before(function() {
|
18 | nock('https://mattandre.ws')
|
19 | .get('/succeed.txt')
|
20 | .reply(200, good);
|
21 | nock('https://mattandre.ws')
|
22 | .get('/fail.txt')
|
23 | .reply(404, bad);
|
24 | });
|
25 |
|
26 | it('should be defined', function() {
|
27 | expect(fetch).to.be.a('function');
|
28 | });
|
29 |
|
30 | it('should facilitate the making of requests', function(done) {
|
31 | fetch('//mattandre.ws/succeed.txt')
|
32 | .then(responseToText)
|
33 | .then(function(data) {
|
34 | expect(data).to.equal(good);
|
35 | done();
|
36 | })
|
37 | .catch(done);
|
38 | });
|
39 |
|
40 | it('should do the right thing with bad requests', function(done) {
|
41 | fetch('//mattandre.ws/fail.txt')
|
42 | .then(responseToText)
|
43 | .catch(function(err) {
|
44 | expect(err.toString()).to.equal("Error: Bad server response");
|
45 | done();
|
46 | })
|
47 | .catch(done);
|
48 | });
|
49 |
|
50 | });
|