forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
149 lines (127 loc) · 3.8 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Tests for the sample program to show how to use JWTs
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
func TestLoginHandler(t *testing.T) {
tests := []struct {
Body string
StatusCode int
}{
{`{"email": "jacob@example.com", "password": "rory"}`, http.StatusOK},
{`{"email": "anna@example.com", "password": "rory"}`, http.StatusUnauthorized},
{`{"email": "jacob@example.com", "password": "BAD"}`, http.StatusUnauthorized},
{`{"email": "", "password": ""}`, http.StatusUnauthorized},
{``, http.StatusBadRequest},
}
// Start a server to handle these requests.
ts := httptest.NewServer(App())
defer ts.Close()
for _, tt := range tests {
// Send the request body to /login
body := strings.NewReader(tt.Body)
res, err := http.Post(ts.URL+"/login", "application/json", body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != tt.StatusCode {
t.Log("Wanted:", tt.StatusCode)
t.Log("Got :", res.StatusCode)
t.Fatal("Mismatch status code")
}
// If we didn't expect success then move on to the next test
if tt.StatusCode != http.StatusOK {
continue
}
// Read in the response from the api call.
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// Validate we received a token in the response. We're just checking
// that it looks kind of like a token here.
re := regexp.MustCompile(`^([-\w=+/]+\.){2}[-\w=+/]+$`)
got := strings.TrimSpace(string(b))
if !re.MatchString(got) {
t.Log("Wanted a token")
t.Log("Got:", got)
t.Fatal("Mismatch")
}
}
}
// expired is a valid but old token for testing
const expired = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODg0OTM0ODMsImlhdCI6MTQ4ODQ5MzQ4MiwibmJmIjoxNDg4NDkzNDgyLCJzdWIiOiJqYWNvYkBleGFtcGxlLmNvbSJ9.e0cWExi4hHxODo44x5P1wHHPKlZukFk93ib3f2UOwEY`
func TestSecureHandler(t *testing.T) {
// Start a server to handle these requests.
ts := httptest.NewServer(App())
defer ts.Close()
// Before we test the secure handler let's get a valid token
// Send a request to /login to get a token
body := strings.NewReader(`{"email": "jacob@example.com", "password": "rory"}`)
res, err := http.Post(ts.URL+"/login", "application/json", body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusOK {
t.Log("Wanted:", http.StatusOK)
t.Log("Got :", res.StatusCode)
t.Fatal("Could not log in")
}
// Read in the token
tkn, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
tests := []struct {
Token string
Want string
StatusCode int
}{
{string(tkn), "Be sure to drink your Ovaltine!", http.StatusOK},
{"bad token", "Not authorized", http.StatusUnauthorized},
{expired, "Not authorized", http.StatusUnauthorized},
{"", "Not authorized", http.StatusUnauthorized},
}
for _, tt := range tests {
// Create a new request for the GET call.
req, err := http.NewRequest("GET", ts.URL+"/secure", nil)
if err != nil {
t.Fatal(err)
}
// If we have a token then add it to the Authentication header
if tt.Token != "" {
req.Header.Set("Authentication", "Bearer "+tt.Token)
}
// Create a Client and perform the GET call.
var c http.Client
res, err := c.Do(req)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != tt.StatusCode {
t.Log("Wanted:", tt.StatusCode)
t.Log("Got :", res.StatusCode)
t.Fatal("Mismatch")
}
// Read in the response from the api call.
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// Validate we received the expected response.
got := strings.TrimSpace(string(b))
want := tt.Want
if got != want {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}
}