Skip to content

Commit 573e9ef

Browse files
MrMageshalvah
authored andcommitted
Include query parameters and headers in the generated Postman collection. (#537)
* Include query parameters in the generated Postman collection. * Style fixes * Also include headers in the Postman collection. * Also include JSON content type headers for good measure. * Update the tests. * Add a test to validate the body parameters in a Postman collection. * Remove the "Content-Type" header again, as we are actually sending content as form data. * Add a query test and make the tests more deterministic. * PR fixes.
1 parent 5f9babf commit 573e9ef

File tree

7 files changed

+376
-10
lines changed

7 files changed

+376
-10
lines changed

src/Postman/CollectionWriter.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,29 @@ public function getCollection()
5656
return [
5757
'name' => $route['title'] != '' ? $route['title'] : url($route['uri']),
5858
'request' => [
59-
'url' => url($route['uri']),
59+
'url' => url($route['uri']).(collect($route['queryParameters'])->isEmpty()
60+
? ''
61+
: ('?'.implode('&', collect($route['queryParameters'])->map(function ($parameter, $key) {
62+
return $key.'='.($parameter['value'] ?? '');
63+
})->all()))),
6064
'method' => $route['methods'][0],
65+
'header' => collect($route['headers'])
66+
->union([
67+
'Accept' => 'application/json',
68+
])
69+
->map(function ($value, $header) {
70+
return [
71+
'key' => $header,
72+
'value' => $value,
73+
];
74+
})
75+
->values()->all(),
6176
'body' => [
6277
'mode' => $mode,
6378
$mode => collect($route['bodyParameters'])->map(function ($parameter, $key) {
6479
return [
6580
'key' => $key,
66-
'value' => isset($parameter['value']) ? $parameter['value'] : '',
81+
'value' => $parameter['value'] ?? '',
6782
'type' => 'text',
6883
'enabled' => true,
6984
];

tests/Fixtures/collection.json

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
{"variables":[],"info":{"name":"Laravel API","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"Group A","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/localhost\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/localhost\/api\/responseTag","request":{"url":"http:\/\/localhost\/api\/responseTag","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]}
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "Laravel API",
5+
"_postman_id": "",
6+
"description": "",
7+
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "Group A",
12+
"description": "",
13+
"item": [
14+
{
15+
"name": "Example title.",
16+
"request": {
17+
"url": "http:\/\/localhost\/api\/test",
18+
"method": "GET",
19+
"header": [
20+
{
21+
"key": "Accept",
22+
"value": "application/json"
23+
}
24+
],
25+
"body": {
26+
"mode": "formdata",
27+
"formdata": []
28+
},
29+
"description": "This will be the long description.\nIt can also be multiple lines long.",
30+
"response": []
31+
}
32+
},
33+
{
34+
"name": "http:\/\/localhost\/api\/responseTag",
35+
"request": {
36+
"url": "http:\/\/localhost\/api\/responseTag",
37+
"method": "POST",
38+
"header": [
39+
{
40+
"key": "Accept",
41+
"value": "application/json"
42+
}
43+
],
44+
"body": {
45+
"mode": "formdata",
46+
"formdata": []
47+
},
48+
"description": "",
49+
"response": []
50+
}
51+
}
52+
]
53+
}
54+
]
55+
}
Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
{"variables":[],"info":{"name":"Laravel API","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"Group A","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/yourapp.app\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/yourapp.app\/api\/responseTag","request":{"url":"http:\/\/yourapp.app\/api\/responseTag","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]}
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "Laravel API",
5+
"_postman_id": "",
6+
"description": "",
7+
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "Group A",
12+
"description": "",
13+
"item": [
14+
{
15+
"name": "Example title.",
16+
"request": {
17+
"url": "http:\/\/yourapp.app\/api\/test",
18+
"method": "GET",
19+
"header": [
20+
{
21+
"key": "Accept",
22+
"value": "application/json"
23+
}
24+
],
25+
"body": {
26+
"mode": "formdata",
27+
"formdata": []
28+
},
29+
"description": "This will be the long description.\nIt can also be multiple lines long.",
30+
"response": []
31+
}
32+
},
33+
{
34+
"name": "http:\/\/yourapp.app\/api\/responseTag",
35+
"request": {
36+
"url": "http:\/\/yourapp.app\/api\/responseTag",
37+
"method": "POST",
38+
"header": [
39+
{
40+
"key": "Accept",
41+
"value": "application/json"
42+
}
43+
],
44+
"body": {
45+
"mode": "formdata",
46+
"formdata": []
47+
},
48+
"description": "",
49+
"response": []
50+
}
51+
}
52+
]
53+
}
54+
]
55+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "Laravel API",
5+
"_postman_id": "",
6+
"description": "",
7+
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "Group A",
12+
"description": "",
13+
"item": [
14+
{
15+
"name": "http://localhost/api/withBodyParameters",
16+
"request": {
17+
"url": "http:\/\/localhost\/api\/withBodyParameters",
18+
"method": "GET",
19+
"header": [
20+
{
21+
"key": "Accept",
22+
"value": "application/json"
23+
}
24+
],
25+
"body": {
26+
"mode": "formdata",
27+
"formdata": [
28+
{
29+
"key": "user_id",
30+
"value": 9,
31+
"type": "text",
32+
"enabled": true
33+
},
34+
{
35+
"key": "room_id",
36+
"value": "consequatur",
37+
"type": "text",
38+
"enabled": true
39+
},
40+
{
41+
"key": "forever",
42+
"value": false,
43+
"type": "text",
44+
"enabled": true
45+
},
46+
{
47+
"key": "another_one",
48+
"value": 11613.31890586,
49+
"type": "text",
50+
"enabled": true
51+
},
52+
{
53+
"key": "yet_another_param",
54+
"value": [],
55+
"type": "text",
56+
"enabled": true
57+
},
58+
{
59+
"key": "even_more_param",
60+
"value": [],
61+
"type": "text",
62+
"enabled": true
63+
},
64+
{
65+
"key": "book.name",
66+
"value": "consequatur",
67+
"type": "text",
68+
"enabled": true
69+
},
70+
{
71+
"key": "book.author_id",
72+
"value": 17,
73+
"type": "text",
74+
"enabled": true
75+
},
76+
{
77+
"key": "book[pages_count]",
78+
"value": 17,
79+
"type": "text",
80+
"enabled": true
81+
},
82+
{
83+
"key": "ids.*",
84+
"value": 17,
85+
"type": "text",
86+
"enabled": true
87+
},
88+
{
89+
"key": "users.*.first_name",
90+
"value": "John",
91+
"type": "text",
92+
"enabled": true
93+
},
94+
{
95+
"key": "users.*.last_name",
96+
"value": "Doe",
97+
"type": "text",
98+
"enabled": true
99+
}
100+
]
101+
},
102+
"description": "",
103+
"response": []
104+
}
105+
}
106+
]
107+
}
108+
]
109+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "Laravel API",
5+
"_postman_id": "",
6+
"description": "",
7+
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "Group A",
12+
"description": "",
13+
"item": [
14+
{
15+
"name": "http://localhost/api/headers",
16+
"request": {
17+
"url": "http:\/\/localhost\/api\/headers",
18+
"method": "GET",
19+
"header": [
20+
{
21+
"key": "Authorization",
22+
"value": "customAuthToken"
23+
},
24+
{
25+
"key": "Custom-Header",
26+
"value": "NotSoCustom"
27+
},
28+
{
29+
"key": "Accept",
30+
"value": "application/json"
31+
}
32+
],
33+
"body": {
34+
"mode": "formdata",
35+
"formdata": []
36+
},
37+
"description": "",
38+
"response": []
39+
}
40+
}
41+
]
42+
}
43+
]
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "Laravel API",
5+
"_postman_id": "",
6+
"description": "",
7+
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "Group A",
12+
"description": "",
13+
"item": [
14+
{
15+
"name": "http://localhost/api/withQueryParameters",
16+
"request": {
17+
"url": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur",
18+
"method": "GET",
19+
"header": [
20+
{
21+
"key": "Accept",
22+
"value": "application/json"
23+
}
24+
],
25+
"body": {
26+
"mode": "formdata",
27+
"formdata": []
28+
},
29+
"description": "",
30+
"response": []
31+
}
32+
}
33+
]
34+
}
35+
]
36+
}

0 commit comments

Comments
 (0)