Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error tests #195

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions client_private_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package redash

import (
"context"
"io"
"math"
"net/http"
"testing"

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

func Test_sendRequest_OK(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/queries/1", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key <secret>"},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
assert.Equal("foo=bar", req.URL.Query().Encode())
return httpmock.NewStringResponse(http.StatusOK, `{"zoo":"baz"}`), nil
})

client, _ := NewClient("https://redash.example.com", "<secret>")
res, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", map[string]string{"foo": "bar"}, nil)
assert.NoError(err)
assert.Equal("200", res.Status)
require.NotNil(res.Body)
body, _ := io.ReadAll(res.Body)
assert.Equal(`{"zoo":"baz"}`, string(body))
}

func Test_sendRequest_Err_JoinPath(t *testing.T) {
assert := assert.New(t)
client, _ := NewClient("https://redash.example.com", "<secret>")
client.endpoint = "\b"
_, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", map[string]string{"foo": "bar"}, nil)
assert.ErrorContains(err, "parse \"\\b\": net/url: invalid control character in URL")
}

func Test_sendRequest_Err_Marshal(t *testing.T) {
assert := assert.New(t)
client, _ := NewClient("https://redash.example.com", "<secret>")
_, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", map[string]string{"foo": "bar"}, math.NaN())
assert.ErrorContains(err, "json: unsupported value: NaN")
}

func Test_sendRequest_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/queries/1", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := NewClient("https://redash.example.com", "<secret>")
_, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", map[string]string{"foo": "bar"}, nil)
assert.ErrorContains(err, "HTTP status code not OK: 503\nerror")
}

func Test_sendRequest_Err_params(t *testing.T) {
assert := assert.New(t)
client, _ := NewClient("https://redash.example.com", "<secret>")
_, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", "bad params", nil)
assert.ErrorContains(err, "query: Values() expects struct input. Got string")
}
16 changes: 7 additions & 9 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/redash-go/v2"
)

Expand Down Expand Up @@ -61,6 +62,7 @@ func Test_MustNewClientWithHTTPClient_Err(t *testing.T) {

func Test_Get_OK(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

Expand All @@ -84,9 +86,7 @@ func Test_Get_OK(t *testing.T) {
defer close()
assert.NoError(err)
assert.Equal("200", res.Status)
if res.Body == nil {
assert.FailNow("res.Body is nil")
}
require.NotNil(res.Body)
body, _ := io.ReadAll(res.Body)
assert.Equal(`{"zoo":"baz"}`, string(body))
}
Expand All @@ -102,6 +102,7 @@ func (t *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

func Test_Get_WithTransport(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

Expand Down Expand Up @@ -133,9 +134,7 @@ func Test_Get_WithTransport(t *testing.T) {
defer close()
assert.NoError(err)
assert.Equal("200", res.Status)
if res.Body == nil {
assert.FailNow("res.Body is nil")
}
require.NotNil(res.Body)
body, _ := io.ReadAll(res.Body)
assert.Equal(`{"zoo":"baz"}`, string(body))
}
Expand All @@ -157,6 +156,7 @@ func Test_Get_Err(t *testing.T) {

func Test_Post_OK(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

Expand Down Expand Up @@ -184,9 +184,7 @@ func Test_Post_OK(t *testing.T) {
defer close()
assert.NoError(err)
assert.Equal("200", res.Status)
if res.Body == nil {
assert.FailNow("res.Body is nil")
}
require.NotNil(res.Body)
body, _ := io.ReadAll(res.Body)
assert.Equal(`{"zoo":"baz"}`, string(body))
}
Expand Down