-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers_test.go
117 lines (96 loc) · 2.7 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
112
113
114
115
116
117
package main
import (
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/labstack/echo/v4"
)
type FakeSecretMsgStorer struct {
msg string
token string
err error
lastUsedToken string
lastMsg string
}
func (f *FakeSecretMsgStorer) Get(token string) (msg string, err error) {
f.lastUsedToken = token
return f.msg, f.err
}
func (f *FakeSecretMsgStorer) Store(msg string) (token string, err error) {
f.lastMsg = msg
return f.token, f.err
}
func TestGetMsgHandlerSuccess(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/?token=secrettoken", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
s := &FakeSecretMsgStorer{msg: "secret"}
h := NewSecretHandlers(s)
err := h.GetMsgHandler(c)
if err != nil {
t.Fatalf("got error %v, none expected", err)
}
if s.lastUsedToken != "secrettoken" {
t.Fatalf("Storer::Get was called with %s, expected %s", s.lastUsedToken, "secrettoken")
}
if rec.Code != http.StatusOK {
t.Fatalf("got statusCode %d, expected %d", rec.Code, http.StatusOK)
}
expected := "{\"msg\":\"secret\"}\n"
actual := rec.Body.String()
if expected != actual {
t.Fatalf("got body %s, expected %s", expected, actual)
}
}
func TestGetMsgHandlerError(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/?token=secrettoken", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
s := &FakeSecretMsgStorer{msg: "secret", err: errors.New("expired")}
h := NewSecretHandlers(s)
err := h.GetMsgHandler(c)
if err == nil {
t.Fatalf("got no error, expected one")
}
v, ok := err.(*echo.HTTPError)
if !ok {
t.Fatalf("expected an HTTPError, got %s", reflect.TypeOf(v))
}
if v.Code != http.StatusInternalServerError {
t.Fatalf("got statusCode %d, expected %d", v.Code, http.StatusInternalServerError)
}
}
func TestHealthHandler(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := HealthHandler(c)
if err != nil {
t.Fatalf("error returned %v, expected nil", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("got statusCode %d, expected %d", rec.Code, http.StatusOK)
}
}
func TestRedirectHandler(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := redirect(c)
if err != nil {
t.Fatalf("error returned %v, expected nil", err)
}
if rec.Code != http.StatusPermanentRedirect {
t.Fatalf("got statusCode %d, expected %d", rec.Code, http.StatusOK)
}
l := rec.HeaderMap.Get("Location")
if l != "/" {
t.Fatalf("redirect Location is %s, expected %s", l, "/")
}
}