-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathheader_test.go
53 lines (46 loc) · 1.34 KB
/
header_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
package transport
import (
"net/http"
"testing"
)
type fixtureHeaderTransport struct {
Response *http.Response
Request *http.Request
Err error
}
func (c *fixtureHeaderTransport) RoundTrip(r *http.Request) (*http.Response, error) {
c.Request = r
return c.Response, c.Err
}
func TestRequestHeaderAddsHeaders(t *testing.T) {
const value string = "VALUE"
t.Parallel()
var provider = func(*http.Request) (string, string) {
return "KEY", value
}
var fixture = &fixtureHeaderTransport{}
var client = NewHeader(provider)(fixture)
var r, _ = http.NewRequest("GET", "/", nil)
_, _ = client.RoundTrip(r)
if fixture.Request.Header.Get("KEY") != value {
t.Fatal("Decorator did not add headers to the request.")
}
}
func TestResponseHeaderAddsHeaders(t *testing.T) {
const value string = "VALUE"
t.Parallel()
var provider = func(*http.Response) (string, string) {
return "KEY", value
}
resp := &http.Response{Header: http.Header{}}
var fixture = &fixtureHeaderTransport{Response: resp}
var client = NewHeaders(nil, provider)(fixture)
var r, _ = http.NewRequest("GET", "/", nil)
modifiedResp, _ := client.RoundTrip(r)
if fixture.Request.Header.Get("KEY") == value {
t.Fatal("Decorator should not add headers to the request.")
}
if modifiedResp.Header.Get("KEY") != value {
t.Fatal("Decorator did not add headers to the response.")
}
}