-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest_test.go
136 lines (109 loc) · 3.16 KB
/
digest_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
// 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 (
// Verify that the policy provided by Basic meets the requirements
// of the interface Policy
_ Policy = &Digest{}
// The following policy is used for all of the tests in this file
digestAuth *Digest
success chan bool
)
func init() {
var err error
digestAuth, err = NewDigest("golang", func(username string) string {
return username
}, nil)
if err != nil {
panic(err)
}
success = make(chan bool)
}
func digestHandler(w http.ResponseWriter, r *http.Request) {
username := digestAuth.Authorize(r)
if username == "" {
digestAuth.NotifyAuthRequired(w, r)
return
}
// Ignore spurious requests
if r.URL.String()[0:7] != "/digest" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Println("digest", r.URL)
fmt.Fprintf(w, "<html><body><h1>Hello</h1><p>Welcome, %s</p></body></html>", username)
success <- true
}
func TestDigestNoAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(digestHandler))
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 TestDigestBadAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(digestHandler))
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 TestDigestBrowser(t *testing.T) {
if testing.Short() {
t.Skip("skipping test of digest authorization.")
}
ts := httptest.NewServer(http.HandlerFunc(digestHandler))
defer ts.Close()
url := "http://user:user@" + ts.URL[7:] + "/digest/"
fmt.Println("Use a webbrowser, and navigate to", url, "to check digest authentication.")
fmt.Println("For authentication to succeed, the username and password must match.")
<-success
}
func TestDigestBrowser2(t *testing.T) {
if testing.Short() {
t.Skip("skipping test of digest authorization.")
}
ts := httptest.NewServer(http.HandlerFunc(digestHandler))
defer ts.Close()
url := "http://user:user@" + ts.URL[7:] + "/digest/"
fmt.Println("Use a webbrowser, and navigate to", url, "to check digest authentication.")
fmt.Println("For authentication to succeed, the username and password must match.")
<-success
url = url + "sub/"
fmt.Println("Now try", url, "to check digest authentication.")
<-success
}