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