-
Notifications
You must be signed in to change notification settings - Fork 0
/
authhttp_test.go
102 lines (76 loc) · 2.72 KB
/
authhttp_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
package authhttp_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/b3ntly/go-authhttp"
"net/http"
"bytes"
"io/ioutil"
"fmt"
"encoding/json"
)
type HTTPBinBasicAuthQuery struct {
Authenticated bool `json:"authenticated"`
User string `json:"user"`
}
type HTTBinHeadersQuery struct {
Headers struct {
Accept string `json:"Accept"`
AcceptEncoding string `json:"Accept-Encoding"`
AcceptLanguage string `json:"Accept-Language"`
Connection string `json:"Connection"`
Cookie string `json:"Cookie"`
Host string `json:"Host"`
Referer string `json:"Referer"`
UpgradeInsecureRequests string `json:"Upgrade-Insecure-Requests"`
UserAgent string `json:"User-Agent"`
} `json:"headers"`
}
func TestWithBasicAuth(t *testing.T) {
t.Run("WithBasicAuth will return a client whose future requests satisfy the Basic Auth spec", func(t *testing.T){
const user = "Benjamin"
const password = "Jones"
client := authhttp.NewHTTPClient(authhttp.WithBasicAuth(user, password))
req, err := http.NewRequest("GET", fmt.Sprintf("https://httpbin.org/basic-auth/%v/%v", user, password), bytes.NewReader([]byte("hello")))
require.Nil(t, err)
resp, err := client.Do(req)
require.Nil(t, err)
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
response := &HTTPBinBasicAuthQuery{}
err = json.Unmarshal(contents, response)
require.Nil(t, err)
require.Equal(t, true, response.Authenticated)
require.Equal(t, user, response.User)
})
t.Run("WithBasicAuth with improper parameters will not satisfy the Basic Auth spec", func(t *testing.T){
const user = "ben"
const password = "jones"
const wrongPassword = "wrong"
client := authhttp.NewHTTPClient(authhttp.WithBasicAuth(user, wrongPassword))
req, err := http.NewRequest("GET", fmt.Sprintf("https://httpbin.org/basic-auth/%v/%v", user, password), bytes.NewReader([]byte("hello")))
require.Nil(t, err)
resp, err := client.Do(req)
require.Nil(t, err)
require.Equal(t, 401, resp.StatusCode)
})
}
func TestWithHeader(t *testing.T) {
t.Run("WithHeader will send requests with the desired headers", func(t *testing.T){
const headerKey = "Accept"
const headerValue = "application/json"
client := authhttp.NewHTTPClient(authhttp.WithHeader(headerKey, headerValue))
req, err := http.NewRequest("GET", "https://httpbin.org/headers", bytes.NewReader([]byte("")))
require.Nil(t, err)
resp, err := client.Do(req)
require.Nil(t, err)
require.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
response := &HTTBinHeadersQuery{}
err = json.Unmarshal(contents, response)
require.Equal(t, headerValue, response.Headers.Accept)
})
}