Skip to content

Commit

Permalink
Custom() functions return an error that can be tested against using e…
Browse files Browse the repository at this point in the history
…rrors.Is and errors.As (#10)
  • Loading branch information
Eun authored Nov 18, 2020
1 parent 51516d3 commit d4b7f62
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.14.x]
go-version: [1.14.x, 1.15.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# go-hit [![Actions Status](https://github.com/Eun/go-hit/workflows/CI/badge.svg)](https://github.com/Eun/go-hit/actions) [![Coverage Status](https://coveralls.io/repos/github/Eun/go-hit/badge.svg?branch=master)](https://coveralls.io/github/Eun/go-hit?branch=master) [![PkgGoDev](https://img.shields.io/badge/pkg.go.dev-reference-blue)](https://pkg.go.dev/github.com/Eun/go-hit) [![GoDoc](https://godoc.org/github.com/Eun/go-hit?status.svg)](https://godoc.org/github.com/Eun/go-hit) [![go-report](https://goreportcard.com/badge/github.com/Eun/go-hit)](https://goreportcard.com/report/github.com/Eun/go-hit)
hit is an **h**ttp **i**ntegration **t**est framework written in golang.

Expand Down Expand Up @@ -116,16 +117,19 @@ MustDo(
```go
MustDo(
Get("https://httpbin.org/get"),
Send().Custom(func(hit Hit) {
Send().Custom(func(hit Hit) error {
hit.Request().Body().SetStringf("Hello %s", "World")
return nil
}),
Expect().Custom(func(hit Hit) {
Expect().Custom(func(hit Hit) error {
if len(hit.Response().Body().MustString()) <= 0 {
t.FailNow()
return errors.New("expected the body to be not empty")
}
return nil
}),
Custom(AfterExpectStep, func(Hit) {
Custom(AfterExpectStep, func(Hit) error {
fmt.Println("everything done")
return nil
}),
)
```
Expand Down
9 changes: 6 additions & 3 deletions clear_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package hit_test
import (
"testing"

"errors"

. "github.com/Eun/go-hit"
"github.com/stretchr/testify/require"
)

// ⚠️⚠️⚠️ This file was autogenerated by generators/clear/tests ⚠️⚠️⚠️ //

func storeSteps(steps *[]IStep) IStep {
return Custom(CleanStep, func(hit Hit) {
return Custom(CleanStep, func(hit Hit) error {
*steps = hit.Steps()
return nil
})
}
func expectSteps(t *testing.T, expectSteps *[]IStep, removedStepsCount int) IStep {
return Custom(BeforeExpectStep, func(hit Hit) {
return Custom(BeforeExpectStep, func(hit Hit) error {
require.Len(t, hit.Steps(), len(*expectSteps)-removedStepsCount)
panic("TestOK")
return errors.New("TestOK")
})
}

Expand Down
3 changes: 2 additions & 1 deletion debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ func TestDebug(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body().String("Hello World"),
Custom(BeforeExpectStep, func(hit Hit) {
Custom(BeforeExpectStep, func(hit Hit) error {
hit.MustDo(Fdebug(buf).Request())
return nil
}),
)

Expand Down
12 changes: 12 additions & 0 deletions errortrace/errortrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,15 @@ func (et *ErrorTrace) Error() string {
func (et *ErrorTrace) ErrorText() string {
return et.error.Error()
}

// Implement xerrors

// Is implements the xerrors interface so we can use the xerrors.Is() function.
func (et *ErrorTrace) Is(err error) bool {
return et.error == err
}

// Unwrap implements the xerrors.Wrapper interface.
func (et *ErrorTrace) Unwrap() error {
return et.error
}
8 changes: 5 additions & 3 deletions examples/hash/example_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ import (

func TestHash(t *testing.T) {
// hashes the payload with md5 and puts the value into the Content-Signature header
hashBody := func(hit Hit) {
hashBody := func(hit Hit) error {
hash := md5.Sum(hit.Request().Body().MustBytes())
hit.Request().Header.Set("Content-Signature", hex.EncodeToString(hash[:]))
return nil
}

// expectsInnerText
expectInnerText := func(text string) func(hit Hit) {
return func(hit Hit) {
expectInnerText := func(text string) func(hit Hit) error {
return func(hit Hit) error {
if !strings.Contains(hit.Response().Body().MustString(), text) {
t.Error(fmt.Sprintf("expected %s", text))
}
return nil
}
}

Expand Down
10 changes: 7 additions & 3 deletions examples/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"net/http/cookiejar"
"testing"

. "github.com/Eun/go-hit"
"golang.org/x/xerrors"

"github.com/stretchr/testify/require"

. "github.com/Eun/go-hit"
)

func TestHead(t *testing.T) {
Expand All @@ -30,12 +33,13 @@ func TestPost(t *testing.T) {
func TestStatusCode(t *testing.T) {
Test(t,
Head("https://google.com"),
Expect().Custom(func(e Hit) {
Expect().Custom(func(e Hit) error {
if e.Response().StatusCode > 400 {
// hit will catch errors
// so feel free to panic here
panic("Expected StatusCode to be less than 400")
return xerrors.New("Expected StatusCode to be less than 400")
}
return nil
}),
)
}
Expand Down
8 changes: 4 additions & 4 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ type IExpect interface {
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Custom(func(hit Hit) {
// Expect().Custom(func(hit Hit) error {
// if hit.Response().StatusCode != 200 {
// panic("Expected 200")
// return errors.New("Expected 200")
// }
// return nil
// }),
// )
Custom(fn Callback) IStep
Expand Down Expand Up @@ -94,8 +95,7 @@ func (exp *expect) Custom(fn Callback) IStep {
When: ExpectStep,
CallPath: exp.cleanPath.Push("Custom", []interface{}{fn}),
Exec: func(hit *hitImpl) error {
fn(hit)
return nil
return fn(hit)
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions expect_body_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func TestExpectBodyJSON_Equal(t *testing.T) {
Do(
Post(s.URL),
Send().Body().String(`"Hello Universe"`),
Expect().Custom(func(hit Hit) {
Expect().Custom(func(hit Hit) error {
hit.MustDo(Expect().Body().JSON().Equal("Hello World"))
return nil
}),
),
PtrStr("not equal"), nil, nil, nil, nil, nil, nil,
Expand Down Expand Up @@ -315,8 +316,9 @@ func TestExpectBodyJSON_NotEqual(t *testing.T) {
Do(
Post(s.URL),
Send().Body().String(`"Hello World"`),
Expect().Custom(func(hit Hit) {
Expect().Custom(func(hit Hit) error {
hit.MustDo(Expect().Body().JSON().NotEqual("Hello World"))
return nil
}),
),
PtrStr(`should not be "Hello World"`),
Expand Down
3 changes: 2 additions & 1 deletion expect_formvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func mockFormValues() IStep {
return Custom(BeforeExpectStep, func(hit Hit) {
return Custom(BeforeExpectStep, func(hit Hit) error {
s := url.Values{
"X-String": {"Foo"},
"X-Strings": {"Hello", "World"},
Expand All @@ -19,6 +19,7 @@ func mockFormValues() IStep {
}.Encode()
hit.Request().Body().SetString(s)
hit.Response().Body().SetString(s)
return nil
})
}

Expand Down
3 changes: 2 additions & 1 deletion expect_header_trailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// for convenience we test headers and trailers here

func mockHeadersAndTrailers() IStep {
return Custom(BeforeExpectStep, func(hit Hit) {
return Custom(BeforeExpectStep, func(hit Hit) error {
m := map[string][]string{
"X-String": {"Foo"},
"X-Strings": {"Hello", "World"},
Expand All @@ -19,6 +19,7 @@ func mockHeadersAndTrailers() IStep {
}
hit.Response().Header, hit.Response().Trailer = m, m
hit.Request().Header, hit.Request().Trailer = m, m
return nil
})
}

Expand Down
12 changes: 8 additions & 4 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func TestExpect_Custom(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body().String("Hello World"),
Expect().Custom(func(hit Hit) {
Expect().Custom(func(hit Hit) error {
require.Equal(t, "Hello World", hit.Response().Body().MustString())
return nil
}),
)
}
Expand All @@ -42,13 +43,16 @@ func TestExpect_DeepFunc(t *testing.T) {
Do(
Post(s.URL),
Send().Body().String("Hello World"),
Expect().Custom(func(h1 Hit) {
h1.MustDo(Expect().Custom(func(h2 Hit) {
h2.MustDo(Expect().Custom(func(h3 Hit) {
Expect().Custom(func(h1 Hit) error {
h1.MustDo(Expect().Custom(func(h2 Hit) error {
h2.MustDo(Expect().Custom(func(h3 Hit) error {
calledFunc = true
h3.MustDo(Expect().Body().String().Equal("Hello Universe"))
return nil
}))
return nil
}))
return nil
}),
),
PtrStr("not equal"), nil, nil, nil, nil, nil, nil,
Expand Down
12 changes: 7 additions & 5 deletions generators/clear/tests/clear_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func getDefaultValueRepresentation(t reflect.Type, isVariadic bool) string {
}
return fmt.Sprintf(`%s{1.000000, 2.000000}`, v.Type().String())
case hit.Callback:
return `func(hit Hit){}`
return `func(hit Hit)error{return nil}`
case []interface{}:
if isVariadic {
return `"Foo", "Baz"`
Expand Down Expand Up @@ -113,7 +113,7 @@ func getSampleValueRepresentation(t reflect.Type, isVariadic bool) string {
}
return fmt.Sprintf(`%s{3.000000, 4.000000}`, v.Type().String())
case hit.Callback:
return `func(hit Hit){panic("Err")}`
return `func(hit Hit)error{panic("Err")}`
case []interface{}:
if isVariadic {
return `"Hello", "Earth"`
Expand Down Expand Up @@ -411,21 +411,23 @@ func main() {

f.Op(`import . "github.com/Eun/go-hit"`)
f.Op(`import "github.com/stretchr/testify/require"`)
f.Op(`import "errors"`)

f.Comment("⚠️⚠️⚠️ This file was autogenerated by generators/clear/tests ⚠️⚠️⚠️ //")

// helper func

f.Op(`
func storeSteps(steps *[]IStep) IStep {
return Custom(CleanStep, func(hit Hit) {
return Custom(CleanStep, func(hit Hit) error {
*steps = hit.Steps()
return nil
})
}
func expectSteps(t *testing.T, expectSteps *[]IStep, removedStepsCount int) IStep {
return Custom(BeforeExpectStep, func(hit Hit) {
return Custom(BeforeExpectStep, func(hit Hit) error {
require.Len(t, hit.Steps(), len(*expectSteps)-removedStepsCount)
panic("TestOK")
return errors.New("TestOK")
})
}
`)
Expand Down
12 changes: 5 additions & 7 deletions hit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
)

// Callback will be used for Custom() functions.
type Callback func(hit Hit)
type Callback func(hit Hit) error

// Hit is the interface that will be passed in for Custom() steps.
type Hit interface {
Expand Down Expand Up @@ -93,13 +93,10 @@ type Hit interface {
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Custom(func(hit Hit) {
// err := hit.Do(
// Expect().Custom(func(hit Hit) error {
// return hit.Do(
// Expect().Status().Equal(http.StatusOK),
// )
// if err != nil {
// panic(err)
// }
// }),
// )
Do(steps ...IStep) error
Expand All @@ -109,10 +106,11 @@ type Hit interface {
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Custom(func(hit Hit) {
// Expect().Custom(func(hit Hit) error {
// hit.MustDo(
// Expect().Status().Equal(http.StatusOK),
// )
// return nil
// }),
// )
MustDo(steps ...IStep)
Expand Down
Loading

0 comments on commit d4b7f62

Please sign in to comment.