forked from nnev/kasse
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_test.go
119 lines (104 loc) · 3.81 KB
/
http_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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gorilla/sessions"
)
func createResponse(req *http.Request, res *httptest.ResponseRecorder) *http.Response {
return &http.Response{
Status: fmt.Sprintf("%d %s", res.Code, http.StatusText(res.Code)),
StatusCode: res.Code,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: res.HeaderMap,
Body: ioutil.NopCloser(res.Body),
ContentLength: int64(res.Body.Len()),
TransferEncoding: nil,
Close: false,
Trailer: nil,
Request: req,
TLS: nil,
}
}
func TestLogin(t *testing.T) {
k := Kasse{db: createDB(t), log: testLogger(t)}
k.sessions = sessions.NewCookieStore([]byte("TODO: Set up safer password"))
h := k.Handler()
jar, _ := cookiejar.New(nil)
insertData(t, k.db, []User{
{
ID: 1,
Name: "Merovius",
// "foobar"
Password: []byte("$2a$10$HvkgrSxCQxOSFB4vvPd0SuP5urdZUuXSMumMYA5qjli9Mh0pcVDXS"),
},
{
ID: 2,
Name: "koebi",
// ""
Password: []byte("$2a$10$Jt3qpo7xO9DKCbxYNZbFzuRySIB.KSkFnpRo8jv8UYFIng0pOoOlO"),
},
}, nil, nil)
tests := []struct {
// inputs
method string
url string
form url.Values
// expected outputs
code int
headers map[string]string
grep string
}{
{"GET", "http://localhost:9000/", nil, http.StatusFound, map[string]string{"Location": "/login.html"}, ""},
{"GET", "http://localhost:9000/login.html", nil, http.StatusOK, map[string]string{"Content-Type": "text/html"}, "<title>Login</title>"},
{"POST", "http://localhost:9000/login.html", url.Values{"username": []string{""}, "password": []string{"foobar"}}, http.StatusBadRequest, nil, "Neither username nor password can be empty"},
{"POST", "http://localhost:9000/login.html", url.Values{"username": []string{"koebi"}, "password": []string{""}}, http.StatusBadRequest, nil, "Neither username nor password can be empty"},
{"POST", "http://localhost:9000/login.html", url.Values{"username": []string{"koebi"}, "password": []string{"foobar"}}, http.StatusUnauthorized, nil, ""},
{"POST", "http://localhost:9000/login.html", url.Values{"username": []string{"Merovius"}, "password": []string{"foobaz"}}, http.StatusUnauthorized, nil, ""},
{"POST", "http://localhost:9000/login.html", url.Values{"username": []string{"Merovius"}, "password": []string{"foobar"}}, http.StatusFound, map[string]string{"Location": "/"}, ""},
{"GET", "http://localhost:9000/", nil, http.StatusOK, map[string]string{"Content-Type": "text/html"}, "<title>ccchd Kasse</title>"},
}
for _, tc := range tests {
var body io.Reader
if tc.form != nil {
body = strings.NewReader(tc.form.Encode())
}
req, err := http.NewRequest(tc.method, tc.url, body)
if err != nil {
t.Fatalf(`%s %s %v: %v`, tc.method, tc.url, tc.form, err)
}
if tc.form != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
}
for _, c := range jar.Cookies(req.URL) {
t.Logf("Adding cookie %v", c)
req.AddCookie(c)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if c := rec.Code; c != tc.code {
t.Fatalf(`%s %s %v has code %d, expected %d`, tc.method, tc.url, tc.form, c, tc.code)
}
for k, v := range tc.headers {
if gv := rec.HeaderMap.Get(k); gv != v {
t.Fatalf(`%s %s %v has header %q set to %q, expected %q`, tc.method, tc.url, tc.form, k, gv, v)
}
}
if !strings.Contains(rec.Body.String(), tc.grep) {
t.Fatalf("%s %s %v: Response does not contain %q\nFull Body:\n%s", tc.method, tc.url, tc.form, tc.grep, rec.Body.String())
}
res := createResponse(req, rec)
if c := res.Cookies(); len(c) > 0 {
t.Logf("Setting cookies %v", res.Cookies())
jar.SetCookies(req.URL, res.Cookies())
}
}
}