Skip to content

Commit

Permalink
add Don't See method
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Nov 2, 2024
1 parent ea09c5d commit 7421ff6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
15 changes: 11 additions & 4 deletions contracts/testing/test_response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package testing

type TestResponse interface {
IsSuccessful() bool
IsServerError() bool
Content() (string, error)
Json() (map[string]any, error)
AssertStatus(status int) TestResponse
AssertOk() TestResponse
AssertCreated() TestResponse
Expand All @@ -27,9 +31,12 @@ type TestResponse interface {
AssertInternalServerError() TestResponse
AssertServiceUnavailable() TestResponse
AssertHeader(headerName, value string) TestResponse
AssertHeaderMissing(headerName string) TestResponse
AssertHeaderMissing(string) TestResponse
AssertCookie(name, value string) TestResponse
AssertCookieExpired(name string) TestResponse
AssertCookieNotExpired(name string) TestResponse
AssertCookieMissing(name string) TestResponse
AssertCookieExpired(string) TestResponse
AssertCookieNotExpired(string) TestResponse
AssertCookieMissing(string) TestResponse
AssertSuccessful() TestResponse
AssertServerError() TestResponse
AssertDontSee([]string, ...bool) TestResponse
}
48 changes: 48 additions & 0 deletions testing/test_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testing

import (
"fmt"
"html"
"io"
"net/http"
"testing"
Expand Down Expand Up @@ -37,6 +38,20 @@ func (r *TestResponseImpl) Json() (map[string]any, error) {
return testAble.Json(), nil
}

func (r *TestResponseImpl) IsSuccessful() bool {
statusCode := r.getStatusCode()
return statusCode >= 200 && statusCode < 300
}

func (r *TestResponseImpl) IsServerError() bool {
statusCode := r.getStatusCode()
return statusCode >= 500 && statusCode < 600
}

func (r *TestResponseImpl) Content() (string, error) {
return r.getContent()
}

func (r *TestResponseImpl) AssertStatus(status int) contractstesting.TestResponse {
actual := r.getStatusCode()
assert.Equal(r.t, status, actual, fmt.Sprintf("Expected response status code [%d] but received %d.", status, actual))
Expand Down Expand Up @@ -211,6 +226,39 @@ func (r *TestResponseImpl) AssertCookieMissing(name string) contractstesting.Tes
return r
}

func (r *TestResponseImpl) AssertSuccessful() contractstesting.TestResponse {
assert.True(r.t, r.IsSuccessful(), fmt.Sprintf("Expected response status code >=200, <300 but received %d.", r.getStatusCode()))

return r
}

func (r *TestResponseImpl) AssertServerError() contractstesting.TestResponse {
assert.True(r.t, r.IsServerError(), fmt.Sprintf("Expected response status code >=500, <600 but received %d.", r.getStatusCode()))

return r
}

func (r *TestResponseImpl) AssertDontSee(value []string, escaped ...bool) contractstesting.TestResponse {
content, err := r.getContent()
assert.Nil(r.t, err)

shouldEscape := true
if len(escaped) > 0 {
shouldEscape = escaped[0]
}

for _, v := range value {
checkValue := v
if shouldEscape {
checkValue = html.EscapeString(v)
}

assert.NotContains(r.t, content, checkValue, fmt.Sprintf("Response should not contain '%s', but it was found.", checkValue))
}

return r
}

func (r *TestResponseImpl) getStatusCode() int {
return r.response.StatusCode
}
Expand Down

0 comments on commit 7421ff6

Please sign in to comment.