-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrequest_test.go
More file actions
201 lines (180 loc) · 5.79 KB
/
request_test.go
File metadata and controls
201 lines (180 loc) · 5.79 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNewRequest_ClientHeadersAndAuth(t *testing.T) {
oldConfig := *config
oldClientType := currentClientType
oldTrToken := Tr_csrfToken
defer func() {
tmpConf := oldConfig
config = &tmpConf
currentClientType = oldClientType
Tr_csrfTokenMutex.Lock()
Tr_csrfToken = oldTrToken
Tr_csrfTokenMutex.Unlock()
}()
tmpConf := oldConfig
config = &tmpConf
config.UseBasicAuth = true
config.ClientUsername = "user-a"
config.ClientPassword = "pass-a"
currentClientType = "Transmission"
Tr_SetCSRFToken("csrf-a")
req := NewRequest(true, "http://example.com", "a=1", true, false, nil)
if req == nil {
t.Fatalf("NewRequest returned nil")
}
if req.Header.Get("User-Agent") != programUserAgent {
t.Fatalf("unexpected user-agent: %q", req.Header.Get("User-Agent"))
}
if req.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("unexpected content-type: %q", req.Header.Get("Content-Type"))
}
if req.Header.Get("X-Transmission-Session-Id") != "csrf-a" {
t.Fatalf("unexpected transmission csrf header: %q", req.Header.Get("X-Transmission-Session-Id"))
}
username, password, ok := req.BasicAuth()
if !ok || username != "user-a" || password != "pass-a" {
t.Fatalf("unexpected basic auth: ok=%v user=%q pass=%q", ok, username, password)
}
}
func TestNewRequest_CacheHeaders(t *testing.T) {
oldETag := urlETagCache
oldLastMod := urlLastModCache
defer func() {
urlETagCache = oldETag
urlLastModCache = oldLastMod
}()
urlETagCache = map[string]string{"http://example.com/rules": "etag-1"}
urlLastModCache = map[string]string{"http://example.com/rules": "Mon, 01 Jan 2024 00:00:00 GMT"}
req := NewRequest(false, "http://example.com/rules", "", false, true, nil)
if req == nil {
t.Fatalf("NewRequest returned nil")
}
if req.Header.Get("If-None-Match") != "etag-1" {
t.Fatalf("unexpected If-None-Match: %q", req.Header.Get("If-None-Match"))
}
if req.Header.Get("If-Modified-Since") == "" {
t.Fatalf("If-Modified-Since should be set")
}
}
func TestFetch_CacheRoundTrip(t *testing.T) {
oldClientExternal := httpClientExternal
oldETag := urlETagCache
oldLastMod := urlLastModCache
defer func() {
httpClientExternal = oldClientExternal
urlETagCache = oldETag
urlLastModCache = oldLastMod
}()
callCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
if callCount == 1 {
w.Header().Set("ETag", "etag-a")
w.Header().Set("Last-Modified", "Mon, 01 Jan 2024 00:00:00 GMT")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"ok":true}`))
return
}
if r.Header.Get("If-None-Match") != "etag-a" {
t.Fatalf("If-None-Match not sent on cached request: %q", r.Header.Get("If-None-Match"))
}
if r.Header.Get("If-Modified-Since") == "" {
t.Fatalf("If-Modified-Since not sent on cached request")
}
w.WriteHeader(http.StatusNotModified)
}))
defer server.Close()
httpClientExternal = *server.Client()
urlETagCache = map[string]string{}
urlLastModCache = map[string]string{}
code, _, body := Fetch(server.URL, false, false, true, nil)
if code != http.StatusOK {
t.Fatalf("first Fetch status=%d, want 200", code)
}
if string(body) != `{"ok":true}` {
t.Fatalf("unexpected first body: %q", string(body))
}
code, _, body = Fetch(server.URL, false, false, true, nil)
if code != http.StatusNotModified {
t.Fatalf("second Fetch status=%d, want 304", code)
}
if body != nil {
t.Fatalf("second Fetch body should be nil for 304")
}
}
func TestFetch_Transmission409SetsCSRF(t *testing.T) {
oldClient := httpClient
oldClientType := currentClientType
oldToken := Tr_csrfToken
defer func() {
httpClient = oldClient
currentClientType = oldClientType
Tr_csrfTokenMutex.Lock()
Tr_csrfToken = oldToken
Tr_csrfTokenMutex.Unlock()
}()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Transmission-Session-Id", "csrf-new")
w.WriteHeader(http.StatusConflict)
}))
defer server.Close()
httpClient = *server.Client()
currentClientType = "Transmission"
Tr_csrfTokenMutex.Lock()
Tr_csrfToken = ""
Tr_csrfTokenMutex.Unlock()
code, _, body := Fetch(server.URL, false, true, false, nil)
if code != http.StatusConflict {
t.Fatalf("Fetch status=%d, want 409", code)
}
if body != nil {
t.Fatalf("body should be nil for 409 token bootstrap path")
}
Tr_csrfTokenMutex.RLock()
token := Tr_csrfToken
Tr_csrfTokenMutex.RUnlock()
if token != "csrf-new" {
t.Fatalf("Tr_csrfToken=%q, want %q", token, "csrf-new")
}
}
func TestNewRequest_CustomHeadersPreserved(t *testing.T) {
custom := map[string]string{
"Content-Type": "application/json",
"User-Agent": "unit-test-agent",
}
req := NewRequest(true, "http://example.com", `{"a":1}`, false, false, &custom)
if req == nil {
t.Fatalf("NewRequest returned nil")
}
if req.Header.Get("Content-Type") != "application/json" {
t.Fatalf("unexpected content-type: %q", req.Header.Get("Content-Type"))
}
if !strings.Contains(req.Header.Get("User-Agent"), "unit-test-agent") {
t.Fatalf("unexpected user-agent: %q", req.Header.Get("User-Agent"))
}
}
func TestSubmit_InputTypes(t *testing.T) {
var capturedBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedBody, _ = io.ReadAll(r.Body)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
// Test string input.
Submit(server.URL, "string-data", false, false, nil)
if string(capturedBody) != "string-data" {
t.Fatalf("unexpected body for string input: %q", string(capturedBody))
}
// Test byte slice input.
Submit(server.URL, []byte("byte-data"), false, false, nil)
if string(capturedBody) != "byte-data" {
t.Fatalf("unexpected body for byte slice input: %q", string(capturedBody))
}
}