Skip to content

Commit

Permalink
Adding custom context to Request (#126) (#127)
Browse files Browse the repository at this point in the history
closes #126
  • Loading branch information
emmanuelay authored Nov 5, 2022
1 parent 509d989 commit aed522b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ func TestApi(t *testing.T) {
}
```

#### Pass a custom context to the request

```go
func TestApi(t *testing.T) {
apitest.Handler(handler).
Get("/hello").
WithContext(context.TODO()).
Expect(t).
Status(http.StatusOK).
End()
}
```

#### Provide cookies in the request

```go
Expand Down
12 changes: 12 additions & 0 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apitest

import (
"bytes"
"context"
"encoding/json"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -229,6 +230,7 @@ type Request struct {
multipart *multipart.Writer
cookies []*Cookie
basicAuth string
context context.Context
apiTest *APITest
}

Expand Down Expand Up @@ -484,6 +486,12 @@ func (r *Request) BasicAuth(username, password string) *Request {
return r
}

// WithContext is a builder method to set a context on the request
func (r *Request) WithContext(ctx context.Context) *Request {
r.context = ctx
return r
}

// FormData is a builder method to set the body form data
// Also sets the content type of the request to application/x-www-form-urlencoded
func (r *Request) FormData(name string, values ...string) *Request {
Expand Down Expand Up @@ -976,6 +984,10 @@ func (a *APITest) buildRequest() *http.Request {
}

req, _ := http.NewRequest(a.request.method, a.request.url, bytes.NewBufferString(a.request.body))
if a.request.context != nil {
req = req.WithContext(a.request.context)
}

req.URL.RawQuery = formatQuery(a.request)
req.Host = SystemUnderTestDefaultName
if a.networkingEnabled {
Expand Down
45 changes: 45 additions & 0 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,51 @@ func TestApiTest_AddsBasicAuthToRequest(t *testing.T) {
End()
}

func TestApiTest_AddsTimedOutContextToRequest(t *testing.T) {

handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Nanosecond * 10)
if r.Context().Err() == context.DeadlineExceeded {
w.WriteHeader(http.StatusRequestTimeout)
}
w.WriteHeader(http.StatusOK)
})

timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Nanosecond*1)
defer cancel()

apitest.New("test with timed out context").
Handler(handler).
Get("/hello").
WithContext(timeoutCtx).
Expect(t).
Status(http.StatusRequestTimeout).
End()
}

func TestApiTest_AddsCancelledContextToRequest(t *testing.T) {

handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
if r.Context().Err() == context.Canceled {
w.WriteHeader(http.StatusNoContent)
}
w.WriteHeader(http.StatusOK)
})

cancelCtx, cancel := context.WithCancel(context.Background())
cancel()

apitest.New("test with canceled context").
Handler(handler).
Get("/hello").
WithContext(cancelCtx).
Expect(t).
Status(http.StatusNoContent).
End()
}

func TestApiTest_GraphQLQuery(t *testing.T) {
apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit aed522b

Please sign in to comment.