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 12, 2024
1 parent 6a572d2 commit 33e63a5
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ jobs:
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ linters:
- perfsprint
- mnd
- nilnil
- testpackage
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":[]
}
9 changes: 9 additions & 0 deletions assets/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ import (

//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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

require (
cosmossdk.io/math v1.1.2
github.com/BurntSushi/toml v1.2.1
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.5
Expand All @@ -13,10 +14,11 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/gogo/protobuf v1.3.3
github.com/google/uuid v1.3.0
github.com/jarcoal/httpmock v1.3.1
github.com/prometheus/client_golang v1.14.0
github.com/rs/zerolog v1.30.0
github.com/spf13/cobra v1.7.0
github.com/superoo7/go-gecko v1.0.0
github.com/stretchr/testify v1.8.4
gopkg.in/guregu/null.v4 v4.0.0
gopkg.in/telebot.v3 v3.1.2
)
Expand All @@ -26,7 +28,6 @@ require (
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/math v1.1.2 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -107,7 +108,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
13 changes: 3 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIG
github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
github.com/google/orderedcode v0.0.1 h1:UzfcAexk9Vhv8+9pNOgRu41f16lHq725vPwnSeiG/Us=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190404155422-f8f10df84213/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
Expand Down Expand Up @@ -377,8 +376,6 @@ github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc=
github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/consul/api v1.12.0/go.mod h1:6pVBMo0ebnYdt2S3H87XhekM/HHrUoTD2XXb/VrZVy0=
github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOjagTIwIR1vPms=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -429,6 +426,8 @@ github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2t
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
Expand Down Expand Up @@ -491,6 +490,7 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM=
Expand All @@ -516,7 +516,6 @@ github.com/mtibben/percent v0.2.1 h1:5gssi8Nqo8QU/r2pynCm+hBQHpkB/uNK7BJCFogWdzs
github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ibNBTZrns=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand Down Expand Up @@ -651,8 +650,6 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/superoo7/go-gecko v1.0.0 h1:Xa1hZu2AYSA20eVMEd4etY0fcJoEI5deja1mdRmqlpI=
github.com/superoo7/go-gecko v1.0.0/go.mod h1:6AMYHL2wP2EN8AB9msPM76Lbo8L/MQOknYjvak5coaY=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
Expand Down Expand Up @@ -693,7 +690,6 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -1200,8 +1196,6 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/guregu/null.v4 v4.0.0 h1:1Wm3S1WEA2I26Kq+6vcW+w0gcDo44YKYD7YIEJNHDjg=
gopkg.in/guregu/null.v4 v4.0.0/go.mod h1:YoQhUrADuG3i9WqesrCmpNRwm1ypAgSHYqoOcTu/JrI=
gopkg.in/h2non/gock.v1 v1.0.14 h1:fTeu9fcUvSnLNacYvYI54h+1/XEteDyHvrVCZEEEYNM=
gopkg.in/h2non/gock.v1 v1.0.14/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/telebot.v3 v3.1.2 h1:uw3zobPBnexytTsIPyxsS10xHRLXCf5f2GQhBxp6NaU=
Expand Down Expand Up @@ -1234,7 +1228,6 @@ nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
pgregory.net/rapid v0.5.5 h1:jkgx1TjbQPD/feRoK+S/mXw9e1uj6WilpHrXJowi6oA=
pgregory.net/rapid v0.5.5/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
Expand Down
6 changes: 6 additions & 0 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"main/pkg/types"
"strings"

abciTypes "github.com/cometbft/cometbft/abci/types"

"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cometbft/cometbft/libs/json"
coreTypes "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -111,6 +113,10 @@ func (c *Converter) ParseEvent(event jsonRpcTypes.RPCResponse, nodeURL string) t
Str("node", nodeURL).
Msg("Got transaction")

return c.ParseTx(txProto, txResult, txHash)
}

func (c *Converter) ParseTx(txProto tx.Tx, txResult abciTypes.TxResult, txHash string) *types.Tx {
txMessages := []types.Message{}

for _, message := range txProto.GetBody().Messages {
Expand Down
40 changes: 40 additions & 0 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"main/pkg/types"
"testing"

abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/types/tx"

jsonRpcTypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
cosmosAuthzTypes "github.com/cosmos/cosmos-sdk/x/authz"
Expand Down Expand Up @@ -86,6 +89,21 @@ func TestConverterErrorUnmarshal(t *testing.T) {
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("{\"data\":{\"type\":\"tendermint/event/Tx\",\"value\":{\"TxResult\":{\"height\":\"1\",\"index\":9,\"tx\":\"cmFuZG9tYnl0ZXMK\",\"result\":{\"data\":\"CisKKS9zZW50aW5lbC5ub2RlLnYyLk1zZ1VwZGF0ZURldGFpbHNSZXF1ZXN0\",\"log\":\"\",\"gas_wanted\":\"106365\",\"gas_used\":\"102726\",\"events\":[]}}}},\"events\":{}}"),
}
result := converter.ParseEvent(event, "example")
require.NotNil(t, result)
require.IsType(t, &types.TxError{}, result)
}

func TestConverterOkUnmarshal(t *testing.T) {
t.Parallel()

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("{\"data\":{\"type\":\"tendermint/event/Tx\",\"value\":{\"TxResult\":{\"height\":\"1\",\"index\":9,\"tx\":\"CmEKXwooL3NlbnRpbmVsLm5vZGUudjIuTXNnVXBkYXRlU3RhdHVzUmVxdWVzdBIzCi9zZW50bm9kZTFmdGNycnU0MDdmbGdhZTB0cm4wbjRja2RtY2w1aDZsZ3V4ZHIydBABEmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQIgzmHYcht/wBxkUOilsMRa2qUxPhHn8smOT1eFQBxlmRIECgIIARheEhMKDQoFdWR2cG4SBDk1MjYQk+gFGkB20FDj4l1Btj7avEltQAB3KH63PHg+52nXfcshadIwZmDErlv5dzF1Jz/d2NIs4gRj/5/twPFCabAffMlLsYlm\",\"result\":{\"data\":\"CisKKS9zZW50aW5lbC5ub2RlLnYyLk1zZ1VwZGF0ZURldGFpbHNSZXF1ZXN0\",\"log\":\"\",\"gas_wanted\":\"106365\",\"gas_used\":\"102726\",\"events\":[]}}}},\"events\":{}}"),
}
Expand Down Expand Up @@ -175,3 +193,25 @@ func TestConverterParsedInternal(t *testing.T) {
require.Len(t, result.GetParsedMessages(), 1)
require.IsType(t, &messages.MsgSend{}, result.GetParsedMessages()[0])
}

func TestConverterAllMessageSkipped(t *testing.T) {
t.Parallel()

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

txProto := tx.Tx{
Body: &tx.TxBody{Messages: []*codecTypes.Any{}},
}

txResult := abciTypes.TxResult{
Height: 123,
Index: 0,
Tx: nil,
Result: abciTypes.ResponseDeliverTx{},
}

result := converter.ParseTx(txProto, txResult, "hash")
require.Nil(t, result)
}
16 changes: 15 additions & 1 deletion pkg/filterer/filterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ func TestFilterReportableTxNodeConnectError(t *testing.T) {
}))
}

