Skip to content

Commit

Permalink
Merge pull request #5 from maxcnunes/custom-assertor-func
Browse files Browse the repository at this point in the history
Creating a new CustomAssertor type
  • Loading branch information
erutherford authored Mar 9, 2021
2 parents 774018c + 8d3b266 commit 4368940
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: go

go:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- 1.16.x
- master

script:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ supported assertions are:
* The expected body of your request

[WithTesting](https://godoc.org/github.com/maxcnunes/httpfake#WithTesting) **must** be provided as a server
option when creating the test server if intend to set request assertions. Failing to set the option
option when creating the test server if you intend to set request assertions. Failing to set the option
when using request assertions will result in a panic.

### Custom Assertions

You can also provide your own request assertions by creating a type that implements the
[Assertor interface](https://godoc.org/github.com/maxcnunes/httpfake#Assertor). The `Assertor.Log` method will be
You can also provide your own assertions by creating a type that implements the
[Assertor interface](https://godoc.org/github.com/maxcnunes/httpfake#Assertor) or utilizing the
[CustomAssertor function type](https://pkg.go.dev/github.com/maxcnunes/httpfake#CustomAssertor). The `Assertor.Log` method will be
called for each assertion before it's processed. The `Assertor.Error` method will only be called if the
`Assertor.Assert` method returns an error.

Expand Down
19 changes: 19 additions & 0 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ func (b *requiredBody) Log(t testing.TB) {
func (b *requiredBody) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}

// CustomAssertor provides a function signature that implements the Assertor interface. This allows for
// adhoc creation of a custom assertion for use with the AssertCustom assertor.
type CustomAssertor func(r *http.Request) error

// Assert runs the CustomAssertor assertion against the provided request
func (c CustomAssertor) Assert(r *http.Request) error {
return c(r)
}

// Log prints a testing info log for the CustomAssertor
func (c CustomAssertor) Log(t testing.TB) {
t.Log("Testing request with a custom assertor")
}

// Error prints a testing error for the CustomAssertor
func (c CustomAssertor) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
50 changes: 50 additions & 0 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,36 @@ func TestAssertors_Assert(t *testing.T) {
},
expectedErr: "error reading the request body; the request body is nil",
},
{
name: "CustomAssertor should execute the custom assertor as expected",
assertor: CustomAssertor(func(r *http.Request) error {
return nil
}),
requestBuilder: func() (*http.Request, error) {
testReq, err := http.NewRequest(http.MethodGet, "http://fake.url", nil)
if err != nil {
return nil, err
}

return testReq, nil
},
expectedErr: "",
},
{
name: "CustomAssertor should execute the custom assertor and return an error as expected",
assertor: CustomAssertor(func(r *http.Request) error {
return errors.New("custom assertor error")
}),
requestBuilder: func() (*http.Request, error) {
testReq, err := http.NewRequest(http.MethodGet, "http://fake.url", nil)
if err != nil {
return nil, err
}

return testReq, nil
},
expectedErr: "custom assertor error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -341,6 +371,16 @@ func TestAssertors_Log(t *testing.T) {
assertor: &requiredBody{},
expected: "Testing request for a required body value\n",
},
{
name: "CustomAssertor Log should log the expected output when called",
mockTester: &mockTester{
buf: &bytes.Buffer{},
},
assertor: CustomAssertor(func(r *http.Request) error {
return nil
}),
expected: "Testing request with a custom assertor\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -401,6 +441,16 @@ func TestAssertors_Error(t *testing.T) {
assertor: &requiredBody{},
expected: "assertion error: test error",
},
{
name: "CustomAssertor Log should log the expected output when called",
mockTester: &mockTester{
buf: &bytes.Buffer{},
},
assertor: CustomAssertor(func(r *http.Request) error {
return nil
}),
expected: "assertion error: test error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4368940

Please sign in to comment.