forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
74 lines (65 loc) · 2.28 KB
/
client_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
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"context"
http2 "github.com/influxdata/influxdb-client-go/internal/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestUserAgent(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
if r.Header.Get("User-Agent") == http2.UserAgent {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
c := NewClient(server.URL, "x")
ready, err := c.Ready(context.Background())
assert.True(t, ready)
assert.Nil(t, err)
err = c.WriteApiBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
assert.Nil(t, err)
}
func TestServerError429(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
w.Header().Set("Retry-After", "1")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{"code":"too many requests", "message":"exceeded rate limit"}`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
err := c.WriteApiBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.NotNil(t, err)
perror, ok := err.(*http2.Error)
require.True(t, ok)
require.NotNil(t, perror)
assert.Equal(t, "too many requests", perror.Code)
assert.Equal(t, "exceeded rate limit", perror.Message)
}
func TestServerErrorNonJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`internal server error`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
err := c.WriteApiBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.NotNil(t, err)
perror, ok := err.(*http2.Error)
require.True(t, ok)
require.NotNil(t, perror)
assert.Equal(t, "500 Internal Server Error", perror.Code)
assert.Equal(t, "internal server error", perror.Message)
}