Skip to content

Commit

Permalink
feat: add support for body raw language
Browse files Browse the repository at this point in the history
  • Loading branch information
sasisalt authored Aug 12, 2020
1 parent 0a7ffb6 commit e23b212
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 55 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ func main() {

c.AddItemGroup("A folder").AddItem(&postman.Item{
Name: "This is a request",
Request: postman.CreateRequest("http://www.google.fr", postman.Get),
Request: Request{
URL: &URL{
Raw: "http://www.google.fr",
},
Method: postman.Get,
},
})

file, err := os.Create("postman_collection.json")
Expand All @@ -83,7 +88,12 @@ func main() {
// Create a simple item.
item := postman.CreateItem(postman.Item{
Name: "A basic request",
Request: postman.CreateRequest("http://www.google.fr", postman.Get),
Request: Request{
URL: &URL{
Raw: "http://www.google.fr",
},
Method: postman.Get,
}
})

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

```go
// Basic request
req := postman.CreateRequest("http://www.google.fr", postman.Get)
req := Request{
URL: &URL{
Raw: "http://www.google.fr",
},
Method: postman.Get,
}

// Complex request
req := postman.Request{
Expand Down
34 changes: 27 additions & 7 deletions body.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package postman

// These constants represent the available raw languages.
const (
HTML string = "html"
Javascript string = "javascript"
JSON string = "json"
Text string = "text"
XML string = "xml"
)

// Body represents the data usually contained in the request body.
type Body struct {
Mode string `json:"mode"`
Raw string `json:"raw,omitempty"`
URLEncoded interface{} `json:"urlencoded,omitempty"`
FormData interface{} `json:"formdata,omitempty"`
File interface{} `json:"file,omitempty"`
GraphQL interface{} `json:"graphql,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Mode string `json:"mode"`
Raw string `json:"raw,omitempty"`
URLEncoded interface{} `json:"urlencoded,omitempty"`
FormData interface{} `json:"formdata,omitempty"`
File interface{} `json:"file,omitempty"`
GraphQL interface{} `json:"graphql,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Options *BodyOptions `json:"options,omitempty"`
}

// BodyOptions holds body options.
type BodyOptions struct {
Raw BodyOptionsRaw `json:"raw,omitempty"`
}

// BodyOptionsRaw represents the acutal language to use in postman. (See possible options in the cost above)
type BodyOptionsRaw struct {
Language string `json:"language,omitempty"`
}
118 changes: 113 additions & 5 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func (suite *CollectionTestSuite) SetupTest() {
},
},
Body: &Body{
Mode: "raw",
Raw: "{\"aKey\":\"a-value\"}",
Mode: "raw",
Raw: "{\"aKey\":\"a-value\"}",
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
},
},
},
Expand Down Expand Up @@ -162,8 +163,9 @@ func (suite *CollectionTestSuite) SetupTest() {
},
},
Body: &Body{
Mode: "raw",
Raw: "{\"aKey\":\"a-value\"}",
Mode: "raw",
Raw: "{\"aKey\":\"a-value\"}",
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
},
},
},
Expand Down Expand Up @@ -258,7 +260,6 @@ func (suite *CollectionTestSuite) TestParseCollection() {
file, _ := os.Open(tc.testFile)

c, err := ParseCollection(file)

assert.Equal(suite.T(), tc.expectedError, err, tc.scenario)
assert.Equal(suite.T(), tc.expectedCollection, c, tc.scenario)
}
Expand Down Expand Up @@ -293,3 +294,110 @@ func (suite *CollectionTestSuite) TestWriteCollection() {
assert.Equal(suite.T(), string(file), fmt.Sprintf("%s\n", buf.String()), tc.scenario)
}
}

func (suite *CollectionTestSuite) TestSimplePOSTItem() {
c := CreateCollection("Test Collection", "My Test Collection")

file, err := os.Create("postman_collection.json")
assert.Nil(suite.T(), err)
assert.NotNil(suite.T(), file)

defer file.Close()

pURL := URL{
Raw: "https://test.com",
Protocol: "https",
Host: []string{"test", "com"},
}

headers := []*Header{{
Key: "h1",
Value: "h1-value",
}}

pBody := Body{
Mode: "raw",
Raw: "{\"a\":\"1234\",\"b\":123}",
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
}

pReq := Request{
Method: Post,
URL: &pURL,
Header: headers,
Body: &pBody,
}

cr := Request{
Method: Post,
URL: &pURL,
Header: pReq.Header,
Body: pReq.Body,
}

item := CreateItem(Item{
Name: "Test-POST",
Request: &cr,
})

c.AddItemGroup("grp1").AddItem(item)

err = c.Write(file, V210)
assert.Nil(suite.T(), err)

err = os.Remove("postman_collection.json")
assert.Nil(suite.T(), err)
}

func (suite *CollectionTestSuite) TestSimpleGETItem() {
c := CreateCollection("Test Collection", "My Test Collection")

file, err := os.Create("postman_collection.json")
assert.Nil(suite.T(), err)
assert.NotNil(suite.T(), file)

defer file.Close()

m1 := map[string]interface{}{"key": "param1", "value": "value1"}
m2 := map[string]interface{}{"key": "param2", "value": "value2"}

var arrMaps []map[string]interface{}
arrMaps = append(arrMaps, m1)
arrMaps = append(arrMaps, m2)

pURL := URL{
Raw: "https://test.com?a=3",
Protocol: "https",
Host: []string{"test", "com"},
Query: arrMaps,
}

headers := []*Header{}
headers = append(headers, &Header{
Key: "h1",
Value: "h1-value",
})
headers = append(headers, &Header{
Key: "h2",
Value: "h2-value",
})

pReq := Request{
Method: Get,
URL: &pURL,
Header: headers,
}

item := CreateItem(Item{
Name: "Test-GET",
Request: &pReq,
})

c.AddItemGroup("grp1").AddItem(item)

err = c.Write(file, V210)
assert.Nil(suite.T(), err)

err = os.Remove("postman_collection.json")
assert.Nil(suite.T(), err)
}
10 changes: 0 additions & 10 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ type Request struct {
// mRequest is used for marshalling/unmarshalling.
type mRequest Request

// CreateRequest creates a new request.
func CreateRequest(u string, m method) *Request {
return &Request{
URL: &URL{
Raw: u,
},
Method: m,
}
}

// MarshalJSON returns the JSON encoding of a Request.
// If the Request only contains an URL with the Get HTTP method, it is returned as a string.
func (r Request) MarshalJSON() ([]byte, error) {
Expand Down
32 changes: 4 additions & 28 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCreateRequest(t *testing.T) {
cases := []struct {
method method
url string
expectedRequest *Request
}{
{
Get,
"an-url",
&Request{
Method: Get,
URL: &URL{
Raw: "an-url",
},
},
},
}

for _, tc := range cases {
req := CreateRequest(tc.url, tc.method)

assert.Equal(t, tc.expectedRequest, req)
}
}

func TestRequestMarshalJSON(t *testing.T) {
cases := []struct {
scenario string
Expand All @@ -58,11 +33,12 @@ func TestRequestMarshalJSON(t *testing.T) {
version: V200,
},
Body: &Body{
Mode: "raw",
Raw: "raw-content",
Mode: "raw",
Raw: "raw-content",
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
},
},
"{\"url\":\"http://www.google.fr\",\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\"}}",
"{\"url\":\"http://www.google.fr\",\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\",\"options\":{\"raw\":{\"language\":\"json\"}}}}",
},
{
"Successfully marshalling a Request as an object (v2.1.0)",
Expand Down
7 changes: 6 additions & 1 deletion testdata/collection_v2.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
],
"body": {
"mode": "raw",
"raw": "{\"aKey\":\"a-value\"}"
"raw": "{\"aKey\":\"a-value\"}",
"options": {
"raw": {
"language": "json"
}
}
}
}
},
Expand Down
7 changes: 6 additions & 1 deletion testdata/collection_v2.1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@
],
"body": {
"mode": "raw",
"raw": "{\"aKey\":\"a-value\"}"
"raw": "{\"aKey\":\"a-value\"}",
"options": {
"raw": {
"language": "json"
}
}
}
}
},
Expand Down

0 comments on commit e23b212

Please sign in to comment.