-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.go
129 lines (91 loc) · 1.73 KB
/
core_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
package alf
import (
"bytes"
"io"
"net/http"
"testing"
)
const (
// Host name of the HTTP Server
Host = "localhost"
// Port of the HTTP Server
Port = "8080"
)
const (
BodyResponse = "Hello World"
)
func TestRawGet(t *testing.T) {
generateApp(t)
body := getRequest(t, "/raw")
t.Log(string(body))
if string(body) != BodyResponse {
t.FailNow()
}
}
func TestRawPost(t *testing.T) {
generateApp(t)
body := postRequest(t, "/api/postRaw", []byte(BodyResponse))
t.Log(string(body))
if string(body) != BodyResponse {
t.FailNow()
}
}
func generateApp(t *testing.T) {
err := App(&AppConfig{
Routes: CreateRouter([]Route{
{
Path: "/raw",
Method: "get",
Handle: func(ctx *Ctx) error {
ctx.WriteString(BodyResponse)
return nil
},
},
{
Path: "/api",
Method: "get",
Handle: func(ctx *Ctx) error {
ctx.WriteString("Working 💪")
return nil
},
Children: []Route{
{
Path: "/postRaw",
Method: "post",
Handle: func(ctx *Ctx) error {
ctx.Write(ctx.Request.Body())
return nil
},
},
},
},
}),
})
if err != nil {
t.Error(err)
}
}
func getRequest(t *testing.T, path string) []byte {
resp, err := http.Get("http://" + Host + ":" + Port + path)
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
return body
}
func postRequest(t *testing.T, path string, body []byte) []byte {
resp, err := http.Post("http://"+Host+":"+Port+path, "text/plain", bytes.NewReader(body))
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
return respBody
}