This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dolphin_test.go
121 lines (99 loc) · 2.55 KB
/
dolphin_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
package dolphin
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func TestGetRequestQuery(t *testing.T) {
app := Default()
app.Use(func(c *Context) {
if c.Method() != http.MethodGet {
t.Errorf("Expect GET request, actual %s", c.Method())
c.String("Method Should be GET", http.StatusBadRequest)
return
}
name := c.Query("name")
c.String(fmt.Sprintf("Hello %s", name), http.StatusOK)
})
go func() {
app.Run()
}()
cli := http.Client{}
resp, err := cli.Get("http://localhost:8080?name=dolphin")
defer app.Shutdown()
if err != nil {
t.Errorf("GET request expect no error, actual got %v", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("GET request expect status code %d, actual got %d", http.StatusOK, resp.StatusCode)
return
}
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Read from response body expect no error, actual got %v", err)
return
}
if string(res) != "Hello dolphin" {
t.Errorf("Expect response body 'Hello dolphin', actual got %v", string(res))
}
}
func TestGetRequestPostJSON(t *testing.T) {
type PostJsonTestPayload struct {
Name *string `json:"name"`
Message *string `json:"message"`
}
app := New(&Config{
Port: 8081,
})
app.Use(func(c *Context) {
if c.Method() != http.MethodPost {
t.Errorf("Expect POST request, actual %s", c.Method())
c.String("Method Should be POST", http.StatusBadRequest)
return
}
var payload PostJsonTestPayload
err := c.PostJSON(&payload)
if err != nil {
t.Log(err)
c.String("Invalid parameter format", http.StatusBadRequest)
return
}
c.JSON(O{
"message": fmt.Sprintf("Hello %s", *payload.Name),
})
})
go func() {
app.Run()
}()
defer app.Shutdown()
cli := http.Client{}
body, err := json.Marshal(map[string]string{
"name": "dolphin",
})
if err != nil {
t.Errorf("json.Marshal expect ni error, actual %v", err)
return
}
resp, err := cli.Post("http://localhost:8081", "application/json", bytes.NewBuffer(body))
if err != nil {
t.Errorf("GET request expect no error, actual got %v", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("GET request expect status code %d, actual got %d", http.StatusOK, resp.StatusCode)
return
}
var data PostJsonTestPayload
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
t.Errorf("Decode response body expect no error, actual got %v", err)
return
}
if data.Message == nil || *data.Message != "Hello dolphin" {
t.Errorf("Expect response body 'Hello dolphin', actual got %v", *data.Message)
}
}