-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a572d2
commit 33e63a5
Showing
15 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,4 @@ linters: | |
- perfsprint | ||
- mnd | ||
- nilnil | ||
- testpackage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"code":12, | ||
"message":"Not Implemented", | ||
"details":[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.