-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker.test.js
More file actions
113 lines (96 loc) · 2.73 KB
/
tracker.test.js
File metadata and controls
113 lines (96 loc) · 2.73 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
var expect = require('chai').expect;
var sinon = require('sinon');
var http = require('http');
describe('Tracker', function () {
const PORT = 8787;
var Tracker = require('../release/tracker').default,
TrackerService = require('../release/tracker-service').default,
bencode = require('bencode'),
tracker,
url;
before(function () {
sinon.stub(bencode, 'encode', function (data) {
return 'encoded-'+data;
});
});
after(function () {
bencode.encode.restore();
});
beforeEach(function () {
tracker = new Tracker();
tracker.start(PORT);
});
afterEach(function () {
tracker.close();
});
function itHasStatus (status) {
it('has status 404', function (done) {
http.get(url, function (response) {
expect(response.statusCode).to.equal(status);
done();
});
});
}
function itHasContentTypeTextPlain () {
it('has content type "text/plain"', function (done) {
http.get(url, function (response) {
expect(response.headers['content-type']).to.equal('text/plain');
done();
});
});
}
function itRespondsWithBencodedData (data) {
it(`responds with bencoded data "${data}"`, function (done) {
http.get(url, function (response) {
var text = '';
response.on('data', function (chunk) {
text += chunk;
});
response.on('end', function () {
expect(text).to.equal(`encoded-${data}`);
done();
});
});
});
}
describe('GET /', function () {
beforeEach(function () {
url = `http://localhost:${PORT}`;
});
itHasStatus(404);
itHasContentTypeTextPlain();
itRespondsWithBencodedData('Not found')
});
describe('GET /announce', function () {
beforeEach(function () {
url = `http://localhost:${PORT}/announce`;
});
describe('when service worked properly', function () {
beforeEach(function () {
sinon.stub(TrackerService.prototype, 'announce', function() {
return 'announce-result';
});
});
afterEach(function () {
TrackerService.prototype.announce.restore();
});
itHasStatus(200);
itHasContentTypeTextPlain();
itRespondsWithBencodedData('announce-result');
});
describe('when service throws an unhandled error', function () {
beforeEach(function () {
sinon.stub(TrackerService.prototype, 'announce', function() {
throw new Error('unhandled error message');
});
});
afterEach(function () {
TrackerService.prototype.announce.restore();
});
itHasStatus(500);
itHasContentTypeTextPlain();
itRespondsWithBencodedData('unhandled error message');
});
});
});