-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
executable file
·135 lines (120 loc) · 3.83 KB
/
main_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
package main
import (
"encoding/json"
"testing"
)
type Scenario struct {
Name string
Param string
Want bool
ErrMessage string
}
func TestInStringSlice(t *testing.T) {
type Scenario struct {
Name string
Param string
Want bool
}
tests := []Scenario{
{Name: "Success Case", Param: "c", Want: true},
{Name: "Error Case", Param: "d", Want: false},
}
s := []string{"a", "b", "c"}
for _, tc := range tests {
r := inStringSlice(tc.Param, s)
if r != tc.Want {
t.Fatalf(`%v: inStringSlice(c, %v) result=%v, want=%v`, tc.Name, s, r, tc.Want)
}
}
}
func TestGetFilePath(t *testing.T) {
type Scenario struct {
Name string
Want string
BaseDir string
FilePrefix string
Separator string
ServiceName string
}
tests := []Scenario{
{Name: "test1", Want: "/base/dir/krakend-service.json", BaseDir: "/base/dir/", FilePrefix: "krakend", Separator: "-", ServiceName: "service"},
{Name: "test2", Want: "/base/dir/service.json", BaseDir: "/base/dir/", FilePrefix: "", Separator: "", ServiceName: "service"},
}
for _, tc := range tests {
r := getFilePath(tc.BaseDir, tc.FilePrefix, tc.Separator, tc.ServiceName)
if r != tc.Want {
t.Fatalf(`%v: getFilePath: result=%v, want=%v`, tc.Name, r, tc.Want)
}
}
}
func TestTransformSwaggerResponse(t *testing.T) {
configPayload := []byte(`{
"name": "test-service",
"internal_address": "http://localhost:8080",
"output_encoding": "no-op",
"backend_encoding": "no-op",
}`)
serviceConfig := Service{}
json.Unmarshal(configPayload, &serviceConfig)
type Want struct {
Urls []string
}
type Scenario struct {
Name string
Want Want
SwaggerPayload []byte
}
tests := []Scenario{
{
Name: "test1",
Want: Want{Urls: []string{"/user/{username}", "/user/login", "/user/logout"}},
SwaggerPayload: []byte(`{ "paths": { "/user/{username}": { "get": { "parameters": [ { "name": "username", "in": "path" } ] }, "put": { "parameters": [ { "name": "username", "in": "path" }, { "in": "body", "name": "body" } ] }, "delete": { "parameters": [ { "name": "username", "in": "path" } ] } }, "/user/login": { "get": { "parameters": [ { "name": "username", "in": "query" }, { "name": "password", "in": "query" } ] } }, "/user/logout": { "get": { "tags": [ "user" ], "parameters": [] } } } }`),
},
}
for _, tc := range tests {
swgResponse := SwaggerResponse{}
json.Unmarshal(tc.SwaggerPayload, &swgResponse)
paths := transformSwaggerResponse(serviceConfig, swgResponse)
for _, p := range paths {
if !inStringSlice(p.Endpoint, tc.Want.Urls) {
t.Fatalf(`%v: transformSwaggerResponse: result=%v, want=%v`, tc.Name, p.Endpoint, tc.Want.Urls)
}
}
}
}
func TestCrossMapping(t *testing.T) {
payloadOne := []byte(` {
"name": "test-service",
"output_encoding": "no-op",
"backend_encoding": "no-op",
"cross_mapping": [
{"from": "/<service_name>/swagger", "to": "/swagger", "method": "GET", "internal_address": "http://localhost:8080", "headers_to_pass": []}
]
}`)
payloadTwo := []byte(` {
"name": "test-service",
"output_encoding": "no-op",
"backend_encoding": "no-op",
"cross_mapping": [
{"from": "/swagger", "to": "/swagger", "method": "GET", "internal_address": "http://localhost:8080", "headers_to_pass": []}
]
}`)
type Scenario struct {
Name string
Payload []byte
Want string
}
tests := []Scenario{
{Name: "test1", Payload: payloadOne, Want: "/test-service/swagger"},
{Name: "test2", Payload: payloadTwo, Want: "/swagger"},
}
for _, tc := range tests {
serviceConfig := Service{}
json.Unmarshal(tc.Payload, &serviceConfig)
paths := crossMapping(serviceConfig)
r := paths[0].Endpoint
if r != tc.Want {
t.Fatalf(`%v: crossMapping: result=%v, want=%v`, tc.Name, r, tc.Want)
}
}
}