From debbe85597504ce6f9e93d419ec45ff2461ed602 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 7 Jul 2024 12:15:30 +0300 Subject: [PATCH] chore: add http client test --- assets/error.json | 5 ++ assets/fs.go | 17 +++++++ assets/fs_test.go | 19 +++++++ assets/invalid-json.json | 1 + go.mod | 1 + go.sum | 4 ++ pkg/http/http_test.go | 105 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 152 insertions(+) create mode 100644 assets/error.json create mode 100644 assets/fs.go create mode 100644 assets/fs_test.go create mode 100644 assets/invalid-json.json create mode 100644 pkg/http/http_test.go diff --git a/assets/error.json b/assets/error.json new file mode 100644 index 0000000..e364cfe --- /dev/null +++ b/assets/error.json @@ -0,0 +1,5 @@ +{ + "code":12, + "message":"Not Implemented", + "details":[] +} \ No newline at end of file diff --git a/assets/fs.go b/assets/fs.go new file mode 100644 index 0000000..9e47564 --- /dev/null +++ b/assets/fs.go @@ -0,0 +1,17 @@ +package assets + +import ( + "embed" +) + +//go:embed * +var EmbedFS embed.FS + +func GetBytesOrPanic(path string) []byte { + bytes, err := EmbedFS.ReadFile(path) + if err != nil { + panic(err) + } + + return bytes +} diff --git a/assets/fs_test.go b/assets/fs_test.go new file mode 100644 index 0000000..d745720 --- /dev/null +++ b/assets/fs_test.go @@ -0,0 +1,19 @@ +package assets + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGetPanicOrFailPanic(t *testing.T) { + t.Parallel() + + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + GetBytesOrPanic("not-existing") +} diff --git a/assets/invalid-json.json b/assets/invalid-json.json new file mode 100644 index 0000000..e466dcb --- /dev/null +++ b/assets/invalid-json.json @@ -0,0 +1 @@ +invalid \ No newline at end of file diff --git a/go.mod b/go.mod index bb93e08..8d3aefc 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/creasty/defaults v1.7.0 github.com/google/uuid v1.6.0 github.com/guregu/null/v5 v5.0.0 + github.com/jarcoal/httpmock v1.3.1 github.com/prometheus/client_golang v1.12.2 github.com/rs/zerolog v1.26.1 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index eed8d53..1866d25 100644 --- a/go.sum +++ b/go.sum @@ -152,6 +152,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= +github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -174,6 +176,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= +github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= diff --git a/pkg/http/http_test.go b/pkg/http/http_test.go new file mode 100644 index 0000000..c40333c --- /dev/null +++ b/pkg/http/http_test.go @@ -0,0 +1,105 @@ +package http + +import ( + "errors" + "main/assets" + "main/pkg/constants" + loggerPkg "main/pkg/logger" + "main/pkg/tracing" + "main/pkg/types" + "net/http" + "testing" + + "github.com/jarcoal/httpmock" + "github.com/stretchr/testify/require" +) + +func TestHttpClientErrorCreating(t *testing.T) { + t.Parallel() + + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := NewClient(*logger, "chain", tracer) + queryInfo, _, err := client.Get("://test", nil, types.HTTPPredicateAlwaysPass(), nil) + require.Error(t, err) + require.False(t, queryInfo.Success) +} + +//nolint:paralleltest // disabled due to httpmock usage +func TestHttpClientQueryFail(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder( + "GET", + "https://example.com", + httpmock.NewErrorResponder(errors.New("custom error")), + ) + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := NewClient(*logger, "chain", tracer) + + var response interface{} + _, _, err := client.Get("https://example.com", &response, types.HTTPPredicateCheckHeightAfter(100), nil) + require.Error(t, err) + require.ErrorContains(t, err, "custom error") +} + +//nolint:paralleltest // disabled due to httpmock usage +func TestHttpClientPredicateFail(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder( + "GET", + "https://example.com", + httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("error.json")).HeaderAdd(http.Header{ + constants.HeaderBlockHeight: []string{"1"}, + }), + ) + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := NewClient(*logger, "chain", tracer) + queryInfo, _, err := client.Get("https://example.com", nil, types.HTTPPredicateCheckHeightAfter(100), nil) + require.Error(t, err) + require.False(t, queryInfo.Success) +} + +//nolint:paralleltest // disabled due to httpmock usage +func TestHttpClientJsonParseFail(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder( + "GET", + "https://example.com", + httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("invalid-json.json")), + ) + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := NewClient(*logger, "chain", tracer) + var response interface{} + + _, _, err := client.Get("https://example.com", &response, types.HTTPPredicateAlwaysPass(), nil) + require.Error(t, err) + require.ErrorContains(t, err, "invalid character") +} + +//nolint:paralleltest // disabled due to httpmock usage +func TestHttpClientOk(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder( + "GET", + "https://example.com", + httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("error.json")), + ) + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := NewClient(*logger, "chain", tracer) + + var response interface{} + _, _, err := client.Get("https://example.com", &response, types.HTTPPredicateAlwaysPass(), nil) + require.NoError(t, err) +}