-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
options_test.go
143 lines (119 loc) · 3.43 KB
/
options_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package rek
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)
var (
headers = map[string]string{"Foo": "bar"}
timeout = 11 * time.Hour
username = "user"
password = "pass"
data = struct {
Name string
Age int
}{"Methuselah", 965}
userAgent = "Feldman"
callback = func(res *Response) { fmt.Println(res.StatusCode()) }
cookies = []*http.Cookie{{Path: "/foo"}}
f = &file{FieldName: "file", Filepath: "go.mod", Params: nil}
form = map[string]string{"foo": "bar", "baq": "baz"}
bearer = "a1b2c3d4"
reqModifier = func(req *http.Request) { fmt.Println(req.URL.String()) }
apiKey = bearer
ctx = context.WithValue(context.Background(), "value", "some-val")
cl = &http.Client{}
oauthCfg = &oauth2.Config{Scopes: []string{"admin", "superuser"}}
oauthTok = &oauth2.Token{AccessToken: "a1b2c3d4"}
)
func TestOptionsBuilder(t *testing.T) {
is := assert.New(t)
opts, err := buildOptions()
is.NoError(err)
is.NotNil(opts)
is.Equal(opts.headers, map[string]string(nil))
is.Zero(opts.timeout)
is.Empty(opts.username, opts.password, opts.userAgent, opts.bearer, opts.apiKey, opts.ctx, opts.client,
opts.oauth2Cfg)
is.Nil(opts.data, opts.jsonObj, opts.callback, opts.cookies, opts.cookieJar, opts.file, opts.formData,
opts.reqModifier)
is.False(opts.disallowRedirects)
// Headers
opts, err = buildOptions(Headers(headers))
is.NoError(err)
is.Equal(opts.headers, headers)
// Timeout
opts, err = buildOptions(Timeout(timeout))
is.NoError(err)
is.Equal(opts.timeout, timeout)
// Basic auth
opts, err = buildOptions(BasicAuth(username, password))
is.NoError(err)
is.Equal(opts.username, username)
is.Equal(opts.password, password)
// Data
opts, err = buildOptions(Data(data))
is.NoError(err)
is.Equal(opts.data, data)
// User agent
opts, err = buildOptions(UserAgent(userAgent))
is.NoError(err)
is.Equal(opts.userAgent, userAgent)
// JSON
opts, err = buildOptions(Json(data))
is.NoError(err)
is.Equal(opts.jsonObj, data)
// Callback
opts, err = buildOptions(Callback(callback))
is.NoError(err)
// We can't compare functions for equality, so NotNil will have to do
is.NotNil(opts.callback)
// Cookies
opts, err = buildOptions(Cookies(cookies))
is.NoError(err)
is.Equal(opts.cookies, cookies)
// Cookie jar
// TODO
// File
opts, err = buildOptions(File(f.FieldName, f.Filepath, f.Params))
is.NoError(err)
is.Equal(opts.file, f)
// Form data
opts, err = buildOptions(FormData(form))
is.NoError(err)
is.Equal(opts.formData, form)
// Bearer
opts, err = buildOptions(Bearer(bearer))
is.NoError(err)
is.Equal(opts.bearer, bearer)
// Disallow redirects
opts, err = buildOptions(DisallowRedirects())
is.NoError(err)
is.True(opts.disallowRedirects)
// Request modifier
opts, err = buildOptions(RequestModifier(reqModifier))
is.NoError(err)
// We can't compare functions for equality, so NotNil will have to do
is.NotNil(opts.reqModifier)
// API key
opts, err = buildOptions(ApiKey(apiKey))
is.NoError(err)
is.Equal(opts.apiKey, apiKey)
// Context
opts, err = buildOptions(Context(ctx))
is.NoError(err)
is.Equal(opts.ctx, ctx)
// Client
opts, err = buildOptions(Client(cl))
is.NoError(err)
is.Equal(opts.client, cl)
// OAuth2
opts, err = buildOptions(OAuth2(oauthCfg, oauthTok))
is.NoError(err)
is.Equal(opts.oauth2Cfg.config, oauthCfg)
is.Equal(opts.oauth2Cfg.token, oauthTok)
}