-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicauth_test.go
55 lines (46 loc) · 1.39 KB
/
basicauth_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
package rest
import (
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func tempHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
}
func TestBasicAuth(t *testing.T) {
credStore := NewMemoryCredentialStore("john", "doe")
server := httptest.NewServer(BasicAuthWrapper("foo", credStore, tempHandler))
defer server.Close()
c := http.Client{}
req, err := http.NewRequest("GET", server.URL, nil)
if err != nil {
t.Fatal("Error creating request: ", err)
}
// req.SetBasicAuth("", "")
resp, _ := c.Do(req)
if resp.StatusCode != http.StatusUnauthorized {
t.Fatal("Got ", resp.StatusCode, " with not username or password")
}
req.Header.Set("Authorization", "xxx&! !!!!")
resp, _ = c.Do(req)
if resp.StatusCode != http.StatusUnauthorized {
t.Fatal("Got ", resp.StatusCode, " with garbled chars")
}
req.Header.Set("Authorization", fmt.Sprintf("something %s", base64.StdEncoding.EncodeToString([]byte("justusername"))))
resp, _ = c.Do(req)
if resp.StatusCode != http.StatusUnauthorized {
t.Fatal("Got ", resp.StatusCode, " with garbled chars")
}
req.SetBasicAuth("john", "incorrect")
resp, _ = c.Do(req)
if resp.StatusCode != http.StatusUnauthorized {
t.Fatal("Got ", resp.StatusCode, " with incorrect password")
}
req.SetBasicAuth("john", "doe")
resp, _ = c.Do(req)
if resp.StatusCode != http.StatusOK {
t.Fatal("Did not expect error")
}
}