-
Notifications
You must be signed in to change notification settings - Fork 4
/
app_test.go
139 lines (113 loc) · 4.22 KB
/
app_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
package goapi_test
import (
"io"
"net/http"
"testing"
"time"
"github.com/hvuhsg/goapi"
"github.com/hvuhsg/goapi/request"
"github.com/hvuhsg/goapi/responses"
"github.com/hvuhsg/goapi/validators"
)
func TestCreateView(t *testing.T) {
app := goapi.GoAPI("title", "1.0")
t.Run("No methods", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expecting panic on missing methods")
}
}()
app.Path("/a").Description("test view").Parameter("t", goapi.QUERY).Action(
func(request *request.Request) responses.Response { return responses.NewHTMLResponse("1", 200) },
)
})
t.Run("No description", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expecting panic on missing description")
}
}()
app.Path("/b").Methods(goapi.GET).Parameter("t", goapi.QUERY).Action(
func(request *request.Request) responses.Response { return responses.NewHTMLResponse("1", 200) },
)
})
t.Run("same path", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expecting panic on same path for two views")
}
}()
app.Path("/c").Methods(goapi.GET).Description("c").Parameter("t", goapi.QUERY).Action(
func(request *request.Request) responses.Response { return responses.NewHTMLResponse("1", 200) },
)
app.Path("/c").Methods(goapi.GET).Description("c").Parameter("t", goapi.QUERY).Action(
func(request *request.Request) responses.Response { return responses.NewHTMLResponse("1", 200) },
)
})
app.Path("/").Methods(goapi.GET).Description("test view").Parameter("t", goapi.QUERY).Action(
func(request *request.Request) responses.Response {
return responses.NewHTMLResponse("1", 200)
},
)
}
func TestRunApp(t *testing.T) {
app := goapi.GoAPI("test", "1.0")
ping := app.Path("/ping")
ping.Methods(goapi.GET)
ping.Description("ping pong")
ping.Parameter("age", goapi.QUERY, validators.VRequired{}, validators.VIsInt{}, validators.VRange{Min: 5, Max: 25})
ping.Action(func(request *request.Request) responses.Response {
return responses.NewJSONResponse(responses.Json{"age": request.GetInt("age")}, 200)
})
go app.Run("127.0.0.1", 8080)
time.Sleep(time.Millisecond * 200)
resp, err := http.Get("http://127.0.0.1:8080/ping?age=20")
if err != nil {
t.Error("not expecting error")
}
if resp.StatusCode != 200 {
t.Errorf("expecting status-code 200 got %d", resp.StatusCode)
}
respBody, _ := io.ReadAll(resp.Body)
respString := string(respBody[:])
if respString != `{"age":20}` {
t.Errorf("expecting response body to be '{\"age\":20}' got '%s'", respBody)
}
}
func TestOpenAPISchema(t *testing.T) {
app := goapi.GoAPI("example", "1.0v")
app.Description("Example app")
app.TermOfServiceURL("www.example.com/term_of_service")
app.License("MIT", "")
app.Contact("yoyo", "example.com", "[email protected]")
app.Tag("math", "math operations")
app.Tag("deprecated", "deprecated operations")
add := app.Path("/add")
add.Tags("math")
add.Methods(goapi.GET)
add.Description("Add two numbers")
add.Parameter("a", goapi.QUERY, validators.VRequired{}, validators.VIsInt{}, validators.VRange{Min: 0, Max: 100})
add.Parameter("b", goapi.QUERY, validators.VRequired{}, validators.VIsInt{}, validators.VRange{Min: 0, Max: 100})
add.Action(func(request *request.Request) responses.Response {
return responses.NewJSONResponse(responses.Json{"sum": request.GetInt("a") + request.GetInt("b")}, 200)
})
sub := app.Path("/sub")
sub.Deprecated() // deprecated route
sub.Tags("math", "deprecated")
sub.Methods(goapi.GET)
add.Description("Subtruct two numbers (a - b)")
add.Parameter("a", goapi.QUERY, validators.VRequired{}, validators.VIsInt{}, validators.VRange{Min: 0, Max: 100})
add.Parameter("b", goapi.QUERY, validators.VRequired{}, validators.VIsInt{}, validators.VRange{Min: 0, Max: 100})
add.Action(func(request *request.Request) responses.Response {
return responses.NewJSONResponse(responses.Json{"result": request.GetInt("a") - request.GetInt("b")}, 200)
})
go app.Run("127.0.0.1", 8081)
time.Sleep(time.Millisecond * 200)
resp, err := http.Get("http://127.0.0.1:8081/openapi.json")
if err != nil {
t.Error("not expecting error")
}
if resp.StatusCode != 200 {
t.Errorf("expecting status-code 200 got %d", resp.StatusCode)
}
}