-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
180 lines (141 loc) · 4.11 KB
/
test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
describe('carpenter-api-client', function () {
'use strict';
var assume = require('assume'),
Carpenter = require('./'),
nock = require('nock'),
url = require('url');
var carpenter, uri;
beforeEach(function each() {
uri = 'http://localhost:8999/';
carpenter = new Carpenter(uri);
});
afterEach(function each() {
carpenter = null;
});
it('can be configured with object', function () {
carpenter = new Carpenter(url.parse(uri));
assume(carpenter.base).to.be.an('string');
assume(carpenter.base).to.equal(uri);
});
it('can be configured with an agent and url', function () {
carpenter = new Carpenter({
url: uri,
agent: require('http').Agent
});
assume(carpenter.agent);
});
it('can be configured with default timeout and defaults to 0', function () {
assume(carpenter).to.have.property('timeout', 0);
carpenter = new Carpenter({
url: uri,
timeout: 2 * 60 * 1000
});
assume(carpenter).to.have.property('timeout', 120000);
});
it('can be configured with an agent and url object', function () {
carpenter = new Carpenter({
url: url.format(uri),
agent: require('http').Agent
});
assume(carpenter.base).to.equal(uri);
assume(carpenter.agent);
});
it('can be configured with an api version', function () {
[1, '1', 'v1'].forEach(function (version) {
carpenter = new Carpenter({ uri, version });
assume(carpenter.version).equals('');
});
});
it('defaults to the v2 api', function () {
assume(new Carpenter({ uri }).version).equals('v2');
assume(new Carpenter({ uri, version: 'not one' }).version).equals('v2');
});
describe('#build', function () {
var options = {
data: {
data: { name: 'foo-bar' },
promote: true
}
};
it('sends a request to /v2/build', function (next) {
next = assume.wait(2, next);
nock(uri)
.post('/v2/build')
.reply(200, function reply(uri, body) {
assume(body.data.name).equals('foo-bar');
assume(body.promote).equals(true);
nock.cleanAll();
next();
return {};
});
carpenter.build(options, next);
});
it('can also send stringified data', function (next) {
next = assume.wait(2, next);
nock(uri)
.post('/v2/build')
.reply(200, function reply(uri, body) {
assume(body.data.name).equals('foo-bar');
assume(body.promote).equals(true);
nock.cleanAll();
next();
return {};
});
carpenter.build({
data: JSON.stringify(options.data)
}, next);
});
it('sends a request to /build when using v1 API', function (next) {
carpenter = new Carpenter({
uri,
version: 'v1'
});
next = assume.wait(2, next);
nock(uri)
.post('/build')
.reply(200, function reply(uri, body) {
assume(body.name).equals('foo-bar');
nock.cleanAll();
next();
return {};
});
carpenter.build({
data: { name: 'foo-bar' }
}, next);
});
});
describe('#cancel', function () {
var options = {
pkg: 'foo-bar',
version: '1.0.0',
env: 'prod'
};
it('sends a request to /cancel', function (next) {
next = assume.wait(2, next);
nock(uri)
.get('/cancel/foo-bar/1.0.0/prod')
.reply(200, function reply(uri, body) {
assume(uri).equals('/cancel/foo-bar/1.0.0/prod');
assume(body).to.be.falsey();
nock.cleanAll();
next();
return {};
});
carpenter.cancel(options, next);
});
it('has optional env parameter', function (next) {
next = assume.wait(2, next);
delete options.env;
nock(uri)
.get('/cancel/foo-bar/1.0.0')
.reply(200, function reply(uri, body) {
assume(uri).equals('/cancel/foo-bar/1.0.0');
assume(body).to.be.falsey();
nock.cleanAll();
next();
return {};
});
carpenter.cancel(options, next);
});
});
});