This repository was archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions_test.go
120 lines (99 loc) · 2.85 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
package gotcha
import (
urlValues "github.com/Sleeyax/urlValues"
"github.com/sleeyax/gotcha/internal/tests"
"io"
"net/http"
"strings"
"testing"
)
func TestOptions_Merge(t *testing.T) {
var beforeRequestCalled bool
left := NewDefaultOptions()
right := &Options{
URI: "example.com",
Retry: true,
Method: http.MethodPost,
Headers: http.Header{
"Foo": {"Bar"},
},
Body: io.NopCloser(strings.NewReader("hello world")),
SearchParams: urlValues.Values{
"xyz": {"123"},
"abc": {"def"},
urlValues.OrderKey: {"xyz", "abc"},
},
Timeout: 1000,
FollowRedirect: false,
RedirectOptions: RedirectOptions{},
Hooks: Hooks{
BeforeRequest: []BeforeRequestHook{
func(options *Options) {
beforeRequestCalled = true
},
},
},
}
options, err := left.Extend(right)
if err != nil {
t.Fatal(err)
}
// test merged Options fields
if u := options.URI; u != right.URI {
t.Errorf(tests.MismatchFormat, "url", right.URI, u)
}
if r := options.Retry; r != right.Retry {
t.Errorf(tests.MismatchFormat, "retry", right.Retry, r)
}
if m := options.Method; m != right.Method {
t.Errorf(tests.MismatchFormat, "method", right.Method, m)
}
if h := options.Headers.Get("Foo"); h != "Bar" {
t.Errorf(tests.MismatchFormat, "headers", "Bar", h)
}
optionsBody, err := io.ReadAll(options.Body)
if err != nil {
t.Error(err)
}
if b := string(optionsBody); b != "hello world" {
t.Errorf(tests.MismatchFormat, "body", "hello world", b)
}
if options.UnmarshalJson == nil || options.MarshalJson == nil {
t.Errorf("Any of the json marshal functions shouldn't be nil.")
}
if sp := options.SearchParams.EncodeWithOrder(); sp != "xyz=123&abc=def" {
t.Errorf(tests.MismatchFormat, "search parameters", "abc=def", sp)
}
if to := options.Timeout; to != right.Timeout {
t.Errorf(tests.MismatchFormat, "timeout", right.Timeout, to)
}
if fr := options.FollowRedirect; fr != right.FollowRedirect {
t.Errorf(tests.MismatchFormat, "follow redirect", right.FollowRedirect, fr)
}
if ro := options.RedirectOptions; ro != right.RedirectOptions {
t.Errorf(tests.MismatchFormat, "redirect Options", right.RedirectOptions, ro)
}
if len(options.Hooks.BeforeRequest) == 0 {
t.Errorf("At least 1 BeforeRequestHook hook should be set.")
}
options.Hooks.BeforeRequest[0](&Options{})
if !beforeRequestCalled {
t.Errorf(tests.MismatchFormat, "hook result 'beforeRequestCalled'", true, beforeRequestCalled)
}
}
func TestOptions_Merge_Bool(t *testing.T) {
testCases := [2]bool{true, false}
for _, x := range testCases {
parent := &Options{Retry: x}
for _, y := range testCases {
child := &Options{Retry: y}
merged, err := parent.Extend(child)
if err != nil {
t.Error(err)
}
if merged.Retry != child.Retry {
t.Errorf(tests.MismatchFormat, "retry", child.Retry, merged.Retry)
}
}
}
}