-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
57 lines (46 loc) · 1.75 KB
/
http_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
package rd
import (
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type TestRoundTripFunc func(req *http.Request) *http.Response
func (rt TestRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return rt(req), nil
}
func NewTestClient(fn TestRoundTripFunc) *HTTPClient {
c := &http.Client{
Transport: fn,
}
return &HTTPClient{token: Token{ExpiresIn: 3600, TokenType: "Bearer", AccessToken: "VALID_TOKEN", RefreshToken: "REFRESH_TOKEN"}, client: c}
}
func Test_AuthorizationHeaderIsPresent(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
auth := req.Header.Get("Authorization")
assert.Equal(t, auth, "Bearer VALID_TOKEN", "Authorization header is not set correctly")
return &http.Response{
StatusCode: http.StatusOK,
Header: map[string][]string{"Content-Type": {"application/json"}},
}
})
req, _ := http.NewRequest("GET", "https://example.com", nil)
_, err := client.Do(req)
assert.NoError(t, err)
}
func Test_FormPost(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
contentType := req.Header.Get("Content-Type")
assert.Equal(t, contentType, "multipart/form-data; boundary=realdebrid-boundary", "Content-Type is not set properly")
body, _ := ioutil.ReadAll(req.Body)
bodyString := string(body)
expectedBody := "--realdebrid-boundary\r\nContent-Disposition: form-data; name=\"hello\"\r\n\r\nworld\r\n--realdebrid-boundary--\r\n"
assert.Equal(t, bodyString, expectedBody)
return &http.Response{
StatusCode: http.StatusOK,
Header: map[string][]string{"Content-Type": {"application/json"}},
}
})
_, err := httpPostForm(client, "https://example.com", map[string]string{"hello": "world"})
assert.NoError(t, err)
}