-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripperware_test.go
44 lines (40 loc) · 1.49 KB
/
tripperware_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
package httpwares_test
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/improbable-eng/go-httpwares"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func assertingTripperware(t *testing.T, placeInChain int) httpwares.Tripperware {
return func(next http.RoundTripper) http.RoundTripper {
return httpwares.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
for i := 0; i < placeInChain; i++ {
require.NotEmpty(t, req.Header.Get(fmt.Sprintf("assert-%d", i)), "%d iteration of round tripper must have the headers from previous ones")
}
req.Header.Set(fmt.Sprintf("assert-%d", placeInChain), "true")
return next.RoundTrip(req)
})
}
}
func TestTripperwareChainsInFifoOrder(t *testing.T) {
numWares := 5
retResp := &http.Response{StatusCode: 400}
retErr := errors.New("some err")
var wares []httpwares.Tripperware
for i := 0; i < numWares; i++ {
wares = append(wares, assertingTripperware(t, i))
}
finalTripper := httpwares.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
for i := 0; i < numWares; i++ {
require.NotEmpty(t, req.Header.Get(fmt.Sprintf("assert-%d", i)), "the final round tripper must see the assserts of previous ones: %d", i)
}
return retResp, retErr
})
req, _ := http.NewRequest("GET", "http://whatever", nil)
outResp, outErr := httpwares.WrapClient(&http.Client{Transport: finalTripper}, wares...).Transport.RoundTrip(req)
assert.Equal(t, retResp, outResp)
assert.Equal(t, retErr, outErr)
}