-
Notifications
You must be signed in to change notification settings - Fork 10
/
postman_test.go
171 lines (156 loc) · 3.8 KB
/
postman_test.go
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
package postman
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/luraproject/lura/v2/config"
)
func ExampleHandleCollection() {
cfg := config.ServiceConfig{
Port: 8080,
Name: "sample",
Endpoints: []*config.EndpointConfig{
{
Endpoint: "/foo",
Method: "GET",
},
},
}
parsedConfig, _ := Parse(cfg)
ts := httptest.NewServer(http.HandlerFunc(HandleCollection(parsedConfig)))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
fmt.Println(err.Error())
}
c := Collection{}
if err := json.NewDecoder(res.Body).Decode(&c); err != nil {
fmt.Println(err.Error())
}
res.Body.Close()
fmt.Println(c.Info.Name)
fmt.Println(c.Info.Schema)
fmt.Println(len(c.Item))
fmt.Printf("%+v\n", c.Item[0].Name)
fmt.Printf("%+v\n", c.Item[0].Item)
fmt.Printf("%+v\n", c.Item[0].Request.URL.Raw)
fmt.Println(len(c.Variables))
fmt.Printf("%+v\n", c.Variables[0].Key)
fmt.Printf("%+v\n", c.Variables[0].Value)
fmt.Printf("%+v\n", c.Variables[0].Type)
fmt.Printf("%+v\n", c.Variables[1].Key)
fmt.Printf("%+v\n", c.Variables[1].Value)
fmt.Printf("%+v\n", c.Variables[1].Type)
// output:
// sample
// https://schema.getpostman.com/json/collection/v2.1.0/collection.json
// 1
// /foo
// []
// {{SCHEMA}}://{{HOST}}/foo
// 2
// HOST
// localhost:8080
// string
// SCHEMA
// http
// string
}
func ExampleParseError() {
invalidVersion := config.ServiceConfig{
Port: 8080,
Name: "sample",
ExtraConfig: map[string]interface{}{
namespace: map[string]interface{}{
"version": "meh",
},
},
}
invalidServiceConfig := config.ServiceConfig{
Port: 8080,
Name: "sample",
ExtraConfig: map[string]interface{}{
namespace: map[string]interface{}{
"description": 100,
},
},
}
invalidEndpointConfig := config.ServiceConfig{
Port: 8080,
Name: "sample",
Endpoints: []*config.EndpointConfig{
{
Endpoint: "/foo",
Method: "GET",
ExtraConfig: map[string]interface{}{
namespace: map[string]interface{}{
"folder": 1,
},
},
},
},
}
cases := []config.ServiceConfig{invalidVersion, invalidServiceConfig, invalidEndpointConfig}
for _, c := range cases {
if _, err := Parse(c); err != nil {
fmt.Println(err.Error())
}
}
// output:
// the provided version is not in semver format
// invalid service config
// invalid endpoint config: GET /foo
}
func TestParse(t *testing.T) {
tests := map[string]struct {
in string
out string
}{
"Backwards compatibility": {
in: "./test/fixtures/compatibility.json",
out: "./test/fixtures/compatibility.out.json",
},
"Basic collection info": {
in: "./test/fixtures/basic-collection-info.json",
out: "./test/fixtures/basic-collection-info.out.json",
},
"Happy path for folder organization": {
in: "./test/fixtures/folder-happy-path.json",
out: "./test/fixtures/folder-happy-path.out.json",
},
"Endpoint explicitly placed at root": {
in: "./test/fixtures/folder-endpoint-at-root.json",
out: "./test/fixtures/folder-endpoint-at-root.out.json",
},
"Folders get created without service config": {
in: "./test/fixtures/folder-at-service-not-defined.json",
out: "./test/fixtures/folder-at-service-not-defined.out.json",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
cfg, err := parseCfg(test.in)
if err != nil {
t.Error(err)
return
}
c, err := Parse(cfg)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
b, _ := json.MarshalIndent(c, "", "\t")
exp, _ := os.ReadFile(test.out)
if !bytes.Equal(b, exp) {
t.Errorf("unexpected output in %s:\n[GOT]\n%s\n\n[EXPECTED]\n%s", name, string(b), string(exp))
}
})
}
}
func parseCfg(path string) (config.ServiceConfig, error) {
cfg, _ := config.NewParser().Parse(path)
return cfg, nil
}