-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_basic_test.go
More file actions
112 lines (88 loc) · 2.7 KB
/
Copy pathwrap_basic_test.go
File metadata and controls
112 lines (88 loc) · 2.7 KB
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
// Copyright 2013 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"
)
func wrappedHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<html><body><h1>Hello</h1><p>Welcome, authenticated user!</p></body></html>")
}
func TestWrapBasicNoAuth(t *testing.T) {
ts := httptest.NewServer(NewHandlerWithAuth(basicAuth, http.HandlerFunc(wrappedHandler)))
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 TestWrapBasicBadAuth(t *testing.T) {
ts := httptest.NewServer(NewHandlerWithAuth(basicAuth, http.HandlerFunc(wrappedHandler)))
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 TestWrapBasicGoodAuth(t *testing.T) {
ts := httptest.NewServer(NewHandlerWithAuth(basicAuth, http.HandlerFunc(wrappedHandler)))
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, authenticated user!</p></body></html>" {
println(string(buffer))
t.Errorf("Incorrect body text.")
}
}
func TestWrapBasicCredientials(t *testing.T) {
ts := httptest.NewServer(NewHandlerWithAuth(basicAuth, http.HandlerFunc(wrappedHandler)))
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.")
}
}