/
register_handler_test.go
82 lines (73 loc) · 1.86 KB
/
register_handler_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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestWebhookRegisterHandler(t *testing.T) {
tt := []struct {
name string
method string
statusCode int
postData *webhookRegisterRequest
want string
compareBody bool
}{
{
name: "invalid request: wrong http method",
method: http.MethodGet,
statusCode: http.StatusMethodNotAllowed,
postData: &webhookRegisterRequest{},
want: "method not allowed",
compareBody: true,
},
{
name: "invalid request: validation failed",
method: http.MethodPost,
statusCode: http.StatusBadRequest,
postData: &webhookRegisterRequest{},
want: "",
compareBody: false,
},
{
name: "valid request",
method: http.MethodPost,
statusCode: http.StatusOK,
postData: &webhookRegisterRequest{
Url: "http://example.com",
Token: "token",
},
want: "",
compareBody: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(tc.postData)
request := httptest.NewRequest(tc.method, "/api/webhooks", payloadBuf)
responseRecorder := httptest.NewRecorder()
WebhookRegisterHandler{
webhooks: make(webhookLocalStorage),
}.ServeHTTP(responseRecorder, request)
if responseRecorder.Code != tc.statusCode {
t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
}
if tc.compareBody {
if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
}
}
})
}
}
func TestGenerateNanoId(t *testing.T) {
got := len(generateNanoId())
want := defaultIdLen
if got != want {
t.Errorf("wrong ID length; got: %d; want: %d", got, want)
}
}