Skip to content

Commit

Permalink
Allow custom http client on serve matcher.
Browse files Browse the repository at this point in the history
Signed-off-by: Sophie Wigmore <[email protected]>
  • Loading branch information
robdimsdale authored and ForestEckhardt committed Apr 12, 2022
1 parent 54ab038 commit 9a1f56d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
12 changes: 11 additions & 1 deletion matchers/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
func Serve(expected interface{}) *ServeMatcher {
return &ServeMatcher{
expected: expected,
client: http.DefaultClient,
docker: occam.NewDocker(),
}
}
Expand All @@ -27,6 +28,7 @@ type ServeMatcher struct {
endpoint string
docker occam.Docker
response string
client *http.Client
}

// OnPort sets the container port that is expected to be exposed.
Expand All @@ -35,6 +37,14 @@ func (sm *ServeMatcher) OnPort(port int) *ServeMatcher {
return sm
}

// WithClient sets the http client that will be used to make the request. This
// allows for non-default client settings like custom redirect handling or
// adding a cookie jar.
func (sm *ServeMatcher) WithClient(client *http.Client) *ServeMatcher {
sm.client = client
return sm
}

// WithEndpoint sets the endpoint or subdirectory where the expected content
// should be available. For example, WithEndpoint("/health") will attempt to
// access the server's /health endpoint.
Expand Down Expand Up @@ -74,7 +84,7 @@ func (sm *ServeMatcher) Match(actual interface{}) (success bool, err error) {
return false, fmt.Errorf("ServeMatcher looking for response from container port %s which is not in container port map", port)
}

response, err := http.Get(fmt.Sprintf("http://%s:%s%s", container.Host(), container.HostPort(port), sm.endpoint))
response, err := sm.client.Get(fmt.Sprintf("http://%s:%s%s", container.Host(), container.HostPort(port), sm.endpoint))

if err != nil {
return false, err
Expand Down
35 changes: 35 additions & 0 deletions matchers/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func testServe(t *testing.T, context spec.G, it spec.S) {
case "/":
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "some string")
case "/redirect":
w.Header()["Location"] = []string{"/"}
w.WriteHeader(http.StatusMovedPermanently)
case "/empty":
// do nothing
case "/teapot":
Expand Down Expand Up @@ -149,6 +152,38 @@ func testServe(t *testing.T, context spec.G, it spec.S) {
})
})

context("when given a client", func() {
var (
redirectFunctionCalled bool
)

it.Before(func() {
redirectFunctionCalled = false

client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
redirectFunctionCalled = true
return nil
},
}

matcher = matcher.WithClient(client)
})

it("uses the provided client", func() {
result, err := matcher.WithEndpoint("/redirect").Match(occam.Container{
Ports: map[string]string{
"8080": port,
},
Env: map[string]string{"PORT": "8080"},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(BeTrue())

Expect(redirectFunctionCalled).To(BeTrue())
})
})

context("when given a port", func() {
it.Before(func() {
matcher = matcher.OnPort(8080)
Expand Down

0 comments on commit 9a1f56d

Please sign in to comment.