-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtests.js
More file actions
85 lines (66 loc) · 2.12 KB
/
Copy pathtests.js
File metadata and controls
85 lines (66 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const assert = require('assert');
const sinon = require('sinon');
const http = require('http');
const PassThrough = require('stream').PassThrough;
const turl = require('./index.js');
describe('turl', () => {
beforeEach(() => {
this.request = sinon.stub(http, 'request');
});
afterEach(() => {
this.request.restore();
});
it('should return the response when the request is successful', (done) => {
const expected = 'https://tinyurl/ok';
const response = new PassThrough();
response.statusCode = 200;
response.write(expected);
response.end();
const request = new PassThrough();
this.request.callsArgWith(1, response).returns(request);
turl.shorten('www.google.com').then((result) => {
assert(result);
assert(typeof result === 'string');
assert.equal(result, expected);
done();
}).catch(done);
});
it('should return an HTTPS short URL', (done) => {
const rawResult = 'http://tinyurl/ok';
const expected = 'https://tinyurl/ok';
const response = new PassThrough();
response.statusCode = 200;
response.write(rawResult);
response.end();
const request = new PassThrough();
this.request.callsArgWith(1, response).returns(request);
turl.shorten('www.google.com').then((result) => {
assert(result);
assert(typeof result === 'string');
assert.equal(result, expected);
done();
}).catch(done);
});
it('should return an error when the status code is invalid', (done) => {
const response = new PassThrough();
response.statusCode = 400;
response.end();
const request = new PassThrough();
this.request.callsArgWith(1, response).returns(request);
turl.shorten('www.google.com').catch((error) => {
assert(error);
done();
});
});
it('should return in case request fails', (done) => {
const expected = 'some error';
const request = new PassThrough();
this.request.returns(request);
turl.shorten('www.google.com').catch((error) => {
assert(error);
assert.equal(error, expected);
done();
});
request.emit('error', expected);
});
});