Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #269 Unit tests for Request and Response properties of AssertionContext #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
132 changes: 132 additions & 0 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,135 @@ func TestExpect_Adapters(t *testing.T) {
assert.Contains(t, message, "test logger called")
})
}

type contextAssertionHandler struct {
Formatter Formatter
Reporter Reporter
Logger Logger
AssertionContext *AssertionContext
}

// Success implements AssertionHandler.Success.
func (h *contextAssertionHandler) Success(ctx *AssertionContext) {
if h.Formatter == nil {
panic("DefaultAssertionHandler.Formatter is nil")
}
h.Formatter.FormatSuccess(ctx)
h.AssertionContext = ctx

}

// Failure implements AssertionHandler.Failure.
func (h *contextAssertionHandler) Failure(
ctx *AssertionContext, failure *AssertionFailure,
) {
if h.Formatter == nil {
panic("DefaultAssertionHandler.Formatter is nil")
}

switch failure.Severity {
case SeverityError:
if h.Reporter == nil {
panic("DefaultAssertionHandler.Reporter is nil")
}

h.Formatter.FormatFailure(ctx, failure)
h.AssertionContext = ctx

case SeverityLog:
if h.Logger == nil {
return
}

h.Formatter.FormatFailure(ctx, failure)
h.AssertionContext = ctx

}
}

func TestExpect_AssertionContext_Success(t *testing.T) {
client := &mockClient{}

reporter := NewAssertReporter(t)
formatter := &DefaultFormatter{}

handler := &contextAssertionHandler{Reporter: reporter, Formatter: formatter}

config := Config{
BaseURL: "http://example.com",
Client: client,
Reporter: reporter,
Formatter: formatter,
AssertionHandler: handler,
}

e := WithConfig(config)

req := e.GET("/test").WithText("test")
resp := req.Expect()
body := resp.Body()
assert.Equal(t, req, handler.AssertionContext.Request)
assert.Equal(t, body.value, handler.AssertionContext.Response.Body().value)
}

func TestExpect_AssertionContextResponseRequest(t *testing.T) {

prepare := func(client Client) (*Expect, Config, *contextAssertionHandler) {
reporter := NewAssertReporter(t)
formatter := &DefaultFormatter{}

handler := &contextAssertionHandler{Reporter: reporter, Formatter: formatter}

config := Config{
BaseURL: "http://example.com",
Client: client,
Reporter: reporter,
Formatter: formatter,
AssertionHandler: handler,
}
return WithConfig(config), config, handler
}

t.Run("success", func(t *testing.T) {
e, _, handler := prepare(&mockClient{})
req := e.GET("/test").WithText("test")
resp := req.Expect()
resp.Body()
assert.Equal(t, &req, &handler.AssertionContext.Request)
assert.Equal(t, &resp, &handler.AssertionContext.Response)
})

t.Run("fail request", func(t *testing.T) {
e, _, handler := prepare(&mockClient{err: errors.New("test")})
req := e.GET("/test").WithText("test")
resp := req.Expect()
resp.Body()
assert.Equal(t, &req, &handler.AssertionContext.Request)
assert.Nil(t, handler.AssertionContext.Response)
})

t.Run("fail response", func(t *testing.T) {
client := ClientFunc(func(_ *http.Request) (*http.Response, error) {
return &http.Response{
Status: "Test Status",
StatusCode: 504,
}, nil
})
e, _, handler := prepare(client)
req := e.GET("/test")
resp := req.Expect()
resp.Body()
assert.Equal(t, &req, &handler.AssertionContext.Request)
assert.Equal(t, &resp, &handler.AssertionContext.Response)
})

t.Run("fail response children", func(t *testing.T) {
e, _, handler := prepare(&mockClient{})
req := e.GET("/test").WithText("{{}")
resp := req.Expect()
resp.JSON().Array()
req.chain.assertFlags(t, flagFailedChildren)
assert.Equal(t, &req, &handler.AssertionContext.Request)
assert.Equal(t, &resp, &handler.AssertionContext.Response)
})
}