Skip to content

Commit

Permalink
chore: add http client test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 7, 2024
1 parent 1e14e5f commit debbe85
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions assets/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"code":12,
"message":"Not Implemented",
"details":[]
}
17 changes: 17 additions & 0 deletions assets/fs.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions assets/fs_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 1 addition & 0 deletions assets/invalid-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
105 changes: 105 additions & 0 deletions pkg/http/http_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit debbe85

Please sign in to comment.