func TestFilterReportableNotSupported(t *testing.T) {
t.Parallel()

config := &configPkg.AppConfig{}
logger := loggerPkg.GetDefaultLogger()
metricsManager := metrics.NewManager(logger, configPkg.MetricsConfig{Enabled: false})
filterer := filtererPkg.NewFilterer(logger, config, metricsManager)
chain := &configTypes.Chain{Name: "chain"}

reportable := &types.UnsupportedReportable{}
require.Nil(t, filterer.FilterForChainAndSubscription(reportable, chain, &configTypes.ChainSubscription{
Chain: "chain",
}))
}

func TestFilterReportableTxFailed(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -336,7 +351,6 @@ func TestFilterReportableTxAllMessagesFiltered(t *testing.T) {
}
require.Nil(t, filterer.FilterForChainAndSubscription(reportable, chain, subscription))
}

func TestFilterReportableTxInvalidHeight(t *testing.T) {
t.Parallel()

Expand Down
102 changes: 102 additions & 0 deletions pkg/http/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package http

import (
"errors"
"main/assets"
loggerPkg "main/pkg/logger"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
)

func TestHttpClientErrorCreating(t *testing.T) {
t.Parallel()

logger := loggerPkg.GetNopLogger()
client := NewClient(logger, "", "chain")
err, queryInfo := client.Get("://test", 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()
client := NewClient(logger, "https://example.com", "chain")

var response interface{}
err, queryInfo := client.Get("/", &response)
require.Error(t, err)
require.ErrorContains(t, err, "custom error")
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()
client := NewClient(logger, "https://example.com", "chain")
var response interface{}

err, queryInfo := client.Get("/", &response)
require.Error(t, err)
require.ErrorContains(t, err, "invalid character")
require.False(t, queryInfo.Success)
}

//nolint:paralleltest // disabled due to httpmock usage
func TestHttpClientBadHttpCode(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(
"GET",
"https://example.com/",
httpmock.NewBytesResponder(500, assets.GetBytesOrPanic("error.json")),
)
logger := loggerPkg.GetNopLogger()
client := NewClient(logger, "https://example.com", "chain")

var response interface{}
err, queryInfo := client.Get("/", &response)
require.Error(t, err)
require.ErrorContains(t, err, "bad HTTP code")
require.False(t, queryInfo.Success)
}

//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()
client := NewClient(logger, "https://example.com", "chain")

var response interface{}
err, queryInfo := client.GetWithHeaders("/", &response, map[string]string{
"User-Agent": "custom",
})
require.NoError(t, err)
require.True(t, queryInfo.Success)
}
6 changes: 6 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (

func GetDefaultLogger() *zerolog.Logger {
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
zerolog.SetGlobalLevel(zerolog.TraceLevel)
return &log
}

func GetNopLogger() *zerolog.Logger {
log := zerolog.Nop()
return &log
}

Expand Down
Loading

0 comments on commit 33e63a5

Please sign in to comment.