-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions_auth_test.go
More file actions
55 lines (50 loc) · 1.33 KB
/
options_auth_test.go
File metadata and controls
55 lines (50 loc) · 1.33 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
package httpx
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
)
func TestAuthHeaders(t *testing.T) {
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotAuth = r.Header.Get("Authorization")
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
c := New()
_, err := Get[string](c, srv.URL, Auth("Token", "abc123"))
if err != nil {
t.Fatalf("auth request failed: %v", err)
}
if gotAuth != "Token abc123" {
t.Fatalf("auth header = %q", gotAuth)
}
}
func TestBearerAndBasic(t *testing.T) {
var auths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auths = append(auths, r.Header.Get("Authorization"))
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
c := New()
_, err := Get[string](c, srv.URL, Bearer("token"))
if err != nil {
t.Fatalf("bearer request failed: %v", err)
}
_, err = Get[string](c, srv.URL, Basic("user", "pass"))
if err != nil {
t.Fatalf("basic request failed: %v", err)
}
if len(auths) != 2 {
t.Fatalf("auths len = %d", len(auths))
}
if auths[0] != "Bearer token" {
t.Fatalf("bearer header = %q", auths[0])
}
wantBasic := "Basic " + base64.StdEncoding.EncodeToString([]byte("user:pass"))
if auths[1] != wantBasic {
t.Fatalf("basic header = %q", auths[1])
}
}