-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 11- Validation.postman_collection.json
228 lines (228 loc) · 11.1 KB
/
Day 11- Validation.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
{
"info": {
"_postman_id": "436b6020-a379-4492-82cb-a9eb708e4aab",
"name": "Day 11: Validation",
"description": "## Instructions for Day 11: Validation\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- Validate responses against a defined schema\n- Explain the importance of schema validation\n- Write a conditional test to validate various responses\n- Write a negative test to validate an unsuccessful response \n\n## Concepts covered\n* [Writing tests](https://learning.postman.com/docs/writing-scripts/test-scripts/)\n* [Validating responses](https://learning.postman.com/docs/writing-scripts/test-scripts/#validating-responses)\n\n## Additional resources\n\n* [ajv](https://learning.postman.com/docs/sending-requests/grpc/postman-sandbox-api/#using-external-libraries) and other built-in libraries\n* [Validate using ajv](https://www.postman.com/postman/workspace/postman-answers/request/9215231-333ddae8-eb72-496d-9e45-6b49a87c7d09) code sample\n* Validate data formats using [JSON schema](https://json-schema.org/) \n* [Validate API definitions](https://learning.postman.com/docs/designing-and-developing-your-api/developing-an-api/validating-elements-against-schema/) docs\n* [Security warnings during API definition](https://blog.postman.com/security-warnings-during-api-validation/) blog",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "3488052"
},
"item": [
{
"name": "Validation",
"item": [
{
"name": "pokemon berry",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const schema = {",
" 'type': 'object',",
" 'properties': {",
" 'firmness': {",
" type: 'object'",
" },",
" 'name': {",
" type: 'string'",
" },",
" 'growth_time': {",
" type: 'number'",
" },",
" 'size': {",
" type: 'number'",
" }",
" },",
" required: ['flavors', 'size', 'id']",
"};",
"if (pm.response.code === 200){",
" pm.test('Schema is valid', () => {",
" const response = pm.response.json();",
" pm.expect(response).to.have.jsonSchema(schema);",
"})",
"}",
"",
"const errorSchema = {",
" 'properties': {",
" 'message': {",
" 'type': 'string'",
" }",
" }",
"};",
"if (pm.response.code === 404) {",
" pm.test('Error response is valid', () => {",
" pm.expect(pm.response.text()).to.have.jsonSchema(errorSchema);",
" })",
"}",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://pokeapi.co/api/v2/berry/:berryId",
"protocol": "https",
"host": [
"pokeapi",
"co"
],
"path": [
"api",
"v2",
"berry",
":berryId"
],
"variable": [
{
"key": "berryId",
"value": null
}
]
}
},
"response": []
}
],
"description": "Let's use the [Pokemon API](https://pokeapi.co/docs/v2#berries-section) in this challenge.\n\n1. **Add a request**: Add a request called `pokemon berry` to the folder `Validation` with the following details:\n * `GET` method\n * `https://pokeapi.co/api/v2/berry` request URL \n\n **Send** the request, and inspect the response. Update the request URL to include the berry id you want to explore, and **Save** your changes.\n1. **Define the schema**: Under the **Tests** tab, create an object to represent the schema we expect the berry data to match:\n```javascript\nconst schema = {\n 'type': 'object',\n 'properties': {\n 'firmness': {\n type: 'object'\n },\n 'name': {\n type: 'string'\n },\n 'growth_time': {\n type: 'number'\n },\n 'size': {\n type: 'number'\n }\n },\n required: ['flavors', 'size', 'id']\n};\n```\n1. **Validate response against schema**: Add the following test to check the response against the schema.\n```javascript\npm.test('Schema is valid', () => {\n const response = pm.response.json();\n pm.expect(response).to.have.jsonSchema(schema);\n})\n```\n **Send** the request, and verify the test results. Remember to also make sure it fails (e.g. temporarily change one of the schema `type` values an **Send** again). \n1. **Test unsuccessful responses**: Update the request URL to include a berry id for which there is no berry. **Send** the request and inspect the response. \n\n Under the **Tests** tab, create an object to represent the schema we expect an error response to match.\n\n```javascript\nconst errorSchema = {\n 'properties': {\n 'message': {\n 'type': 'string'\n }\n }\n};\nif (pm.response.code === 404) {\n pm.test('Error response is valid', () => {\n pm.expect(pm.response.text()).to.have.jsonSchema(errorSchema);\n })\n}\n```\n\n Add a conditional statement before the test on the successful schema.\n```javascript\n if (pm.response.code === 200)\n```\n\nThere are a number of methods to validate responses. Check out the **Additional resources** section to explore validation against an API definition, using JSON Schema, and using AJV and other built-in libraries.\n\nWrapping a test in an `if` statement is one way to run conditional tests. We will learn other ways in an upcoming challenge.\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 11: Validation\")",
" pass += 1",
"})",
"",
"let folder = collection.item.find(fol => {return fol.name === \"Validation\"})",
"",
"pm.test(\"Requests added correctly\", () => {",
" pm.expect(folder.item.length, 'check number of requests').equals(1)",
"",
" let berryRequest = folder.item.find(req => { return req.name === \"pokemon berry\"})",
" pm.expect(berryRequest.name, 'check name').equals(\"pokemon berry\")",
" pm.expect(berryRequest.request.method, 'check method').equals(\"GET\")",
"",
" pass += 1",
"})",
"",
"",
"pm.test(\"Validation data added\", () => {",
" let berryRequest = folder.item.find(req => { return req.name === \"pokemon berry\"})",
" let event = berryRequest.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 successful').contains(\"200\")",
" pm.expect(script, 'check unsuccessful').contains(\"404\")",
" pm.expect(script, 'check test').contains(\"pm.test\")",
" pm.expect(script, 'check schema').contains(\"jsonSchema\")",
"",
"",
"",
" pass += 1",
"})",
"",
"// visualization for test results",
"let template",
"if (pass == totalToPass) {",
" template = `🍪 passing!",
" <br />",
" <img src=\"https://media4.giphy.com/media/UuebWyG4pts3rboawU/giphy.gif?cid=ecf05e47mgrdpij2xqhmiefstri039d0d0kl7erpduqjgbea&rid=giphy.gif&ct=g\" />",
" `",
"} else {",
" template = `🙅 please try again",
" <br />",
" <img src=\"https://media3.giphy.com/media/3o6ZtnXjuLuNjQpq9i/giphy.gif?cid=ecf05e47nwuwbrkik0my42ek9yfxncw239cwwvdg4scr2pvz&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-436b6020-a379-4492-82cb-a9eb708e4aab",
"type": "string"
}
]
}