Skip to content

Commit

Permalink
unit test for Request and Response properties of AssertionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
m-posluszny committed Jan 10, 2024
1 parent d0cfaf9 commit abd980f
Showing 1 changed file with 132 additions and 0 deletions.
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)
})
}

0 comments on commit abd980f

Please sign in to comment.