-
Notifications
You must be signed in to change notification settings - Fork 23
/
restAPIExample1.js
299 lines (262 loc) · 8.99 KB
/
restAPIExample1.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// restAPIExample1.js
//
// This test script will demonstrate how to use supertest to test the
// Example Order System Rest APIs.
// This example will validate existing data (customer/order/item) sets and then
// add records and finally delete records.
//
// Data:
// Customer(s)
// Order(s)
// Item(s)
// To Run:
// Start json-server with json database file (db.json)
// $ cd json-server
// $ json-server db.json
//
// see https://github.com/typicode/json-server for project
// see http://jsonplaceholder.typicode.com/ for working demo online of json-server
//
// then run script
// $ mocha restAPIExample1.js
// Updated to support version 4 of webdriverio
// use a tool like postman to view the data before and after the script is ran.
//
// URLs:
// GET http://localhost:3000/customer - all customer(s)
// GET http://localhost:3000/customer/2 - customer #2
// GET http://localhost:3000/order/ - all order(s)
// GET http://localhost:3000/order?customerId=1 - order(s) for customer #1
// GET http://localhost:3000/item - all item(s)
// GET http://localhost:3000/item?orderId=1 - item(s) for order #1
// POST http://localhost:3000/customer - add a customer
// DELETE http://localhost:3000/customer/3 - delete customer #3
// required libraries
var request = require('supertest'),
should = require('should');
// Custom assertions makes the code cleaner
should.Assertion.add('validCustomer', function(first, last, id) {
this.params = { operator: 'to be a valid customer' };
var config = this.obj;
config.should.have.property('firstname').and.be.equal(first);
config.firstname.should.be.instanceOf(String);
config.should.have.property('lastname').and.be.equal(last);
config.lastname.should.be.instanceOf(String);
config.should.have.property('id').and.be.equal(id);
config.id.should.be.instanceOf(Number);
}, true);
should.Assertion.add('validOrder', function(desc, id) {
this.params = { operator: 'to be a valid order' };
var config = this.obj;
config.should.have.property('desc').and.be.equal(desc);
config.desc.should.be.instanceOf(String);
config.should.have.property('customerId').and.be.equal(id);
config.id.should.be.instanceOf(Number);
}, true);
should.Assertion.add('validItem', function(desc, id) {
this.params = { operator: 'to be a valid item' };
var config = this.obj;
config.should.have.property('desc').and.be.equal(desc);
config.desc.should.be.instanceOf(String);
config.should.have.property('orderId').and.be.equal(id);
config.id.should.be.instanceOf(Number);
}, true);
describe('Test Example Order System Rest APIs using supertest', function() {
var app = request('http://localhost:3000');
// {
// id: Number,
// firstname: String,
// lastname: String
// }
it('Validate Customer #1', function () {
app
.get('/customer/1')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
// individual assertions
res.body.should.have.property('firstname').and.be.equal("John");
res.body.firstname.should.be.instanceOf(String);
res.body.should.have.property('lastname').and.be.equal("Smith");
res.body.lastname.should.be.instanceOf(String);
res.body.should.have.property('id').and.be.equal(1);
res.body.id.should.be.instanceOf(Number);
console.log("Customer #1 Validated");
});
});
it('Validate Customer #2', function () {
app
.get('/customer/2')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
// customer assertion - validCustomer()
res.body.should.have.validCustomer("John", "Doe", 2);
console.log("Customer #2 Validated");
});
});
// {
// id: number,
// desc: String,
// customerId: Number
// }
it('Validate Order #1 for Customer #1', function () {
app
.get('/order?customerId=1')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
// customer assertion - validOrder()
res.body[0].should.have.validOrder("First order", 1);
console.log("Order #1 for Customer #1 Validated");
});
});
it('Validate Order #1 for Customer #2', function () {
app
.get('/order?customerId=2')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
// customer assertion - validOrder()
res.body[0].should.have.validOrder("Repeat Customer", 2);
console.log("Order #1 for Customer #2 Validated");
});
});
// {
// id: number,
// desc: String,
// orderId: Number
// }
it('Validate Items in Order #1 for Customer #1', function () {
app
.get('/item?orderId=1')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Found items for order #1, customer #1");
res.body.should.be.instanceof(Array).and.have.lengthOf(3);
res.body[0].should.have.validItem("Bike", 1);
res.body[1].should.have.validItem("Gas", 1);
res.body[2].should.have.validItem("Car", 1);
console.log("Items for Order #1 for Customer #1 Validated");
});
});
it('Validate Items in Order #1 for Customer #2', function () {
app
.get('/item?orderId=2')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Found items for order #1, customer #2");
res.body.should.be.instanceof(Array).and.have.lengthOf(3);
res.body[0].should.have.validItem("Table", 2);
res.body[1].should.have.validItem("Chairs", 2);
res.body[2].should.have.validItem("Dishes", 2);
console.log("Items for Order #1 for Customer #2 Validated");
});
});
it('Create New Customer #3', function () {
var cust = { firstname : 'Jane', lastname : 'Doe'};
app
.post('/customer')
.send(cust)
.set('Accept', 'application/json')
.expect(201)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
res.body.should.have.validCustomer("Jane", "Doe", 3);
console.log("New Customer #3 Created");
});
});
it('Create New Order #1 for Customer #3', function () {
var order = { desc : 'Business Order', customerId : 3};
app
.post('/order')
.send(order)
.set('Accept', 'application/json')
.expect(201)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
res.body.should.have.validOrder("Business Order", 3);
console.log("New Order #1 for Customer #3 Created");
});
});
it('Create 2 New Items for Order #1 for Customer #3', function () {
var item1 = { desc : 'Books', orderId : 3};
var item2 = { desc : 'Shelf', orderId : 3};
app
.post('/item')
.send(item1)
.set('Accept', 'application/json')
.expect(201)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Added item #1 for order #1, customer #3");
res.body.should.have.validItem("Books", 3);
});
app
.post('/item')
.send(item2)
.set('Accept', 'application/json')
.expect(201)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Added item #2 for order #1, customer #3");
res.body.should.have.validItem("Shelf", 3);
});
});
it('Delete Items, then Order then Customer all for #3', function () {
app
.delete('/item/7')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Deleted item #1 for order #1, customer #3");
});
app
.delete('/item/8')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Deleted item #2 for order #1, customer #3");
});
app
.delete('/order/3')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Deleted order #1, customer #3");
});
app
.delete('/customer/3')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return err;
console.log("Deleted ocustomer #3");
});
});
});