-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrappers_test.go
76 lines (65 loc) · 1.89 KB
/
wrappers_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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetOnly(t *testing.T) {
tt := []struct {
methodType string
expectedStatus string
expectedStatusCode int
}{
{http.MethodGet, "StatusOK", http.StatusOK},
{http.MethodPost, "StatusMethodNotAllowed", http.StatusMethodNotAllowed},
{http.MethodPut, "StatusMethodNotAllowed", http.StatusMethodNotAllowed},
}
for _, tc := range tt {
req, err := http.NewRequest(tc.methodType, "localhost:51234/", nil)
if err != nil {
t.Fatalf("could not created request: %v", err)
}
rec := httptest.NewRecorder()
h := GetOnly(IndexHandler)
h(rec, req)
res := rec.Result()
defer res.Body.Close()
if res.StatusCode != tc.expectedStatusCode {
t.Fatalf("expected %v; got %v", tc.expectedStatus, res.Status)
}
}
}
func TestPostOnly(t *testing.T) {
tt := []struct {
methodType string
keyword string
expectedStatus string
expectedStatusCode int
}{
{http.MethodPost, "아주대학교입구", "StatusOK", http.StatusOK},
{http.MethodGet, "아주대학교입구", "StatusMethodNotAllowed", http.StatusMethodNotAllowed},
{http.MethodPut, "아주대학교입구", "StatusMethodNotAllowed", http.StatusMethodNotAllowed},
}
for _, tc := range tt {
rawBody := SearchInput{tc.keyword}
jsonBody, err := json.Marshal(rawBody)
if err != nil {
t.Fatalf("could not parsed json data: %v", err)
}
reqBody := bytes.NewBufferString(string(jsonBody))
req, err := http.NewRequest(tc.methodType, "localhost:51234/user/search?type=station", reqBody)
if err != nil {
t.Fatalf("could not created request: %v", err)
}
rec := httptest.NewRecorder()
h := PostOnly(SearchHandler)
h(rec, req)
res := rec.Result()
defer res.Body.Close()
if res.StatusCode != tc.expectedStatusCode {
t.Fatalf("expected %v; got %v", tc.expectedStatus, res.Status)
}
}
}