Skip to content

Commit e23b212

Browse files
authored
feat: add support for body raw language
1 parent 0a7ffb6 commit e23b212

7 files changed

+174
-55
lines changed

README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func main() {
5757

5858
c.AddItemGroup("A folder").AddItem(&postman.Item{
5959
Name: "This is a request",
60-
Request: postman.CreateRequest("http://www.google.fr", postman.Get),
60+
Request: Request{
61+
URL: &URL{
62+
Raw: "http://www.google.fr",
63+
},
64+
Method: postman.Get,
65+
},
6166
})
6267

6368
file, err := os.Create("postman_collection.json")
@@ -83,7 +88,12 @@ func main() {
8388
// Create a simple item.
8489
item := postman.CreateItem(postman.Item{
8590
Name: "A basic request",
86-
Request: postman.CreateRequest("http://www.google.fr", postman.Get),
91+
Request: Request{
92+
URL: &URL{
93+
Raw: "http://www.google.fr",
94+
},
95+
Method: postman.Get,
96+
}
8797
})
8898

8999
// Create a simple folder.
@@ -101,7 +111,12 @@ Part of the `Item`, a `Request` represents an HTTP request.
101111

102112
```go
103113
// Basic request
104-
req := postman.CreateRequest("http://www.google.fr", postman.Get)
114+
req := Request{
115+
URL: &URL{
116+
Raw: "http://www.google.fr",
117+
},
118+
Method: postman.Get,
119+
}
105120

106121
// Complex request
107122
req := postman.Request{

body.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package postman
22

3+
// These constants represent the available raw languages.
4+
const (
5+
HTML string = "html"
6+
Javascript string = "javascript"
7+
JSON string = "json"
8+
Text string = "text"
9+
XML string = "xml"
10+
)
11+
312
// Body represents the data usually contained in the request body.
413
type Body struct {
5-
Mode string `json:"mode"`
6-
Raw string `json:"raw,omitempty"`
7-
URLEncoded interface{} `json:"urlencoded,omitempty"`
8-
FormData interface{} `json:"formdata,omitempty"`
9-
File interface{} `json:"file,omitempty"`
10-
GraphQL interface{} `json:"graphql,omitempty"`
11-
Disabled bool `json:"disabled,omitempty"`
14+
Mode string `json:"mode"`
15+
Raw string `json:"raw,omitempty"`
16+
URLEncoded interface{} `json:"urlencoded,omitempty"`
17+
FormData interface{} `json:"formdata,omitempty"`
18+
File interface{} `json:"file,omitempty"`
19+
GraphQL interface{} `json:"graphql,omitempty"`
20+
Disabled bool `json:"disabled,omitempty"`
21+
Options *BodyOptions `json:"options,omitempty"`
22+
}
23+
24+
// BodyOptions holds body options.
25+
type BodyOptions struct {
26+
Raw BodyOptionsRaw `json:"raw,omitempty"`
27+
}
28+
29+
// BodyOptionsRaw represents the acutal language to use in postman. (See possible options in the cost above)
30+
type BodyOptionsRaw struct {
31+
Language string `json:"language,omitempty"`
1232
}

collection_test.go

+113-5
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ func (suite *CollectionTestSuite) SetupTest() {
7474
},
7575
},
7676
Body: &Body{
77-
Mode: "raw",
78-
Raw: "{\"aKey\":\"a-value\"}",
77+
Mode: "raw",
78+
Raw: "{\"aKey\":\"a-value\"}",
79+
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
7980
},
8081
},
8182
},
@@ -162,8 +163,9 @@ func (suite *CollectionTestSuite) SetupTest() {
162163
},
163164
},
164165
Body: &Body{
165-
Mode: "raw",
166-
Raw: "{\"aKey\":\"a-value\"}",
166+
Mode: "raw",
167+
Raw: "{\"aKey\":\"a-value\"}",
168+
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
167169
},
168170
},
169171
},
@@ -258,7 +260,6 @@ func (suite *CollectionTestSuite) TestParseCollection() {
258260
file, _ := os.Open(tc.testFile)
259261

260262
c, err := ParseCollection(file)
261-
262263
assert.Equal(suite.T(), tc.expectedError, err, tc.scenario)
263264
assert.Equal(suite.T(), tc.expectedCollection, c, tc.scenario)
264265
}
@@ -293,3 +294,110 @@ func (suite *CollectionTestSuite) TestWriteCollection() {
293294
assert.Equal(suite.T(), string(file), fmt.Sprintf("%s\n", buf.String()), tc.scenario)
294295
}
295296
}
297+
298+
func (suite *CollectionTestSuite) TestSimplePOSTItem() {
299+
c := CreateCollection("Test Collection", "My Test Collection")
300+
301+
file, err := os.Create("postman_collection.json")
302+
assert.Nil(suite.T(), err)
303+
assert.NotNil(suite.T(), file)
304+
305+
defer file.Close()
306+
307+
pURL := URL{
308+
Raw: "https://test.com",
309+
Protocol: "https",
310+
Host: []string{"test", "com"},
311+
}
312+
313+
headers := []*Header{{
314+
Key: "h1",
315+
Value: "h1-value",
316+
}}
317+
318+
pBody := Body{
319+
Mode: "raw",
320+
Raw: "{\"a\":\"1234\",\"b\":123}",
321+
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
322+
}
323+
324+
pReq := Request{
325+
Method: Post,
326+
URL: &pURL,
327+
Header: headers,
328+
Body: &pBody,
329+
}
330+
331+
cr := Request{
332+
Method: Post,
333+
URL: &pURL,
334+
Header: pReq.Header,
335+
Body: pReq.Body,
336+
}
337+
338+
item := CreateItem(Item{
339+
Name: "Test-POST",
340+
Request: &cr,
341+
})
342+
343+
c.AddItemGroup("grp1").AddItem(item)
344+
345+
err = c.Write(file, V210)
346+
assert.Nil(suite.T(), err)
347+
348+
err = os.Remove("postman_collection.json")
349+
assert.Nil(suite.T(), err)
350+
}
351+
352+
func (suite *CollectionTestSuite) TestSimpleGETItem() {
353+
c := CreateCollection("Test Collection", "My Test Collection")
354+
355+
file, err := os.Create("postman_collection.json")
356+
assert.Nil(suite.T(), err)
357+
assert.NotNil(suite.T(), file)
358+
359+
defer file.Close()
360+
361+
m1 := map[string]interface{}{"key": "param1", "value": "value1"}
362+
m2 := map[string]interface{}{"key": "param2", "value": "value2"}
363+
364+
var arrMaps []map[string]interface{}
365+
arrMaps = append(arrMaps, m1)
366+
arrMaps = append(arrMaps, m2)
367+
368+
pURL := URL{
369+
Raw: "https://test.com?a=3",
370+
Protocol: "https",
371+
Host: []string{"test", "com"},
372+
Query: arrMaps,
373+
}
374+
375+
headers := []*Header{}
376+
headers = append(headers, &Header{
377+
Key: "h1",
378+
Value: "h1-value",
379+
})
380+
headers = append(headers, &Header{
381+
Key: "h2",
382+
Value: "h2-value",
383+
})
384+
385+
pReq := Request{
386+
Method: Get,
387+
URL: &pURL,
388+
Header: headers,
389+
}
390+
391+
item := CreateItem(Item{
392+
Name: "Test-GET",
393+
Request: &pReq,
394+
})
395+
396+
c.AddItemGroup("grp1").AddItem(item)
397+
398+
err = c.Write(file, V210)
399+
assert.Nil(suite.T(), err)
400+
401+
err = os.Remove("postman_collection.json")
402+
assert.Nil(suite.T(), err)
403+
}

request.go

-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ type Request struct {
2121
// mRequest is used for marshalling/unmarshalling.
2222
type mRequest Request
2323

24-
// CreateRequest creates a new request.
25-
func CreateRequest(u string, m method) *Request {
26-
return &Request{
27-
URL: &URL{
28-
Raw: u,
29-
},
30-
Method: m,
31-
}
32-
}
33-
3424
// MarshalJSON returns the JSON encoding of a Request.
3525
// If the Request only contains an URL with the Get HTTP method, it is returned as a string.
3626
func (r Request) MarshalJSON() ([]byte, error) {

request_test.go

+4-28
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,6 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestCreateRequest(t *testing.T) {
11-
cases := []struct {
12-
method method
13-
url string
14-
expectedRequest *Request
15-
}{
16-
{
17-
Get,
18-
"an-url",
19-
&Request{
20-
Method: Get,
21-
URL: &URL{
22-
Raw: "an-url",
23-
},
24-
},
25-
},
26-
}
27-
28-
for _, tc := range cases {
29-
req := CreateRequest(tc.url, tc.method)
30-
31-
assert.Equal(t, tc.expectedRequest, req)
32-
}
33-
}
34-
3510
func TestRequestMarshalJSON(t *testing.T) {
3611
cases := []struct {
3712
scenario string
@@ -58,11 +33,12 @@ func TestRequestMarshalJSON(t *testing.T) {
5833
version: V200,
5934
},
6035
Body: &Body{
61-
Mode: "raw",
62-
Raw: "raw-content",
36+
Mode: "raw",
37+
Raw: "raw-content",
38+
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
6339
},
6440
},
65-
"{\"url\":\"http://www.google.fr\",\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\"}}",
41+
"{\"url\":\"http://www.google.fr\",\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\",\"options\":{\"raw\":{\"language\":\"json\"}}}}",
6642
},
6743
{
6844
"Successfully marshalling a Request as an object (v2.1.0)",

testdata/collection_v2.0.0.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343
],
4444
"body": {
4545
"mode": "raw",
46-
"raw": "{\"aKey\":\"a-value\"}"
46+
"raw": "{\"aKey\":\"a-value\"}",
47+
"options": {
48+
"raw": {
49+
"language": "json"
50+
}
51+
}
4752
}
4853
}
4954
},

testdata/collection_v2.1.0.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@
6565
],
6666
"body": {
6767
"mode": "raw",
68-
"raw": "{\"aKey\":\"a-value\"}"
68+
"raw": "{\"aKey\":\"a-value\"}",
69+
"options": {
70+
"raw": {
71+
"language": "json"
72+
}
73+
}
6974
}
7075
}
7176
},

0 commit comments

Comments
 (0)