forked from u5surf/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
75 lines (62 loc) · 1.42 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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
)
func TestHTTP__extractCookie(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
if req == nil {
t.Error("nil req")
}
req.AddCookie(&http.Cookie{
Name: "moov_auth",
Value: "data",
})
cookie := extractCookie(req)
if cookie == nil {
t.Error("nil cookie")
}
}
func TestHTTP_createCookie(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
userId := generateID()
cookie, err := createCookie(userId, auth)
if err != nil {
t.Fatal(err)
}
if id, _ := auth.findUserId(cookie.Value); id != userId {
t.Errorf("got %q, expected %q", id, userId)
}
}
func TestHTTP_internalError(t *testing.T) {
w := httptest.NewRecorder()
internalError(w, errors.New("problem Y"))
w.Flush()
if w.Code != http.StatusInternalServerError {
t.Errorf("got %d", w.Code)
}
}
func TestHttp__addPingRoute(t *testing.T) {
r := httptest.NewRequest("GET", "/ping", nil)
w := httptest.NewRecorder()
router := mux.NewRouter()
addPingRoute(router)
router.ServeHTTP(w, r)
w.Flush()
if w.Code != http.StatusOK {
t.Errorf("got %d", w.Code)
}
if v := w.Body.String(); v != "PONG" {
t.Errorf("got %q", v)
}
}