Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions hammy/httpassert/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,52 @@ import (
"github.com/gogunit/gunit/hammy/httpassert"
)

func ExampleStatus() {
printExample(httpassert.Status(exampleResponse(http.StatusCreated, nil, ""), http.StatusCreated))
func ExampleResp_Status() {
printExample(httpassert.Response(exampleResponse(http.StatusCreated, nil, "")).Status(http.StatusCreated))
// Output:
// message="got status <201>, wanted <201>"
// success=true
}

func ExampleStatusInRange() {
printExample(httpassert.StatusInRange(exampleResponse(http.StatusNoContent, nil, ""), 200, 299))
func ExampleResp_StatusInRange() {
printExample(httpassert.Response(exampleResponse(http.StatusNoContent, nil, "")).StatusInRange(200, 299))
// Output:
// message="got status <204>, wanted in range <200..299>"
// success=true
}

func ExampleHeader() {
func ExampleResp_Header() {
resp := exampleResponse(http.StatusOK, http.Header{"Content-Type": {"application/json"}}, "")
printExample(httpassert.Header(resp, "Content-Type", "application/json"))
printExample(httpassert.Response(resp).Header("Content-Type", "application/json"))
// Output:
// message="got header <Content-Type>=<application/json>, wanted <application/json>"
// success=true
}

func ExampleHeaderContains() {
func ExampleResp_HeaderContains() {
resp := exampleResponse(http.StatusOK, http.Header{"Content-Type": {"application/json; charset=utf-8"}}, "")
printExample(httpassert.HeaderContains(resp, "Content-Type", "application/json"))
printExample(httpassert.Response(resp).HeaderContains("Content-Type", "application/json"))
// Output:
// message="got header <Content-Type>=<application/json; charset=utf-8>, wanted containing <application/json>"
// success=true
}

func ExampleBodyEqual() {
printExample(httpassert.BodyEqual(exampleResponse(http.StatusOK, nil, "hello world"), "hello world"))
func ExampleResp_BodyEqual() {
printExample(httpassert.Response(exampleResponse(http.StatusOK, nil, "hello world")).BodyEqual("hello world"))
// Output:
// message="got body <hello world>, wanted equal to <hello world>"
// success=true
}

func ExampleBodyContains() {
printExample(httpassert.BodyContains(exampleResponse(http.StatusOK, nil, "hello world"), "world"))
func ExampleResp_BodyContains() {
printExample(httpassert.Response(exampleResponse(http.StatusOK, nil, "hello world")).BodyContains("world"))
// Output:
// message="got body <hello world>, wanted containing <world>"
// success=true
}

func ExampleBodyMatchesRegexp() {
printExample(httpassert.BodyMatchesRegexp(exampleResponse(http.StatusOK, nil, "status 204"), `status \d+`))
func ExampleResp_BodyMatchesRegexp() {
printExample(httpassert.Response(exampleResponse(http.StatusOK, nil, "status 204")).BodyMatchesRegexp(`status \d+`))
// Output:
// message="got body <status 204>, wanted regexp <status \\d+>"
// success=true
Expand Down
39 changes: 29 additions & 10 deletions hammy/httpassert/httpassert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ import (
"github.com/gogunit/gunit/hammy"
)

func Status(resp *http.Response, expected int) hammy.AssertionMessage {
func Response(resp *http.Response) *Resp {
return &Resp{actual: resp}
}

type Resp struct {
actual *http.Response
}

func (r *Resp) Status(expected int) hammy.AssertionMessage {
resp := r.response()
if resp == nil {
return hammy.Assert(false, "got nil response, wanted status <%d>", expected)
}
return hammy.Assert(resp.StatusCode == expected, "got status <%d>, wanted <%d>", resp.StatusCode, expected)
}

func StatusInRange(resp *http.Response, min, max int) hammy.AssertionMessage {
func (r *Resp) StatusInRange(min, max int) hammy.AssertionMessage {
resp := r.response()
if min > max {
return hammy.Assert(false, "got invalid status range <%d..%d>", min, max)
}
Expand All @@ -27,40 +37,42 @@ func StatusInRange(resp *http.Response, min, max int) hammy.AssertionMessage {
return hammy.Assert(resp.StatusCode >= min && resp.StatusCode <= max, "got status <%d>, wanted in range <%d..%d>", resp.StatusCode, min, max)
}

func Header(resp *http.Response, key, expected string) hammy.AssertionMessage {
func (r *Resp) Header(key, expected string) hammy.AssertionMessage {
resp := r.response()
if resp == nil {
return hammy.Assert(false, "got nil response, wanted header <%s> equal to <%s>", key, expected)
}
actual := resp.Header.Get(key)
return hammy.Assert(actual == expected, "got header <%s>=<%s>, wanted <%s>", key, actual, expected)
}

func HeaderContains(resp *http.Response, key, expected string) hammy.AssertionMessage {
func (r *Resp) HeaderContains(key, expected string) hammy.AssertionMessage {
resp := r.response()
if resp == nil {
return hammy.Assert(false, "got nil response, wanted header <%s> containing <%s>", key, expected)
}
actual := resp.Header.Get(key)
return hammy.Assert(strings.Contains(actual, expected), "got header <%s>=<%s>, wanted containing <%s>", key, actual, expected)
}

func BodyEqual(resp *http.Response, expected string) hammy.AssertionMessage {
actual, result := readBody(resp)
func (r *Resp) BodyEqual(expected string) hammy.AssertionMessage {
actual, result := readBody(r.response())
if !result.IsSuccessful {
return result
}
return hammy.Assert(actual == expected, "got body <%s>, wanted equal to <%s>", actual, expected)
}

func BodyContains(resp *http.Response, expected string) hammy.AssertionMessage {
actual, result := readBody(resp)
func (r *Resp) BodyContains(expected string) hammy.AssertionMessage {
actual, result := readBody(r.response())
if !result.IsSuccessful {
return result
}
return hammy.Assert(strings.Contains(actual, expected), "got body <%s>, wanted containing <%s>", actual, expected)
}

func BodyMatchesRegexp(resp *http.Response, pattern string) hammy.AssertionMessage {
actual, result := readBody(resp)
func (r *Resp) BodyMatchesRegexp(pattern string) hammy.AssertionMessage {
actual, result := readBody(r.response())
if !result.IsSuccessful {
return result
}
Expand All @@ -72,6 +84,13 @@ func BodyMatchesRegexp(resp *http.Response, pattern string) hammy.AssertionMessa
return hammy.Assert(re.MatchString(actual), "got body <%s>, wanted regexp <%s>", actual, pattern)
}

func (r *Resp) response() *http.Response {
if r == nil {
return nil
}
return r.actual
}

func readBody(resp *http.Response) (string, hammy.AssertionMessage) {
if resp == nil {
return "", hammy.Assert(false, "got nil response, wanted response body")
Expand Down
Loading
Loading