@@ -7,11 +7,15 @@ package influxdb2
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "log"
10
11
"net/http"
11
12
"net/http/httptest"
13
+ "runtime"
14
+ "strings"
12
15
"testing"
13
16
"time"
14
17
18
+ ihttp "github.com/influxdata/influxdb-client-go/v2/api/http"
15
19
"github.com/influxdata/influxdb-client-go/v2/domain"
16
20
http2 "github.com/influxdata/influxdb-client-go/v2/internal/http"
17
21
iwrite "github.com/influxdata/influxdb-client-go/v2/internal/write"
@@ -76,24 +80,76 @@ func TestWriteAPIManagement(t *testing.T) {
76
80
assert .Len (t , c .syncWriteAPIs , 0 )
77
81
}
78
82
83
+ func TestUserAgentBase (t * testing.T ) {
84
+ ua := fmt .Sprintf ("influxdb-client-go/%s (%s; %s)" , Version , runtime .GOOS , runtime .GOARCH )
85
+ assert .Equal (t , ua , http2 .UserAgentBase )
86
+
87
+ }
88
+
89
+ type doer struct {
90
+ userAgent string
91
+ doer ihttp.Doer
92
+ }
93
+
94
+ func (d * doer ) Do (req * http.Request ) (* http.Response , error ) {
95
+ req .Header .Set ("User-Agent" , d .userAgent )
96
+ return d .doer .Do (req )
97
+ }
98
+
79
99
func TestUserAgent (t * testing.T ) {
100
+ ua := http2 .UserAgentBase
80
101
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
81
102
<- time .After (100 * time .Millisecond )
82
- if r .Header .Get ("User-Agent" ) == http2 . UserAgent {
103
+ if r .Header .Get ("User-Agent" ) == ua {
83
104
w .WriteHeader (http .StatusNoContent )
84
105
} else {
85
106
w .WriteHeader (http .StatusNotFound )
86
107
}
87
108
}))
88
109
89
110
defer server .Close ()
111
+ var sb strings.Builder
112
+ log .SetOutput (& sb )
113
+ log .SetFlags (0 )
90
114
c := NewClient (server .URL , "x" )
115
+ assert .True (t , strings .Contains (sb .String (), "Application name is not set" ))
91
116
up , err := c .Ping (context .Background ())
92
117
require .NoError (t , err )
93
118
assert .True (t , up )
94
119
95
120
err = c .WriteAPIBlocking ("o" , "b" ).WriteRecord (context .Background (), "a,a=a a=1i" )
96
121
assert .NoError (t , err )
122
+
123
+ c .Close ()
124
+ sb .Reset ()
125
+ // Test setting application name
126
+ c = NewClientWithOptions (server .URL , "x" , DefaultOptions ().SetApplicationName ("Monitor/1.1" ))
127
+ ua = fmt .Sprintf ("influxdb-client-go/%s (%s; %s) Monitor/1.1" , Version , runtime .GOOS , runtime .GOARCH )
128
+ assert .False (t , strings .Contains (sb .String (), "Application name is not set" ))
129
+ up , err = c .Ping (context .Background ())
130
+ require .NoError (t , err )
131
+ assert .True (t , up )
132
+
133
+ err = c .WriteAPIBlocking ("o" , "b" ).WriteRecord (context .Background (), "a,a=a a=1i" )
134
+ assert .NoError (t , err )
135
+ c .Close ()
136
+
137
+ ua = "Monitor/1.1"
138
+ opts := DefaultOptions ()
139
+ opts .HTTPOptions ().SetHTTPDoer (& doer {
140
+ userAgent : ua ,
141
+ doer : http .DefaultClient ,
142
+ })
143
+
144
+ //Create client with custom user agent setter
145
+ c = NewClientWithOptions (server .URL , "x" , opts )
146
+ up , err = c .Ping (context .Background ())
147
+ require .NoError (t , err )
148
+ assert .True (t , up )
149
+
150
+ err = c .WriteAPIBlocking ("o" , "b" ).WriteRecord (context .Background (), "a,a=a a=1i" )
151
+ assert .NoError (t , err )
152
+ c .Close ()
97
153
}
98
154
99
155
func TestServerError429 (t * testing.T ) {
0 commit comments