Skip to content

Commit

Permalink
extend http round tripper spec with error in context case
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Oct 11, 2023
1 parent afba958 commit 2b618f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export PATH="${WDP}/bin:${PATH}"
export PATH="${WDP}/.tools:${PATH}"

export GO111MODULE=on

export CGO_ENABLED=1
export TESTCASE_PP_DEBUG=TRUE
19 changes: 18 additions & 1 deletion httpspec/RoundTripperMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c RoundTripperMiddlewareContract) Spec(s *testcase.Spec) {
next := testcase.Let(s, func(t *testcase.T) *RoundTripperDouble {
return &RoundTripperDouble{
RoundTripperFunc: func(r *http.Request) (*http.Response, error) {
return Response.Get(t), nil
return Response.Get(t), r.Context().Err()
},
}
})
Expand Down Expand Up @@ -79,5 +79,22 @@ func (c RoundTripperMiddlewareContract) Spec(s *testcase.Spec) {
t.Must.Nil(err)
t.Must.Equal(expectedBody.Get(t), string(actualBody))
})

s.When("request context has an error", func(s *testcase.Spec) {
Context := testcase.Let(s, func(t *testcase.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
})

request.Let(s, func(t *testcase.T) *http.Request {
return request.Super(t).WithContext(Context.Get(t))
})

s.Then("context error is propagated back", func(t *testcase.T) {
_, err := act(t)
t.Must.ErrorIs(err, Context.Get(t).Err())
})
})
})
}
4 changes: 3 additions & 1 deletion httpspec/doubles.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func (d *RoundTripperDouble) RoundTrip(r *http.Request) (*http.Response, error)
if d.RoundTripperFunc != nil {
return d.RoundTripperFunc(r)
}

if err := r.Context().Err(); err != nil {
return nil, err
}
const code = http.StatusOK
return &http.Response{
Status: http.StatusText(code),
Expand Down

0 comments on commit 2b618f4

Please sign in to comment.