Skip to content

Commit

Permalink
Merge pull request #195 from winebarrel/add_error_tests
Browse files Browse the repository at this point in the history
Add error tests
  • Loading branch information
winebarrel authored Jun 14, 2024
2 parents 11d5375 + 6ab0460 commit bda48fb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
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

0 comments on commit bda48fb

Please sign in to comment.