diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d651793f..4600f42a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/README.md b/README.md index f25d3044..b4bd081b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 }), ) ``` diff --git a/clear_gen_test.go b/clear_gen_test.go index 703741a6..709131a1 100644 --- a/clear_gen_test.go +++ b/clear_gen_test.go @@ -3,6 +3,8 @@ package hit_test import ( "testing" + "errors" + . "github.com/Eun/go-hit" "github.com/stretchr/testify/require" ) @@ -10,14 +12,15 @@ import ( // ⚠️⚠️⚠️ 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") }) } diff --git a/debug_test.go b/debug_test.go index 018da283..15d4ee64 100644 --- a/debug_test.go +++ b/debug_test.go @@ -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 }), ) diff --git a/errortrace/errortrace.go b/errortrace/errortrace.go index 80cd16ed..5ee75632 100644 --- a/errortrace/errortrace.go +++ b/errortrace/errortrace.go @@ -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 +} diff --git a/examples/hash/example_hash_test.go b/examples/hash/example_hash_test.go index bf1ca5e9..88d235f1 100644 --- a/examples/hash/example_hash_test.go +++ b/examples/hash/example_hash_test.go @@ -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 } } diff --git a/examples/simple/simple_test.go b/examples/simple/simple_test.go index c8f78f97..631bba8d 100644 --- a/examples/simple/simple_test.go +++ b/examples/simple/simple_test.go @@ -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) { @@ -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 }), ) } diff --git a/expect.go b/expect.go index 275ccadf..8100a766 100644 --- a/expect.go +++ b/expect.go @@ -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 @@ -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) }, } } diff --git a/expect_body_json_test.go b/expect_body_json_test.go index ffecb2d5..b1013ad5 100644 --- a/expect_body_json_test.go +++ b/expect_body_json_test.go @@ -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, @@ -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"`), diff --git a/expect_formvalues_test.go b/expect_formvalues_test.go index c3f335e4..604879f2 100644 --- a/expect_formvalues_test.go +++ b/expect_formvalues_test.go @@ -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"}, @@ -19,6 +19,7 @@ func mockFormValues() IStep { }.Encode() hit.Request().Body().SetString(s) hit.Response().Body().SetString(s) + return nil }) } diff --git a/expect_header_trailer_test.go b/expect_header_trailer_test.go index 959e7627..89f7af24 100644 --- a/expect_header_trailer_test.go +++ b/expect_header_trailer_test.go @@ -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"}, @@ -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 }) } diff --git a/expect_test.go b/expect_test.go index 51525389..0948e8fd 100644 --- a/expect_test.go +++ b/expect_test.go @@ -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 }), ) } @@ -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, diff --git a/generators/clear/tests/clear_tests.go b/generators/clear/tests/clear_tests.go index 688d24e0..fe49effa 100644 --- a/generators/clear/tests/clear_tests.go +++ b/generators/clear/tests/clear_tests.go @@ -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"` @@ -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"` @@ -411,6 +411,7 @@ 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 ⚠️⚠️⚠️ //") @@ -418,14 +419,15 @@ func main() { 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") }) } `) diff --git a/hit.go b/hit.go index 2a0cee4a..f36bfa2d 100644 --- a/hit.go +++ b/hit.go @@ -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 { @@ -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 @@ -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) diff --git a/hit_test.go b/hit_test.go index 4c1ce215..19d7c971 100644 --- a/hit_test.go +++ b/hit_test.go @@ -4,17 +4,21 @@ import ( "bytes" "context" "crypto/rand" + "errors" "fmt" "io" "io/ioutil" "net/http" "net/http/httptest" "net/url" + "os" "strconv" "strings" "testing" "time" + "golang.org/x/xerrors" + "github.com/lunixbochs/vtclean" "github.com/stretchr/testify/require" @@ -38,14 +42,15 @@ func TestRequest(t *testing.T) { t.Run("overwrite request during send", func(t *testing.T) { Test(t, Get(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, s.URL, nil) if err != nil { - panic(err) + return err } if err := hit.SetRequest(r); err != nil { - panic(err) + return err } + return nil }), Send().Body().String("Hello Universe"), Expect().Body().String().Equal("Hello Universe"), @@ -122,17 +127,19 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"Foo"}, hit.Request().Header["X-Header"]) require.Equal(t, []string{"Bar"}, hit.Request().Trailer["X-Trailer"]) + return nil }), ) Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"Foo"}, hit.Request().Header["X-Header"]) require.Equal(t, []string{"Bar"}, hit.Request().Trailer["X-Trailer"]) + return nil }), ) }) @@ -156,17 +163,19 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"1", "2", "banana"}, hit.Request().Form["a"]) require.Equal(t, []string{"1", "2", "banana"}, hit.Request().PostForm["a"]) + return nil }), ) Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"1", "2", "banana"}, hit.Request().Form["a"]) require.Equal(t, []string{"1", "2", "banana"}, hit.Request().PostForm["a"]) + return nil }), ) }) @@ -189,8 +198,9 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"bar"}, hit.Request().MultipartForm.Value["foo"]) + return nil }), ) @@ -198,8 +208,9 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, "file1", hit.Request().MultipartForm.File["file1"][0].Filename) + return nil }), ) f, err := req.MultipartForm.File["file1"][0].Open() @@ -212,7 +223,7 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Len(t, hit.Request().MultipartForm.File["file1"], 1) require.Equal(t, "file1", hit.Request().MultipartForm.File["file1"][0].Filename) f, err = hit.Request().MultipartForm.File["file1"][0].Open() @@ -221,6 +232,7 @@ func TestMultiUse(t *testing.T) { buf, err = ioutil.ReadAll(f) require.NoError(t, err) require.Equal(t, "baz", string(buf)) + return nil }), ) }) @@ -239,8 +251,9 @@ func TestMultiUse(t *testing.T) { Test(t, Request(req), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, []string{"a", "b"}, hit.Request().TransferEncoding) + return nil }), ) }) @@ -307,8 +320,9 @@ func TestHTTPClient(t *testing.T) { Test(t, Post(s.URL), HTTPClient(client), - Custom(AfterSendStep, func(hit Hit) { + Custom(AfterSendStep, func(hit Hit) error { require.Equal(t, client, hit.HTTPClient()) + return nil }), ) } @@ -340,8 +354,9 @@ func TestCombineSteps_DoubleExecution(t *testing.T) { t, Post(s.URL), CombineSteps( - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { calls++ + return nil }), ), ) @@ -353,8 +368,9 @@ func TestCombineSteps_DoubleExecution(t *testing.T) { t, Post(s.URL), CombineSteps( - Expect().Custom(func(hit Hit) { + Expect().Custom(func(hit Hit) error { calls++ + return nil }), ), ) @@ -366,8 +382,9 @@ func TestCombineSteps_DoubleExecution(t *testing.T) { t, Post(s.URL), CombineSteps( - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { calls++ + return nil }), ), ) @@ -381,10 +398,11 @@ func TestDescription(t *testing.T) { err := Do( Description("Test #1"), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "Test #1", hit.Description()) hit.SetDescription("Test #A") require.Equal(t, "Test #A", hit.Description()) + return nil }), Post(s.URL), Send().Body().String("Hello"), @@ -398,15 +416,56 @@ func TestCustomError(t *testing.T) { s := EchoServer() defer s.Close() - ExpectError(t, - Do( + t.Run("panic", func(t *testing.T) { + ExpectError(t, + Do( + Post(s.URL), + Send().Custom(func(hit Hit) error { + panic("some error") + }), + ), + PtrStr("some error"), + ) + }) + + t.Run("error", func(t *testing.T) { + ExpectError(t, + Do( + Post(s.URL), + Send().Custom(func(hit Hit) error { + return errors.New("some error") + }), + ), + PtrStr("some error"), + ) + }) + + t.Run("errors is", func(t *testing.T) { + myError := errors.New("some error") + err := Do( Post(s.URL), - Send().Custom(func(hit Hit) { - panic("some error") + Send().Custom(func(hit Hit) error { + return myError }), - ), - PtrStr("some error"), - ) + ) + require.True(t, xerrors.Is(err, myError)) + }) + + t.Run("errors as", func(t *testing.T) { + err := Do( + Post(s.URL), + Send().Custom(func(hit Hit) error { + return &os.SyscallError{ + Syscall: "test", + Err: xerrors.New("go away"), + } + }), + ) + var sysCallErr *os.SyscallError + require.True(t, xerrors.As(err, &sysCallErr)) + require.Equal(t, sysCallErr.Syscall, "test") + require.Equal(t, sysCallErr.Err.Error(), "go away") + }) } func TestDo(t *testing.T) { @@ -419,11 +478,12 @@ func TestDo(t *testing.T) { Do( Post(s.URL), Send().Body().String("Hello World"), - Custom(ExpectStep, func(hit Hit) { + Custom(ExpectStep, func(hit Hit) error { hit.MustDo( Expect().Body().String().Equal("Hello Universe"), ) shouldNotRun = true + return nil }), ), PtrStr("not equal"), nil, nil, nil, nil, nil, nil, @@ -437,11 +497,12 @@ func TestDo(t *testing.T) { Do( Post(s.URL), Send().Body().String("Hello World"), - Custom(SendStep, func(hit Hit) { + Custom(SendStep, func(hit Hit) error { hit.MustDo( Expect().Body().String().Equal("Hello Universe"), ) shouldNotRun = true + return nil }), ), PtrStr(`unable to execute Expect().Body().String().Equal("Hello Universe") during SendStep, can only be run during ExpectStep`), @@ -457,11 +518,13 @@ func TestOutOfContext(t *testing.T) { Post(s.URL), Send().Body().String("World"), Expect().Body().String().Equal("World"), - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { Send().Body().String("Hello Universe") // this will never be run, because you need to wrap this with hit.Do()/MustDo() + return nil }), - Expect().Custom(func(hit Hit) { + Expect().Custom(func(hit Hit) error { Expect().Body().String().Equal("Hello Universe") // this will never be run, because you need to wrap this with hit.Do()/MustDo() + return nil }), ) } @@ -472,16 +535,19 @@ func TestAddSteps(t *testing.T) { var callOrder []int Test(t, Post(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 1) hit.AddSteps( - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 2) + return nil }), ) + return nil }), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 3) + return nil }), ) @@ -494,16 +560,19 @@ func TestInsertSteps(t *testing.T) { var callOrder []int Test(t, Post(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 1) hit.InsertSteps( - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 2) + return nil }), ) + return nil }), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 3) + return nil }), ) @@ -514,21 +583,24 @@ func TestAddAndRemoveSteps(t *testing.T) { s := EchoServer() defer s.Close() var callOrder []int - someStep := Custom(BeforeSendStep, func(hit Hit) {}) + someStep := Custom(BeforeSendStep, func(hit Hit) error { return nil }) Test(t, Post(s.URL), someStep, - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 1) hit.AddSteps( - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 2) + return nil }), ) hit.RemoveSteps(someStep) + return nil }), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { callOrder = append(callOrder, 3) + return nil }), ) @@ -573,8 +645,9 @@ func TestReturn(t *testing.T) { Test(t, Post(s.URL), Send().Body().String("Hello World"), - Expect().Custom(func(hit Hit) { + Expect().Custom(func(hit Hit) error { executed = true + return nil }), Expect().Body().String().Equal("Hello World"), Return(), diff --git a/method_test.go b/method_test.go index 223d52e8..2b9a5743 100644 --- a/method_test.go +++ b/method_test.go @@ -14,8 +14,9 @@ func TestMethod(t *testing.T) { t.Run("Connect", func(t *testing.T) { Test(t, Connect(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "CONNECT", hit.Request().Method) + return nil }), ) }) @@ -23,8 +24,9 @@ func TestMethod(t *testing.T) { t.Run("Delete", func(t *testing.T) { Test(t, Delete(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "DELETE", hit.Request().Method) + return nil }), ) }) @@ -32,8 +34,9 @@ func TestMethod(t *testing.T) { t.Run("Get", func(t *testing.T) { Test(t, Get(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "GET", hit.Request().Method) + return nil }), ) }) @@ -41,8 +44,9 @@ func TestMethod(t *testing.T) { t.Run("Head", func(t *testing.T) { Test(t, Head(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "HEAD", hit.Request().Method) + return nil }), ) }) @@ -50,8 +54,9 @@ func TestMethod(t *testing.T) { t.Run("Post", func(t *testing.T) { Test(t, Post(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "POST", hit.Request().Method) + return nil }), ) }) @@ -59,8 +64,9 @@ func TestMethod(t *testing.T) { t.Run("Options", func(t *testing.T) { Test(t, Options(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "OPTIONS", hit.Request().Method) + return nil }), ) }) @@ -68,8 +74,9 @@ func TestMethod(t *testing.T) { t.Run("Trace", func(t *testing.T) { Test(t, Trace(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "TRACE", hit.Request().Method) + return nil }), ) }) @@ -77,8 +84,9 @@ func TestMethod(t *testing.T) { t.Run("Put", func(t *testing.T) { Test(t, Put(s.URL), - Custom(BeforeSendStep, func(hit Hit) { + Custom(BeforeSendStep, func(hit Hit) error { require.Equal(t, "PUT", hit.Request().Method) + return nil }), ) }) diff --git a/send.go b/send.go index fb849e27..90e6aaa7 100644 --- a/send.go +++ b/send.go @@ -50,8 +50,9 @@ type ISend interface { // Example: // MustDo( // Get("https://example.com"), - // Send().Custom(func(hit Hit) { + // Send().Custom(func(hit Hit) error { // hit.Request().Body().SetString("Hello World") + // return nil // }), // ) Custom(fn Callback) IStep @@ -95,8 +96,7 @@ func (snd *send) Custom(fn Callback) IStep { When: SendStep, CallPath: snd.cleanPath.Push("Custom", []interface{}{fn}), Exec: func(hit *hitImpl) error { - fn(hit) - return nil + return fn(hit) }, } } diff --git a/send_body_test.go b/send_body_test.go index 437a1a57..be879432 100644 --- a/send_body_test.go +++ b/send_body_test.go @@ -89,8 +89,9 @@ func TestSendBody_ModifyPreviousBody(t *testing.T) { Test(t, Post(s.URL), Send().Body().String("Hello"), - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { hit.Request().Body().SetString(fmt.Sprintf("%s World", hit.Request().Body().MustString())) + return nil }), Expect().Body().String().Equal("Hello World"), ) @@ -102,8 +103,9 @@ func TestSendBody_EmptyBody(t *testing.T) { Test(t, Get(s.URL), - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { require.Empty(t, hit.Request().Body().MustString()) + return nil }), ) } diff --git a/send_test.go b/send_test.go index c00cd5ef..755c3087 100644 --- a/send_test.go +++ b/send_test.go @@ -16,9 +16,10 @@ func TestSend_Custom(t *testing.T) { calledFunc := false Test(t, Post(s.URL), - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { calledFunc = true hit.Request().Body().SetString("Hello World") + return nil }), Expect().Body().String().Equal(`Hello World`), ) @@ -28,9 +29,10 @@ func TestSend_Custom(t *testing.T) { calledFunc := false Test(t, Post(s.URL), - Send().Custom(func(hit Hit) { + Send().Custom(func(hit Hit) error { calledFunc = true hit.MustDo(Send().Body().String("Hello World")) + return nil }), Expect().Body().String().Equal(`Hello World`), ) diff --git a/static.go b/static.go index b7c0c0b1..98108eec 100644 --- a/static.go +++ b/static.go @@ -503,10 +503,11 @@ func Clear() IClear { // Example: // MustDo( // Get("https://example.com"), -// Custom(ExpectStep, func(hit Hit) { +// Custom(ExpectStep, func(hit Hit) error { // if hit.Response().Body().MustString() != "Hello World" { -// panic("Expected Hello World") +// return errors.New("Expected Hello World") // } +// return nil // }), // ) func Custom(when StepTime, exec Callback) IStep { @@ -515,8 +516,7 @@ func Custom(when StepTime, exec Callback) IStep { When: when, CallPath: newCallPath("Custom", []interface{}{when, exec}), Exec: func(hit *hitImpl) error { - exec(hit) - return nil + return exec(hit) }, } } diff --git a/store_request_test.go b/store_request_test.go index 76c0818a..3c062042 100644 --- a/store_request_test.go +++ b/store_request_test.go @@ -240,8 +240,9 @@ func TestStoreRequest_TransferEncoding(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.Request().TransferEncoding = []string{"gzip", "chunked"} + return nil }), Store().Request().TransferEncoding().In(&transferEncoding), ) diff --git a/store_response_test.go b/store_response_test.go index 9ebafff2..858a7bb6 100644 --- a/store_response_test.go +++ b/store_response_test.go @@ -99,8 +99,9 @@ func TestStoreResponse_TransferEncoding(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.Response().TransferEncoding = []string{"gzip", "chunked"} + return nil }), Store().Response().TransferEncoding().In(&transferEncoding), )