-
Notifications
You must be signed in to change notification settings - Fork 14
/
pipeline_test.go
72 lines (56 loc) · 2.06 KB
/
pipeline_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
package nethttplibrary
import (
assert "github.com/stretchr/testify/assert"
"net/http"
"testing"
)
type TestMiddleware struct{}
func (middleware TestMiddleware) Intercept(pipeline Pipeline, middlewareIndex int, req *http.Request) (*http.Response, error) {
req.Header.Add("test", "test-header")
return pipeline.Next(req, middlewareIndex)
}
func TestCanInterceptRequests(t *testing.T) {
transport := NewCustomTransport(&TestMiddleware{})
client := &http.Client{Transport: transport}
resp, _ := client.Get("https://example.com")
expect := "test-header"
got := resp.Request.Header.Get("test")
if expect != got {
t.Errorf("Expected: %v, but received: %v", expect, got)
}
}
func TestCanInterceptMultipleRequests(t *testing.T) {
transport := NewCustomTransport(&TestMiddleware{})
client := &http.Client{Transport: transport}
resp, _ := client.Get("https://example.com")
expect := "test-header"
got := resp.Request.Header.Get("test")
if expect != got {
t.Errorf("Expected: %v, but received: %v", expect, got)
}
resp2, _ := client.Get("https://example.com")
got2 := resp2.Request.Header.Get("test")
if expect != got2 {
t.Errorf("Expected: %v, but received: %v", expect, got2)
}
}
func TestItReturnsADefaultTransport(t *testing.T) {
transport := GetDefaultTransport()
assert.NotNil(t, transport)
defaultTransport, ok := transport.(*http.Transport)
assert.True(t, ok)
assert.True(t, defaultTransport.ForceAttemptHTTP2)
}
func TestItAcceptsACustomizedTransport(t *testing.T) {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.ForceAttemptHTTP2 = false
customTransport := NewCustomTransportWithParentTransport(transport)
assert.NotNil(t, customTransport)
result, ok := customTransport.middlewarePipeline.transport.(*http.Transport)
assert.True(t, ok)
assert.False(t, result.ForceAttemptHTTP2)
}
func TestItGetsADefaultTransportIfNoneIsProvided(t *testing.T) {
customTransport := NewCustomTransport()
assert.NotNil(t, customTransport.middlewarePipeline.transport)
}