-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
152 lines (146 loc) · 3.67 KB
/
generator_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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"testing"
"github.com/dave/jennifer/jen"
"github.com/stretchr/testify/assert"
)
func TestGenerateMain(t *testing.T) {
initLog(nil)
flows := map[string]Flow{}
flows["1"] = Flow{
Request: http.Request{
Method: "GET",
Host: "localhost:8080",
URL: &url.URL{Path: "/"},
Body: io.NopCloser(bytes.NewReader([]byte(`{"token": "abc"}`))),
},
Response: http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(bytes.NewReader([]byte(`{"foo": "bar"}`))),
},
}
flows["2"] = Flow{
Request: http.Request{
Method: "GET",
Host: "localhost:8081",
URL: &url.URL{Path: "/hoge"},
},
Response: http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": []string{"application/json"},
"X-Foo": []string{"foo"},
},
Body: io.NopCloser(bytes.NewReader([]byte(`{"foo": "bar"}`))),
},
}
o, err := createExternalAPITree(flows)
assert.NoError(t, err)
stmt := jen.Statement(generateServerFuncs(o, true, true))
assert.Equal(t,
`func main() {
func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
body, _ := stringify(r.Body)
if r.Method == "GET" {
if q, _ := stringifyUrlValues(r.URL.Query()); q == "{}" {
if body == "{\"token\":\"abc\"}" {
rw.WriteHeader(200)
fmt.Fprint(rw, "{\"foo\":\"bar\"}")
return
}
}
}
})
port := 8080
server := http.Server{
Addr: "0.0.0.0:" + fmt.Sprint(port),
Handler: enableLogRequest(mux, port),
}
fmt.Printf("Listening on %v\n", server.Addr)
go server.ListenAndServe()
}()
func() {
mux := http.NewServeMux()
mux.HandleFunc("/hoge", func(rw http.ResponseWriter, r *http.Request) {
body, _ := stringify(r.Body)
if r.Method == "GET" {
if q, _ := stringifyUrlValues(r.URL.Query()); q == "{}" {
if body == "" {
rw.Header().Set("X-Foo", "foo")
rw.WriteHeader(200)
fmt.Fprint(rw, "{\"foo\":\"bar\"}")
return
}
}
}
})
port := 8081
server := http.Server{
Addr: "0.0.0.0:" + fmt.Sprint(port),
Handler: enableLogRequest(mux, port),
}
fmt.Printf("Listening on %v\n", server.Addr)
go server.ListenAndServe()
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-sig
}`, fmt.Sprintf("%#v", &stmt))
}
func TestStringify(t *testing.T) {
generated := jen.Statement(generateStringify())
assert.Equal(t,
`func stringify(r io.ReadCloser) (string, error) {
if r == nil {
return "", nil
}
body, err := io.ReadAll(r)
if err != nil {
return "", err
}
defer r.Close()
bm := make(map[string]interface{})
if err := json.Unmarshal(body, &bm); err != nil {
return string(body), err
}
if j, err := json.Marshal(bm); err != nil {
return string(body), err
} else {
return string(j), nil
}
}
`, fmt.Sprintf("%#v", &generated))
}
func TestGenerateStringifyUrlValues(t *testing.T) {
generated := jen.Statement(generateStringifyUrlValues())
assert.Equal(t,
`func stringifyUrlValues(m url.Values) (string, error) {
query, err := json.Marshal(m)
if err != nil {
return "", err
}
return string(query), nil
}
`, fmt.Sprintf("%#v", &generated))
}
func TestGenerateEnableLogRequest(t *testing.T) {
generated := jen.Statement(generateEnableLogRequest())
assert.Equal(t,
`func enableLogRequest(handler http.Handler, port int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
log.Printf("%s %s %v %s \n", r.RemoteAddr, r.Method, port, r.URL)
})
}
`, fmt.Sprintf("%#v", &generated))
}