-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
111 lines (81 loc) · 2.12 KB
/
handlers_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/ckblck/gocryotic/network"
"github.com/gofiber/fiber/v2"
)
type form struct {
key string
value string
}
func TestBasicHandler(t *testing.T) {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("👋")
})
resp := newResponse(app, "GET", "/")
got := resp.StatusCode
want := 200
if got != want {
t.Errorf("got %v want %v", got, want)
}
gotResponse, _ := ioutil.ReadAll(resp.Body)
wantResponse := "👋"
if string(gotResponse) != wantResponse {
t.Errorf("got %v want %v", gotResponse, want)
}
}
func TestAddPlayerRequest(t *testing.T) {
app := fiber.New()
app.Post("/api/v1/player", network.AddPlayer)
form := url.Values{}
form.Add("?", "?")
request := makeRequestWForm("POST", "/api/v1/player", form)
resp := sendRequest(app, request)
got := resp.StatusCode
notWant := 201
if got == notWant {
t.Errorf("got %v and not want %v", got, notWant)
}
}
func TestGetReplayByIDRequest(t *testing.T) {
app := fiber.New()
app.Get("/api/v1/replay/:id", network.GetReplay)
resp := newResponse(app, "GET", "/api/v1/replay/????")
got := resp.StatusCode
want := 404
if got != want {
t.Errorf("got %v want %v", got, want)
}
}
func TestPostReplayRequest(t *testing.T) {
app := fiber.New()
app.Post("/api/v1/replay", network.AddReplay)
form := url.Values{}
form.Add("file", "?")
request := makeRequestWForm("POST", "/api/v1/replay", form)
resp := sendRequest(app, request)
got := resp.StatusCode
want := 422
if got != want {
t.Errorf("got %v want %v", got, want)
}
}
func makeRequestWForm(method, route string, form url.Values) (req *http.Request) {
req = httptest.NewRequest(method, route, strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return
}
func sendRequest(app *fiber.App, req *http.Request) *http.Response {
resp, _ := app.Test(req)
return resp
}
func newResponse(app *fiber.App, method, route string) *http.Response {
req := httptest.NewRequest(method, route, nil)
return sendRequest(app, req)
}