-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathauth_basic_test.go
81 lines (73 loc) · 2.28 KB
/
auth_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
package api_tests
import (
"encoding/base64"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/sharat87/httpbun/c"
)
func TestBasicAuthSuccess(t *testing.T) {
s := assert.New(t)
resp, body := ExecRequest(R{
Path: "basic-auth/scott/tiger",
Headers: map[string][]string{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte("scott:tiger"))},
},
})
s.Equal(http.StatusOK, resp.StatusCode)
s.Equal(c.ApplicationJSON, resp.Header.Get(c.ContentType))
s.NotContains(resp.Header, c.WWWAuthenticate)
s.JSONEq(`{
"authenticated": true,
"user": "scott"
}`, body)
}
func TestBasicAuthIncorrectPassword(t *testing.T) {
s := assert.New(t)
resp, body := ExecRequest(R{
Path: "basic-auth/scott/tiger",
Headers: map[string][]string{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte("scott:incorrect"))},
},
})
s.Equal(http.StatusUnauthorized, resp.StatusCode)
s.NotContains(resp.Header, c.ContentType)
s.Equal("Basic realm=\"httpbun realm\"", resp.Header.Get(c.WWWAuthenticate))
s.Equal("", body)
}
func TestBasicAuthIncorrectUsername(t *testing.T) {
s := assert.New(t)
resp, body := ExecRequest(R{
Path: "basic-auth/scott/tiger",
Headers: map[string][]string{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte("tom:tiger"))},
},
})
s.Equal(http.StatusUnauthorized, resp.StatusCode)
s.NotContains(resp.Header, c.ContentType)
s.Equal("Basic realm=\"httpbun realm\"", resp.Header.Get(c.WWWAuthenticate))
s.Equal("", body)
}
func TestBasicAuthIncorrectCreds(t *testing.T) {
s := assert.New(t)
resp, body := ExecRequest(R{
Path: "basic-auth/scott/tiger",
Headers: map[string][]string{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte("tom:lion"))},
},
})
s.Equal(http.StatusUnauthorized, resp.StatusCode)
s.NotContains(resp.Header, c.ContentType)
s.Equal("Basic realm=\"httpbun realm\"", resp.Header.Get(c.WWWAuthenticate))
s.Equal("", body)
}
func TestBasicAuthMissingCreds(t *testing.T) {
s := assert.New(t)
resp, body := ExecRequest(R{
Path: "basic-auth/scott/tiger",
})
s.Equal(http.StatusUnauthorized, resp.StatusCode)
s.NotContains(resp.Header, c.ContentType)
s.Equal("Basic realm=\"httpbun realm\"", resp.Header.Get(c.WWWAuthenticate))
s.Equal("", body)
}