forked from DevMountain/node-2-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postman_collection.json
494 lines (494 loc) · 13.6 KB
/
postman_collection.json
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
{
"info": {
"_postman_id": "548d4d10-03b7-41d9-bcf5-37ab2c508eb5",
"name": "node_introduction copy",
"description": "node_introduction Unit Tests",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "POST: /api/books",
"event": [
{
"listen": "test",
"script": {
"id": "5b6c06fc-8b65-4ac4-af25-7579ddff39b6",
"type": "text/javascript",
"exec": [
"let isJSON = true;",
"let books = [];",
"try {",
" books = pm.response.json();",
"} catch(err) {",
" isJSON = false;",
"}",
"",
"pm.test(`POST to /api/books/ should return JSON`, () => {",
" pm.expect(isJSON).to.be.true;",
"});",
"",
"pm.test('POST to /api/books should respond with a 200, 201, or 202 status code', () => {",
" pm.expect(pm.response.code).to.be.oneOf( [200, 201, 202] )",
"});",
"",
"pm.test('POST to /api/books should respond with an array that contains the book sent in the request body', () => {",
" let postTitle = pm.environment.get(\"postTitle\");",
" let postAuthor = pm.environment.get(\"postAuthor\");",
" const index = books.findIndex( book => {",
" // console.log(book.title, book.author, book1title, book1author)",
" return book.title === postTitle && book.author === postAuthor;",
" })",
" pm.expect(index).to.not.equal(-1);",
"});",
""
]
}
},
{
"listen": "prerequest",
"script": {
"id": "65d982d5-b614-4808-ba9f-d36ce72ca734",
"type": "text/javascript",
"exec": [
"let book0title = 'title' + Math.floor( Math.random() * 1000);",
"let book0author = 'author' + Math.floor( Math.random() * 1000);",
"",
"pm.environment.set(\"postTitle\", book0title);",
"pm.environment.set(\"postAuthor\", book0author);",
""
]
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"title\": \"{{postTitle}}\",\n\t\"author\": \"{{postAuthor}}\"\n}"
},
"url": {
"raw": "http://localhost:4000/api/books",
"protocol": "http",
"host": [
"localhost"
],
"port": "4000",
"path": [
"api",
"books"
]
}
},
"response": []
},
{
"name": "GET: /api/books",
"event": [
{
"listen": "test",
"script": {
"id": "45e15d0a-01cf-4de6-ba4d-260570b806e0",
"type": "text/javascript",
"exec": [
"pm.test('GET to /api/books can only be tested properly if POST endpoint doesn\\'t have errors. No errors reported in pre-test POST request.', () => {",
" let postError1 = pm.environment.get(\"getpreposterr1\");",
" let postError2 = pm.environment.get(\"getpreposterr2\");",
" let isErr = postError1 || postError2;",
" pm.environment.unset(\"getpreposterr\");",
" pm.expect(isErr).to.be.false;",
"})",
" ",
"",
"pm.test('GET to /api/books should respond with a 200 status code', () => {",
" pm.expect(pm.response.code).to.eql(200);",
"})",
"",
"pm.test('GET to /api/books should return an array with at least two books with defined book and author properties.', () => {",
" const res = pm.response.json();",
" const length = res.length;",
" const isArray = Array.isArray(res);",
" const item1book = res[0] ? res[0].title : null;",
" const item1author = res[0] ? res[0].author : null;",
" const item2book = res[1] ? res[1].title : null;",
" const item2author = res[1] ? res[1].author : null;",
" const item1vals = item1book && item1author ? true : false;",
" const item2vals = item2book && item2author ? true : false;",
" pm.expect(item1vals).to.be.true;",
" pm.expect(item2vals).to.be.true;",
" pm.expect(isArray).to.be.true;",
" pm.expect(res.length >= 2).to.be.true;",
"})",
"",
"pm.test('GET to /api/books should return an array with at least two books and no duplicate IDs', () => {",
" const response = pm.response.json();",
" const length = response.length;",
" let uniqArr = [];",
" let duplicatesExist = false;",
" response.forEach( book => {",
" if(uniqArr.includes(book.id)) {",
" console.log(uniqArr, book.id)",
" duplicatesExist = true;",
" } else {",
" uniqArr.push(book.id);",
" }",
" })",
" pm.expect(duplicatesExist).to.be.false;",
" pm.expect(response.length >= 2).to.be.true",
"})",
"",
"",
""
]
}
},
{
"listen": "prerequest",
"script": {
"id": "7314a215-3f63-4dad-a561-f344e9584d5f",
"type": "text/javascript",
"exec": [
"const postRequest1 = {",
" url: 'http://localhost:4000/api/books',",
" method: 'POST',",
" header: {",
" 'content-type': 'application/json'",
" },",
" body: {",
" mode: 'raw',",
" raw: JSON.stringify({ title: \"Book 3\", author: \"Author 3\"})",
" }",
"};",
"",
"const postRequest2 = {",
" url: 'http://localhost:4000/api/books',",
" method: 'POST',",
" header: {",
" 'content-type': 'application/json'",
" },",
" body: {",
" mode: 'raw',",
" raw: JSON.stringify({ title: \"Book 4\", author: \"Author 4\"})",
" }",
"};",
"",
"pm.sendRequest(postRequest1, (err, data) => {",
" console.log({err, data});",
" let goodCodes = [200, 201, 202];",
" let badResponse = !goodCodes.includes(data.code);",
" if(err || badResponse) {",
" console.log('efff')",
" postErr = true;",
" pm.environment.set(\"getpreposterr1\", true);",
" } else {",
" pm.environment.set(\"getpreposterr1\", false);",
" }",
"});",
"",
"pm.sendRequest(postRequest2, (err, data) => {",
" let goodCodes = [200, 201, 202];",
" let badResponse = !goodCodes.includes(data.code);",
" if(err || badResponse) {",
" console.log('efff')",
" postErr = true;",
" pm.environment.set(\"getpreposterr2\", true);",
" } else {",
" pm.environment.set(\"getpreposterr2\", false);",
" }",
"});",
"",
"",
"",
"",
"",
""
]
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:4000/api/books",
"protocol": "http",
"host": [
"localhost"
],
"port": "4000",
"path": [
"api",
"books"
]
}
},
"response": []
},
{
"name": "PUT: /api/books",
"event": [
{
"listen": "test",
"script": {
"id": "a40ddc50-b33b-4115-bc00-a0c8b32329a5",
"type": "text/javascript",
"exec": [
"let isJSON = true;",
"let books = [];",
"try {",
" books = pm.response.json();",
"} catch(err) {",
" isJSON = false;",
"}",
"",
"let targetId = pm.environment.get(\"putid\");",
"let foundBook = books.find( book => book.id === +targetId);",
"let title = foundBook ? foundBook.title : 'Error';",
"let author = foundBook ? foundBook.author : 'Error';",
"",
"",
"",
"pm.test(`PUT to /api/books/${targetId} should return JSON`, () => {",
" pm.expect(isJSON).to.be.true;",
"})",
"",
"pm.test(`PUT to /api/books/${targetId} should return status 200, 201, or 202`, () => {",
" pm.expect(pm.response.code).to.be.oneOf([200, 201, 202])",
"})",
"",
"pm.test(`PUT to /api/books/${targetId} should update the title of the book with the ID of ${targetId}`, () => {",
" pm.expect( title ).to.eql( \"Updated Book\" )",
" pm.expect(targetId).to.not.equal(undefined)",
"})",
"",
"pm.test(`PUT to /api/books/${targetId} should update the author of the book with the ID of ${targetId}`, () => {",
" pm.expect( author ).to.eql( \"Updated Author\" )",
" pm.expect(targetId).to.not.equal(undefined)",
"})",
""
]
}
},
{
"listen": "prerequest",
"script": {
"id": "9d1ca18f-cccb-43b4-af23-c2d5dfa2cff7",
"type": "text/javascript",
"exec": [
"const postRequest = {",
" url: 'http://localhost:4000/api/books',",
" method: 'POST',",
" header: {",
" 'content-type': 'application/json'",
" },",
" body: {",
" mode: 'raw',",
" raw: JSON.stringify({ title: \"Book 4\", author: \"Author 4\"})",
" }",
"};",
"",
"pm.sendRequest(postRequest, function(err, response) {",
" const startResponse = response.json();",
" let testBook = startResponse.find( book => book.title !== 'Updated Book' && book.author !== 'Updated Author');",
" console.log(startResponse, testBook);",
" let {id, author, title } = testBook;",
" ",
" pm.environment.set(\"putid\", id);",
"});",
"",
"",
""
]
}
}
],
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"title\": \"Updated Book\",\n\t\"author\": \"Updated Author\"\n}"
},
"url": {
"raw": "http://localhost:4000/api/books/{{putid}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "4000",
"path": [
"api",
"books",
"{{putid}}"
]
}
},
"response": []
},
{
"name": "DELETE: /api/books",
"event": [
{
"listen": "test",
"script": {
"id": "374c191c-f984-4fc6-90d8-719b512e7741",
"type": "text/javascript",
"exec": [
"const targetId = pm.environment.get(\"deleteid\");",
"let isJSON = true;",
"let books = [];",
"try {",
" books = pm.response.json();",
"} catch(err) {",
" isJSON = false;",
"}",
"",
"pm.test(`DELETE to /api/books/${targetId} should return JSON`, () => {",
" pm.expect(isJSON).to.be.true;",
"})",
"",
"pm.test(`DELETE to /api/books/${targetId} should return a status code of 200`, () => {",
" pm.response.to.have.status(200);",
"})",
"",
"",
"pm.test(`DELETE to /api/books/${targetId} should respond with an array that does not include a book with an id of ${targetId}`, () => {",
" const index = books.findIndex( book => book.id === +targetId )",
" pm.expect(targetId).to.not.equal(undefined)",
" pm.expect(index).to.eql(-1)",
"})"
]
}
},
{
"listen": "prerequest",
"script": {
"id": "f92ecb7b-d56f-4282-badb-c4adfb87fae8",
"type": "text/javascript",
"exec": [
"const postRequest = {",
" url: 'http://localhost:4000/api/books',",
" method: 'POST',",
" header: {",
" 'content-type': 'application/json'",
" },",
" body: {",
" mode: 'raw',",
" raw: JSON.stringify({ title: \"Book 4\", author: \"Author 4\"})",
" }",
"};",
"",
"pm.sendRequest(postRequest, function (err, response) {",
" const startResponse = response.json();",
" const randomIndex = Math.floor(Math.random() * startResponse.length)",
" const randomId = startResponse[randomIndex].id;",
" pm.environment.set(\"deleteid\", randomId);",
"});",
""
]
}
}
],
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:4000/api/books/{{deleteid}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "4000",
"path": [
"api",
"books",
"{{deleteid}}"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "5b8b81e8-b04c-4cba-8d8d-56143fcb70c0",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "5867e2b5-19bf-406c-9862-7707c455deae",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"id": "ffa184f0-47e0-4aeb-92c6-59997cb98f2d",
"key": "putid",
"value": "0",
"type": "string"
},
{
"id": "4eb8f46f-ea78-4c0c-bd0b-894b49c73905",
"key": "getpreposterr",
"value": "false",
"type": "string"
},
{
"id": "11851db0-0592-4aa8-ba8a-5b0e60b48536",
"key": "book0title",
"value": "comeon",
"type": "string"
},
{
"id": "00170a70-0780-42be-90a7-60b5a0923e2f",
"key": "book0author",
"value": "pleasework",
"type": "string"
},
{
"id": "0839c63f-a7e1-413a-8589-2fd351cebf94",
"key": "deleteid",
"value": "0",
"type": "string"
}
]
}