-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy paththink_test.go
115 lines (93 loc) · 2.83 KB
/
think_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
package thinkgo
import (
"crypto/tls"
"fmt"
"github.com/forgoer/thinkgo/context"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/forgoer/thinkgo/think"
)
func testRequest(t *testing.T, method, url string, data url.Values, res *think.Res) {
var err error
var resp *http.Response
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
var body io.Reader
if strings.ToUpper(method) == "GET" {
} else {
}
contentType := "application/x-www-form-urlencoded"
method = strings.ToUpper(method)
switch method {
case "GET":
url = strings.TrimRight(url, "?") + "?" + data.Encode()
case "POST", "PUT", "DELETE":
if data != nil {
body = strings.NewReader(data.Encode())
}
}
req, err := http.NewRequest(method, url, body)
assert.NoError(t, err)
req.Header.Set("Content-Type", contentType)
resp, err = client.Do(req)
assert.NoError(t, err)
defer resp.Body.Close()
content, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, res.GetCode(), resp.StatusCode)
assert.Equal(t, res.GetContent(), string(content))
}
func TestRunWithPort(t *testing.T) {
th := New()
go func() {
th.RegisterRoute(func(route *think.Route) {
route.Get("/", func(req *think.Req) *think.Res {
return think.Text("it worked")
})
})
// listen and serve on 0.0.0.0:9011
th.Run(":9012")
}()
time.Sleep(5 * time.Millisecond)
testRequest(t, "get", "http://localhost:9012/", nil, context.NewResponse().SetContent("it worked"))
}
func TestThink_Run(t *testing.T) {
th := New()
go func() {
th.RegisterRoute(func(route *think.Route) {
route.Get("/", func(req *think.Req) interface{} {
return "it worked"
})
route.Get("/user/{name}", func(req *think.Req, name string) interface{} {
return fmt.Sprintf("Hello %s !", name)
})
route.Post("/user", func(req *think.Req) interface{} {
name, err := req.Post("name")
if err != nil {
panic(err)
}
return fmt.Sprintf("Create %s", name)
})
route.Delete("/user/{name}", func(name string) interface{} {
return fmt.Sprintf("Delete %s", name)
})
})
// listen and serve on 0.0.0.0:9011
th.Run()
}()
time.Sleep(5 * time.Millisecond)
testRequest(t, "get", "http://localhost:9011/", nil, context.NewResponse().SetContent("it worked"))
testRequest(t, "get", "http://localhost:9011/user/thinkgo", nil, context.NewResponse().SetContent(fmt.Sprintf("Hello %s !", "thinkgo")))
testRequest(t, "post", "http://localhost:9011/user", url.Values{"name": {"thinkgo"}}, context.NewResponse().SetContent(fmt.Sprintf("Create %s", "thinkgo")))
testRequest(t, "delete", "http://localhost:9011/user/thinkgo", url.Values{"name": {"thinkgo"}}, context.NewResponse().SetContent(fmt.Sprintf("Delete %s", "thinkgo")))
}