@@ -22,6 +22,17 @@ import (
2222 "github.com/stretchr/testify/require"
2323)
2424
25+ // customJSONCodec is a custom JSON codec for testing custom codec functionality.
26+ type customJSONCodec struct {}
27+
28+ func (customJSONCodec ) NewEncoder (w io.Writer ) JSONEncoder {
29+ return json .NewEncoder (w )
30+ }
31+
32+ func (customJSONCodec ) NewDecoder (r io.Reader ) JSONDecoder {
33+ return json .NewDecoder (r )
34+ }
35+
2536func Test_buildRequest (t * testing.T ) {
2637 t .Parallel ()
2738
@@ -189,6 +200,7 @@ func Test_NewClient(t *testing.T) {
189200 Timeout : 600 * time .Second ,
190201 },
191202 userAgent : defaultUserAgent (),
203+ jsonCodec : defaultJSONCodec (),
192204 },
193205 },
194206 "succeeds with valid client from env" : {
@@ -203,6 +215,7 @@ func Test_NewClient(t *testing.T) {
203215 Timeout : 600 * time .Second ,
204216 },
205217 userAgent : defaultUserAgent (),
218+ jsonCodec : defaultJSONCodec (),
206219 },
207220 },
208221 "succeeds with valid client from env and config" : {
@@ -225,6 +238,7 @@ func Test_NewClient(t *testing.T) {
225238 Timeout : 500 * time .Second ,
226239 },
227240 userAgent : "bob" ,
241+ jsonCodec : defaultJSONCodec (),
228242 },
229243 },
230244 "succeeds with config, overrides env" : {
@@ -245,6 +259,7 @@ func Test_NewClient(t *testing.T) {
245259 Timeout : 600 * time .Second ,
246260 },
247261 userAgent : defaultUserAgent (),
262+ jsonCodec : defaultJSONCodec (),
248263 },
249264 },
250265 "succeeds with profile" : {
@@ -261,6 +276,7 @@ func Test_NewClient(t *testing.T) {
261276 Timeout : 600 * time .Second ,
262277 },
263278 userAgent : defaultUserAgent (),
279+ jsonCodec : defaultJSONCodec (),
264280 },
265281 },
266282 "succeeds with profile from env" : {
@@ -278,6 +294,7 @@ func Test_NewClient(t *testing.T) {
278294 Timeout : 600 * time .Second ,
279295 },
280296 userAgent : defaultUserAgent (),
297+ jsonCodec : defaultJSONCodec (),
281298 },
282299 },
283300 "succeeds with default profile" : {
@@ -294,6 +311,7 @@ func Test_NewClient(t *testing.T) {
294311 Timeout : 600 * time .Second ,
295312 },
296313 userAgent : defaultUserAgent (),
314+ jsonCodec : defaultJSONCodec (),
297315 },
298316 },
299317 "succeeds with config dir and default profile" : {
@@ -310,6 +328,7 @@ func Test_NewClient(t *testing.T) {
310328 Timeout : 600 * time .Second ,
311329 },
312330 userAgent : defaultUserAgent (),
331+ jsonCodec : defaultJSONCodec (),
313332 },
314333 },
315334 "succeeds with config dir and profile" : {
@@ -326,6 +345,7 @@ func Test_NewClient(t *testing.T) {
326345 Timeout : 600 * time .Second ,
327346 },
328347 userAgent : defaultUserAgent (),
348+ jsonCodec : defaultJSONCodec (),
329349 },
330350 },
331351 "succeeds with profile, overrides env" : {
@@ -346,6 +366,7 @@ func Test_NewClient(t *testing.T) {
346366 Timeout : 600 * time .Second ,
347367 },
348368 userAgent : defaultUserAgent (),
369+ jsonCodec : defaultJSONCodec (),
349370 },
350371 },
351372 "succeeds with host and token from different sources " : {
@@ -364,6 +385,25 @@ func Test_NewClient(t *testing.T) {
364385 Timeout : 600 * time .Second ,
365386 },
366387 userAgent : defaultUserAgent (),
388+ jsonCodec : defaultJSONCodec (),
389+ },
390+ },
391+ "succeeds with custom json codec" : {
392+ config : func (string ) * Config {
393+ return & Config {
394+ Host : "http://localhost" ,
395+ Token : "foo" ,
396+ JSONCodec : customJSONCodec {},
397+ }
398+ },
399+ expectedClient : & Client {
400+ host : "http://localhost/" ,
401+ token : "foo" ,
402+ client : & http.Client {
403+ Timeout : 600 * time .Second ,
404+ },
405+ userAgent : defaultUserAgent (),
406+ jsonCodec : customJSONCodec {},
367407 },
368408 },
369409 "fails with missing address using config" : {
0 commit comments