Skip to content

Commit

Permalink
Add convenience methods that support formatting URLs and body (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher authored Oct 24, 2020
1 parent bf5f747 commit ce65d28
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
37 changes: 37 additions & 0 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func (a *APITest) Get(url string) *Request {
return a.request
}

// Getf is a convenience method that adds formatting support to Get
func (a *APITest) Getf(format string, args ...interface{}) *Request {
return a.Get(fmt.Sprintf(format, args...))
}

// Post is a convenience method for setting the request as http.MethodPost
func (a *APITest) Post(url string) *Request {
r := a.request
Expand All @@ -251,6 +256,11 @@ func (a *APITest) Post(url string) *Request {
return r
}

// Postf is a convenience method that adds formatting support to Post
func (a *APITest) Postf(format string, args ...interface{}) *Request {
return a.Post(fmt.Sprintf(format, args...))
}

// Put is a convenience method for setting the request as http.MethodPut
func (a *APITest) Put(url string) *Request {
r := a.request
Expand All @@ -259,26 +269,47 @@ func (a *APITest) Put(url string) *Request {
return r
}

// Putf is a convenience method that adds formatting support to Put
func (a *APITest) Putf(format string, args ...interface{}) *Request {
return a.Put(fmt.Sprintf(format, args...))
}

// Delete is a convenience method for setting the request as http.MethodDelete
func (a *APITest) Delete(url string) *Request {
a.request.method = http.MethodDelete
a.request.url = url
return a.request
}

// Deletef is a convenience method that adds formatting support to Delete
func (a *APITest) Deletef(format string, args ...interface{}) *Request {
return a.Delete(fmt.Sprintf(format, args...))
}

// Patch is a convenience method for setting the request as http.MethodPatch
func (a *APITest) Patch(url string) *Request {
a.request.method = http.MethodPatch
a.request.url = url
return a.request
}

// Patchf is a convenience method that adds formatting support to Patch
func (a *APITest) Patchf(format string, args ...interface{}) *Request {
return a.Patch(fmt.Sprintf(format, args...))
}

// URL is a builder method for setting the url of the request
func (r *Request) URL(url string) *Request {
r.url = url
return r
}

// URLf is a builder method for setting the url of the request and supports a formatter
func (r *Request) URLf(format string, args ...interface{}) *Request {
r.url = fmt.Sprintf(format, args...)
return r
}

// Body is a builder method to set the request body
func (r *Request) Body(b string) *Request {
r.body = b
Expand Down Expand Up @@ -456,6 +487,12 @@ func (r *Response) Body(b string) *Response {
return r
}

// Bodyf is the expected response body that supports a formatter
func (r *Response) Bodyf(format string, args ...interface{}) *Response {
r.body = fmt.Sprintf(format, args...)
return r
}

// BodyFromFile reads the given file and uses the content as the expected response body
func (r *Response) BodyFromFile(f string) *Response {
b, err := ioutil.ReadFile(f)
Expand Down
83 changes: 83 additions & 0 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,69 @@ func TestApiTest_AddsJSONBodyToRequest(t *testing.T) {
End()
}

func TestApiTest_RequestURLFormat(t *testing.T) {
apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Getf("/user/%s", "1234").
Expect(t).
Status(http.StatusOK).
End()

apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Putf("/user/%s", "1234").
Expect(t).
Status(http.StatusOK).
End()

apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Patchf("/user/%s", "1234").
Expect(t).
Status(http.StatusOK).
End()

apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Postf("/user/%s", "1234").
Expect(t).
Status(http.StatusOK).
End()

apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Deletef("/user/%s", "1234").
Expect(t).
Status(http.StatusOK).
End()

apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "/user/1234", r.URL.Path)
}).
Method(http.MethodGet).
URLf("/user/%d", 1234).
Expect(t).
Status(http.StatusOK).
End()
}

func TestApiTest_JSONBody(t *testing.T) {
type bodyStruct struct {
A int `json:"a"`
Expand Down Expand Up @@ -401,6 +464,26 @@ func TestApiTest_MatchesJSONResponseBody(t *testing.T) {
End()
}

func TestApiTest_MatchesJSONResponseBodyWithFormatter(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"a": 12345}`))
if err != nil {
panic(err)
}
})

apitest.New().
Handler(handler).
Get("/hello").
Expect(t).
Bodyf(`{"a": %d}`, 12345).
Status(http.StatusCreated).
End()
}

func TestApiTest_MatchesJSONBodyFromFile(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit ce65d28

Please sign in to comment.