-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_test.go
132 lines (105 loc) · 2.97 KB
/
basic_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
// Copyright 2012 Robert W. Johnstone. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpauth
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var (
// The following policy is used for all of the tests in this file
basicAuth *Basic
// Ensure that the Basic authentication policy meets the requirements
// for the Policy interface.
_ Policy = &Basic{}
)
func init() {
basicAuth = NewBasic("golang", func(username, password string) bool {
return username == password
}, nil)
}
func basicHandler(w http.ResponseWriter, r *http.Request) {
username := basicAuth.Authorize(r)
if username == "" {
basicAuth.NotifyAuthRequired(w, r)
return
}
fmt.Fprintf(w, "<html><body><h1>Hello</h1><p>Welcome, %s</p></body></html>", username)
}
func TestBasicNoAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(basicHandler))
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
t.Fatalf("Error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized {
t.Errorf("Received incorrect status: %d", resp.StatusCode)
}
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error: %s", err)
}
if string(buffer) != StatusUnauthorizedHtml {
println(string(buffer))
t.Errorf("Incorrect body text.")
}
}
func TestBasicBadAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(basicHandler))
defer ts.Close()
resp, err := http.Get("http://user:pass@" + ts.URL[7:])
if err != nil {
t.Fatalf("Error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized {
t.Errorf("Received incorrect status: %d", resp.StatusCode)
}
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error: %s", err)
}
if string(buffer) != StatusUnauthorizedHtml {
println(string(buffer))
t.Errorf("Incorrect body text.")
}
}
func TestBasicGoodAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(basicHandler))
defer ts.Close()
resp, err := http.Get("http://user:user@" + ts.URL[7:])
if err != nil {
t.Fatalf("Error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Received incorrect status: %d", resp.StatusCode)
}
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error: %s", err)
}
if string(buffer) != "<html><body><h1>Hello</h1><p>Welcome, user</p></body></html>" {
println(string(buffer))
t.Errorf("Incorrect body text.")
}
}
func TestBasicCredientials(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(basicHandler))
defer ts.Close()
resp, err := http.Get("http://user:pass@" + ts.URL[7:])
if err != nil {
t.Fatalf("Error: %s", err)
}
defer resp.Body.Close()
token := resp.Request.Header.Get("Authorization")
username, password := basicAuth.ParseToken(token)
if username != "user" || password != "pass" {
t.Errorf("auth.Credentials returned incorrect values.")
}
}