-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_test.go
134 lines (118 loc) · 3.05 KB
/
main_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
130
131
132
133
134
// Copyright (c) 2020 Jorge Luis Betancourt. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestAPIHandler(t *testing.T) {
testCases := []struct {
name string
method string
payload url.Values
response string
status int
}{
{
name: "Valid tokenizer and str",
method: "POST",
payload: url.Values{
"tokenizer": {"%{key1} %{key2}"},
"str": {"a b"},
},
response: "[{\"key1\":\"a\",\"key2\":\"b\"}]",
status: http.StatusOK,
},
{
name: "Missing str parameter",
method: "POST",
payload: url.Values{
"tokenizer": {"%{key1} %{key2}"},
},
response: "str parameter not found\n",
status: http.StatusBadRequest,
},
{
name: "Missing tokenizer parameter",
method: "POST",
payload: url.Values{
"str": {"a b"},
},
response: "tokenizer parameter not found\n",
status: http.StatusBadRequest,
},
{
name: "Empty payload",
method: "GET",
payload: nil,
response: "str parameter not found\n",
status: http.StatusBadRequest,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest(tc.method, "/api", strings.NewReader(
tc.payload.Encode()),
)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(apiHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != tc.status {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tc.status)
}
if diff := cmp.Diff(tc.response, rr.Body.String()); diff != "" {
t.Errorf("handler returned wrong body (-want +got):\n%s", diff)
}
})
}
}
func TestIndexRoute(t *testing.T) {
mux := http.NewServeMux()
RegisterAppHandlers(mux)
// The response recorder used to record HTTP responses
rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal("Creating 'GET /' request failed,")
}
mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("'GET /' request should've a handler registered, got: %d, want: %d",
rr.Code, http.StatusOK,
)
}
}
func TestAPIRoute(t *testing.T) {
mux := http.NewServeMux()
RegisterAppHandlers(mux)
// The response recorder used to record HTTP responses
rr := httptest.NewRecorder()
payload := url.Values{
"tokenizer": {"%{key1} %{key2}"},
"str": {"a b"},
}
req, err := http.NewRequest("POST", apiPath, strings.NewReader(
payload.Encode()))
if err != nil {
t.Fatalf("Creating 'POST /api' request failed.")
}
req.Header.Add("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8")
mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("'POST /api' request should've a handler registered, got: %d, want: %d",
rr.Code, http.StatusOK,
)
}
}