-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping_test.go
100 lines (84 loc) · 2.84 KB
/
ping_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package redash_test
import (
"context"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/redash-go/v2"
)
func Test_Ping_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/ping", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key " + testRedashAPIKey},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
return httpmock.NewStringResponse(http.StatusOK, `PONG.`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.Ping(context.Background())
assert.NoError(err)
}
func Test_Ping_Err(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/ping", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key " + testRedashAPIKey},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
return httpmock.NewStringResponse(http.StatusOK, `<html></html>`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.Ping(context.Background())
assert.ErrorContains(err, "invalid ping response: <html></html>")
}
func Test_Ping_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/ping", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, `error`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.Ping(context.Background())
assert.ErrorContains(err, "GET ping failed: HTTP status code not OK: 503\nerror")
}
func Test_Ping_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/ping", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.Ping(context.Background())
assert.ErrorContains(err, "IO error")
}
func Test_Ping_Acc(t *testing.T) {
if !testAcc {
t.Skip()
}
require := require.New(t)
// NOTE: No authentication required
client, _ := redash.NewClient(testRedashEndpoint, "")
err := client.Ping(context.Background())
require.NoError(err)
}