diff --git a/client_private_test.go b/client_private_test.go new file mode 100644 index 0000000..c02199b --- /dev/null +++ b/client_private_test.go @@ -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 "}, + "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", "") + 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", "") + 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", "") + _, 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", "") + _, 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", "") + _, err := client.sendRequest(context.Background(), http.MethodGet, "api/queries/1", "bad params", nil) + assert.ErrorContains(err, "query: Values() expects struct input. Got string") +} diff --git a/client_test.go b/client_test.go index 4912e75..cb125e4 100644 --- a/client_test.go +++ b/client_test.go @@ -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" ) @@ -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() @@ -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)) } @@ -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() @@ -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)) } @@ -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() @@ -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)) }