-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 12- Use libraries.postman_collection.json
193 lines (193 loc) · 10.5 KB
/
Day 12- Use libraries.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
{
"info": {
"_postman_id": "f7e3b6ef-ca19-40f7-b302-df63cca82ba1",
"name": "Day 12: Use libraries",
"description": "## Instructions for Day 12: Use libraries\n\n1. **Get the challenge:** Fork the parent collection to your own public workspace.\n2. **Read the documentation:** Select the first folder. Expand the context bar on the right to follow the instructions in the collection documentation.\n3. **Submit your solution:** Select the second folder and follow the instructions in the documentation to validate your solution.\n \n## Learning objectives\n- Use built-in libraries\n- Visualize response data using Handlebars.js\n- Define and reference external libraries\n- Reuse test scripts and requests\n- Send asynchronous HTTP requests from scripts\n\n## Concepts covered\n* [Visualizing responses](https://learning.postman.com/docs/sending-requests/visualizer/)\n* [Use external libraries](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries)\n* [Send HTTP request from scripts](https://learning.postman.com/docs/sending-requests/grpc/postman-sandbox-api/#sending-http-request-from-scripts)\n\n## Additional resources\n- [Handlebars.js](https://handlebarsjs.com/) docs\n- [Add external libraries](https://www.postman.com/postman/workspace/postman-answers/collection/13455110-7a6c90f0-0062-4089-b206-27c803dc1c37?ctx=documentation) code samples\n- [Reuse test script](https://www.postman.com/postman/workspace/test-examples-in-postman/collection/1559645-e86aead2-0cb9-4144-b568-46d84251af9b?ctx=documentation) code samples",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "3488052"
},
"item": [
{
"name": "Use libraries",
"item": [
{
"name": "get all movies",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var template = `",
"<h3 background=\"#00FFEF\">Postman Movie Library</h3>",
"{{#each films}}",
"🎬 <b>{{title}}</b> ({{year}}) - {{minutes}}mins. <i>Director: {{director}} </i>",
"{{#if stock}}[Stock: {{stock}}]{{else}}<font color='#ff69b4'>⚠️ Out of stock</font>{{/if}}",
"<br/>",
"{{/each}}",
"`;",
"// Set visualizer",
"pm.visualizer.set(template, {",
" films: pm.response.json()",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://www.unbreakableapi.com/api/movies/all",
"protocol": "https",
"host": [
"www",
"unbreakableapi",
"com"
],
"path": [
"api",
"movies",
"all"
]
}
},
"response": []
}
],
"description": "We have already used built-in libraries like Faker.js and Chai.js. Let's use more libraries and get artistic!\n\n1. **Add a request**: Add a request called `get all movies` to the folder `Use libraries` with the following details:\n * `GET` method\n * `https://www.unbreakableapi.com/api/movies/all` request URL \n\n **Send** the request, and inspect the response.\n1. **Visualize the data**: Under the **Tests** tab, use the [`pm.visualizer.set()` method](https://learning.postman.com/docs/sending-requests/visualizer/#visualizer-api) to initialize an HTML template string and pass in the response data. \n```javascript\nvar template = `\n<h3 background=\"#00FFEF\">Postman Movie Library</h3>\n {{#each films}}\n 🎬 <b>{{title}}</b> ({{year}}) - {{minutes}}mins. <i>Director: {{director}} </i>\n {{#if stock}}[Stock: {{stock}}]{{else}}<font color=red>⚠️ Out of stock</font>{{/if}}\n <br/>\n {{/each}}\n`;\n// Set visualizer\npm.visualizer.set(template, {\n films: pm.response.json()\n});\n```\n\n **Send** the request and inspect the response under the **Visualize** tab. The template is formatted using [Handlebars.js](https://handlebarsjs.com/) syntax. \n1. **Style the data**: Use [any method of styling](https://learning.postman.com/docs/sending-requests/visualizer/#adding-styling-and-interaction-to-visualizations) you prefer. Please also make the \"Out of stock\" text precisely this color: `#ff69b4`. You can add any other personal flair that you'd like. Make sure the request returns a successful response, and save your changes.\n\nIn addition to built-in libraries, you can [import external libraries in the visualization](https://learning.postman.com/docs/sending-requests/visualizer/#using-your-own-libraries) using a `<script>` tag. And you can also use other libraries throughout the [Postman sandbox](https://learning.postman.com/docs/sending-requests/grpc/postman-sandbox-api/#using-external-libraries) under the pre-request or test script sections.\n\n### Reuse requests and scripts across collections\nThere are several ways to use external libraries in Postman. In a similar manner, you can reuse requests across collections by making an extra API call to fetch that request or script. Review these examples of [Reusing a request](https://www.postman.com/postman/workspace/test-examples-in-postman/collection/1559645-e86aead2-0cb9-4144-b568-46d84251af9b) and scripts.\n\nThis method relies on the [`pm.sendRequest()`](https://learning.postman.com/docs/sending-requests/grpc/postman-sandbox-api/#sending-http-request-from-scripts) function to send an API request from a script. Check out the [gist](https://gist.github.com/prashantagarwal/f278b3ca78b3927cde06f68674b82cae).\n\nOnce you complete these steps, move on to the next folder in this collection to submit your solution. Follow the instructions in the request documentation."
},
{
"name": "Submit your solution",
"item": [
{
"name": "submit collection",
"event": [
{
"listen": "test",
"script": {
"exec": [
"// counter for passed tests",
"let pass = 0",
"let totalToPass = 5",
"",
"let collection = pm.response.json().collection",
"",
"pm.test(\"If you have any failures, review the failed test results or ask for support in the community forum. Remember to save your changes if you update the collection. When all of your tests pass, you are done with today's challenge.\", () => {",
" pm.expect(true);",
" pass += 1",
"});",
"",
"pm.test(\"Status code is 200\", () => {",
" pm.response.to.have.status(200);",
" pass += 1",
"});",
"",
"pm.test(\"Correct collection returned\", () => {",
" pm.expect(collection.info.name).equals(\"Day 12: Use libraries\")",
" pass += 1",
"})",
"",
"let folder = collection.item.find(fol => {return fol.name === \"Use libraries\"})",
"",
"pm.test(\"Requests added correctly\", () => {",
" pm.expect(folder.item.length, 'check number of requests').equals(1)",
"",
" let movieRequest = folder.item.find(req => { return req.name === \"get all movies\"})",
" pm.expect(movieRequest.name, 'check name').equals(\"get all movies\")",
" pm.expect(movieRequest.request.method, 'check method').equals(\"GET\")",
" pm.expect(movieRequest.request.url.raw, 'check url').equals(\"https://www.unbreakableapi.com/api/movies/all\")",
"",
" pass += 1",
"})",
"",
"",
"pm.test(\"Visualizer added\", () => {",
" let movieRequest = folder.item.find(req => { return req.name === \"get all movies\"})",
" let event = movieRequest.event.find(e => {return e.listen === \"test\"})",
"",
" let script = event.script.exec.toString()",
" pm.expect(script, 'check parse').contains(\"pm.response.json\")",
" pm.expect(script, 'check test').contains(\"#ff69b4\")",
" pm.expect(script, 'check schema').contains(\"pm.visualizer.set\")",
"",
" pass += 1",
"})",
"",
"// visualization for test results",
"let template",
"if (pass == totalToPass) {",
" template = `🍪 passing!",
" <br />",
" <img src=\"https://media2.giphy.com/media/4Zo41lhzKt6iZ8xff9/giphy.gif?cid=ecf05e47nd1ra6bkpjoaonuce6l676ejjxx9wsrcs39xjqw9&rid=giphy.gif&ct=g\" />",
" `",
"} else {",
" template = `🙅 please try again",
" <br />",
" <img src=\"https://media3.giphy.com/media/b5Cj0cAsg3buE/giphy.gif?cid=ecf05e473tg89sl05k2ed0ucin8ggjr99sor2jjt3sfv3hty&rid=giphy.gif&ct=g\" />",
" `",
"}",
"pm.visualizer.set(template)"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "x-api-key",
"value": "{{postman_api_key}}",
"type": "text"
}
],
"url": {
"raw": "https://api.getpostman.com/collections/{{collection_uid}}",
"protocol": "https",
"host": [
"api",
"getpostman",
"com"
],
"path": [
"collections",
"{{collection_uid}}"
]
},
"description": "It's time to check your collection.\n\n1. **Get the collection ID:** Select the collection in the sidebar. Then in the context bar to the right, select the `Info` icon and copy the collection `ID`.\n1. **Update the request URL:** Update the `collection_uid` in the request URL with the collection `ID` from the previous step, using any method you prefer. ⚠ Remember to add sensitive values like an API key to the `CURRENT VALUE` (and not `INITIAL VALUE`) of your public workspace.\n1. **Validate your solution**: Hit **Send** and look under the **Tests** tab of the server response at the bottom to review your test results.\n \nIf you have any failures, review the failed test results or ask for support in the [community forum](https://community.postman.com/). When all of your tests pass, you are done with today's challenge."
},
"response": []
}
],
"description": "Follow the instructions in the request documentation."
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "collection_uid",
"value": "3488052-f7e3b6ef-ca19-40f7-b302-df63cca82ba1",
"type": "string"
}
]
}