Skip to content

Commit

Permalink
fix: cannot import sub-collection of postman (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYSF authored May 13, 2024
1 parent 7896523 commit 76c60b3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 17 deletions.
45 changes: 28 additions & 17 deletions pkg/generator/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,20 @@ type PostmanRequest struct {
}

type PostmanBody struct {
Mode string `json:"mode"`
Raw string `json:"raw"`
Mode string `json:"mode"`
Raw string `json:"raw"`
FormData []TypeField `json:"formdata"`
URLEncoded []TypeField `json:"urlencoded"`
Disabled bool `json:"disabled"`
}

type TypeField struct {
Key string `json:"key"`
Value string `json:"value"`
Disabled bool `json:"disabled"`
Type string `json:"type"`
Description string `json:"description"`
Src string `json:"src"`
}

type PostmanURL struct {
Expand Down Expand Up @@ -110,30 +122,29 @@ func (p *postmanImporter) Convert(data []byte) (suite *testing.TestSuite, err er

suite = &testing.TestSuite{}
suite.Name = postman.Info.Name
suite.Items = make([]testing.TestCase, len(postman.Item))
if err = p.convertItems(postman.Item, "", suite); err != nil {
return
}

for i, item := range postman.Item {
return
}

func (p *postmanImporter) convertItems(items []PostmanItem, prefix string, suite *testing.TestSuite) (err error) {
for _, item := range items {
itemName := prefix + item.Name
if len(item.Item) == 0 {
suite.Items[i] = testing.TestCase{
Name: item.Name,
suite.Items = append(suite.Items, testing.TestCase{
Name: itemName,
Request: testing.Request{
Method: item.Request.Method,
API: item.Request.URL.Raw,
Body: testing.NewRequestBody(item.Request.Body.Raw),
Header: item.Request.Header.ToMap(),
},
}
})
} else {
for _, sub := range item.Item {
suite.Items[i] = testing.TestCase{
Name: item.Name + " " + sub.Name,
Request: testing.Request{
Method: sub.Request.Method,
API: sub.Request.URL.Raw,
Body: testing.NewRequestBody(item.Request.Body.Raw),
Header: sub.Request.Header.ToMap(),
},
}
if err = p.convertItems(item.Item, itemName+" ", suite); err != nil {
return
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/generator/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ func TestPostmanImport(t *testing.T) {
assert.Equal(t, expectedSuiteFromPostman, strings.TrimSpace(result), result)
})

t.Run("sub postman, from file", func(t *testing.T) {
suite, err := importer.ConvertFromFile("testdata/postman-sub.json")
assert.NoError(t, err)

var result string
result, err = converter.Convert(suite)
assert.NoError(t, err)
assert.Equal(t, expectedSuiteFromSubPostman, strings.TrimSpace(result), result)
})

t.Run("simple postman, from URl", func(t *testing.T) {
defer gock.Off()
gock.New(urlFoo).Get("/").Reply(http.StatusOK).BodyString(simplePostman)
Expand Down Expand Up @@ -96,3 +106,6 @@ var simplePostman string

//go:embed testdata/expected_suite_from_postman.yaml
var expectedSuiteFromPostman string

//go:embed testdata/expected_suite_from_sub_postman.yaml
var expectedSuiteFromSubPostman string
9 changes: 9 additions & 0 deletions pkg/generator/testdata/expected_suite_from_sub_postman.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Sub
items:
- name: Get Sub Get New Request
request:
api: http://localhost?key=value
method: GET
header:
key: value
body: '{}'
41 changes: 41 additions & 0 deletions pkg/generator/testdata/postman-sub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"info": {
"_postman_id": "84b8940a-009e-4127-b84e-f4d6fd6b9972",
"name": "Sub",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "19536120"
},
"item": [
{
"name": "Get",
"item": [
{
"name": "Sub Get",
"item": [
{
"name": "New Request",
"request": {
"method": "GET",
"header": [
{
"key": "key",
"value": "value",
"description": "description",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{}"
},
"url": {
"raw": "http://localhost?key=value"
}
}
}
]
}
]
}
]
}

0 comments on commit 76c60b3

Please sign in to comment.