1
1
package meilisearch
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"errors"
7
8
"github.com/stretchr/testify/require"
9
+ "io"
8
10
"net/http"
9
11
"net/http/httptest"
10
12
"strings"
@@ -29,9 +31,65 @@ func TestExecuteRequest(t *testing.T) {
29
31
if r .Method == http .MethodGet && r .URL .Path == "/test-get" {
30
32
w .WriteHeader (http .StatusOK )
31
33
_ , _ = w .Write ([]byte (`{"message":"get successful"}` ))
34
+ } else if r .Method == http .MethodGet && r .URL .Path == "/test-get-encoding" {
35
+ encode := r .Header .Get ("Accept-Encoding" )
36
+ if len (encode ) != 0 {
37
+ enc := newEncoding (ContentEncoding (encode ), DefaultCompression )
38
+ d := & mockData {Name : "foo" , Age : 30 }
39
+
40
+ b , err := json .Marshal (d )
41
+ require .NoError (t , err )
42
+
43
+ res , err := enc .Encode (bytes .NewReader (b ))
44
+ require .NoError (t , err )
45
+ _ , _ = w .Write (res .Bytes ())
46
+ w .WriteHeader (http .StatusOK )
47
+ return
48
+ }
49
+ _ , _ = w .Write ([]byte ("invalid message" ))
50
+ w .WriteHeader (http .StatusInternalServerError )
51
+ } else if r .Method == http .MethodPost && r .URL .Path == "/test-req-resp-encoding" {
52
+ accept := r .Header .Get ("Accept-Encoding" )
53
+ ce := r .Header .Get ("Content-Encoding" )
54
+
55
+ reqEnc := newEncoding (ContentEncoding (ce ), DefaultCompression )
56
+ respEnc := newEncoding (ContentEncoding (accept ), DefaultCompression )
57
+ req := new (mockData )
58
+
59
+ if len (ce ) != 0 {
60
+ b , err := io .ReadAll (r .Body )
61
+ require .NoError (t , err )
62
+
63
+ err = reqEnc .Decode (b , req )
64
+ require .NoError (t , err )
65
+ }
66
+
67
+ if len (accept ) != 0 {
68
+ d , err := json .Marshal (req )
69
+ require .NoError (t , err )
70
+ res , err := respEnc .Encode (bytes .NewReader (d ))
71
+ require .NoError (t , err )
72
+ _ , _ = w .Write (res .Bytes ())
73
+ w .WriteHeader (http .StatusOK )
74
+ }
32
75
} else if r .Method == http .MethodPost && r .URL .Path == "/test-post" {
33
76
w .WriteHeader (http .StatusCreated )
34
- _ , _ = w .Write ([]byte (`{"message":"post successful"}` ))
77
+ msg := []byte (`{"message":"post successful"}` )
78
+ _ , _ = w .Write (msg )
79
+
80
+ } else if r .Method == http .MethodPost && r .URL .Path == "/test-post-encoding" {
81
+ w .WriteHeader (http .StatusCreated )
82
+ msg := []byte (`{"message":"post successful"}` )
83
+
84
+ enc := r .Header .Get ("Accept-Encoding" )
85
+ if len (enc ) != 0 {
86
+ e := newEncoding (ContentEncoding (enc ), DefaultCompression )
87
+ b , err := e .Encode (bytes .NewReader (msg ))
88
+ require .NoError (t , err )
89
+ _ , _ = w .Write (b .Bytes ())
90
+ return
91
+ }
92
+ _ , _ = w .Write (msg )
35
93
} else if r .URL .Path == "/test-bad-request" {
36
94
w .WriteHeader (http .StatusBadRequest )
37
95
_ , _ = w .Write ([]byte (`{"message":"bad request"}` ))
@@ -47,13 +105,12 @@ func TestExecuteRequest(t *testing.T) {
47
105
}))
48
106
defer ts .Close ()
49
107
50
- client := newClient (& http.Client {}, ts .URL , "testApiKey" )
51
-
52
108
tests := []struct {
53
- name string
54
- internalReq * internalRequest
55
- expectedResp interface {}
56
- wantErr bool
109
+ name string
110
+ internalReq * internalRequest
111
+ expectedResp interface {}
112
+ contentEncoding ContentEncoding
113
+ wantErr bool
57
114
}{
58
115
{
59
116
name : "Successful GET request" ,
@@ -190,11 +247,108 @@ func TestExecuteRequest(t *testing.T) {
190
247
expectedResp : nil ,
191
248
wantErr : true ,
192
249
},
250
+ {
251
+ name : "Test request encoding gzip" ,
252
+ internalReq : & internalRequest {
253
+ endpoint : "/test-post-encoding" ,
254
+ method : http .MethodPost ,
255
+ withRequest : map [string ]string {"key" : "value" },
256
+ contentType : contentTypeJSON ,
257
+ withResponse : & mockResponse {},
258
+ acceptedStatusCodes : []int {http .StatusCreated },
259
+ },
260
+ expectedResp : & mockResponse {Message : "post successful" },
261
+ contentEncoding : GzipEncoding ,
262
+ wantErr : false ,
263
+ },
264
+ {
265
+ name : "Test request encoding deflate" ,
266
+ internalReq : & internalRequest {
267
+ endpoint : "/test-post-encoding" ,
268
+ method : http .MethodPost ,
269
+ withRequest : map [string ]string {"key" : "value" },
270
+ contentType : contentTypeJSON ,
271
+ withResponse : & mockResponse {},
272
+ acceptedStatusCodes : []int {http .StatusCreated },
273
+ },
274
+ expectedResp : & mockResponse {Message : "post successful" },
275
+ contentEncoding : DeflateEncoding ,
276
+ wantErr : false ,
277
+ },
278
+ {
279
+ name : "Test request encoding brotli" ,
280
+ internalReq : & internalRequest {
281
+ endpoint : "/test-post-encoding" ,
282
+ method : http .MethodPost ,
283
+ withRequest : map [string ]string {"key" : "value" },
284
+ contentType : contentTypeJSON ,
285
+ withResponse : & mockResponse {},
286
+ acceptedStatusCodes : []int {http .StatusCreated },
287
+ },
288
+ expectedResp : & mockResponse {Message : "post successful" },
289
+ contentEncoding : BrotliEncoding ,
290
+ wantErr : false ,
291
+ },
292
+ {
293
+ name : "Test response decoding gzip" ,
294
+ internalReq : & internalRequest {
295
+ endpoint : "/test-get-encoding" ,
296
+ method : http .MethodGet ,
297
+ withRequest : nil ,
298
+ withResponse : & mockData {},
299
+ acceptedStatusCodes : []int {http .StatusOK },
300
+ },
301
+ expectedResp : & mockData {Name : "foo" , Age : 30 },
302
+ contentEncoding : GzipEncoding ,
303
+ wantErr : false ,
304
+ },
305
+ {
306
+ name : "Test response decoding deflate" ,
307
+ internalReq : & internalRequest {
308
+ endpoint : "/test-get-encoding" ,
309
+ method : http .MethodGet ,
310
+ withRequest : nil ,
311
+ withResponse : & mockData {},
312
+ acceptedStatusCodes : []int {http .StatusOK },
313
+ },
314
+ expectedResp : & mockData {Name : "foo" , Age : 30 },
315
+ contentEncoding : DeflateEncoding ,
316
+ wantErr : false ,
317
+ },
318
+ {
319
+ name : "Test response decoding brotli" ,
320
+ internalReq : & internalRequest {
321
+ endpoint : "/test-get-encoding" ,
322
+ method : http .MethodGet ,
323
+ withRequest : nil ,
324
+ withResponse : & mockData {},
325
+ acceptedStatusCodes : []int {http .StatusOK },
326
+ },
327
+ expectedResp : & mockData {Name : "foo" , Age : 30 },
328
+ contentEncoding : BrotliEncoding ,
329
+ wantErr : false ,
330
+ },
331
+ {
332
+ name : "Test request and response encoding" ,
333
+ internalReq : & internalRequest {
334
+ endpoint : "/test-req-resp-encoding" ,
335
+ method : http .MethodPost ,
336
+ contentType : contentTypeJSON ,
337
+ withRequest : & mockData {Name : "foo" , Age : 30 },
338
+ withResponse : & mockData {},
339
+ acceptedStatusCodes : []int {http .StatusOK },
340
+ },
341
+ expectedResp : & mockData {Name : "foo" , Age : 30 },
342
+ contentEncoding : GzipEncoding ,
343
+ wantErr : false ,
344
+ },
193
345
}
194
346
195
347
for _ , tt := range tests {
196
348
t .Run (tt .name , func (t * testing.T ) {
197
- err := client .executeRequest (context .Background (), tt .internalReq )
349
+ c := newClient (& http.Client {}, ts .URL , "testApiKey" , tt .contentEncoding , DefaultCompression )
350
+
351
+ err := c .executeRequest (context .Background (), tt .internalReq )
198
352
if tt .wantErr {
199
353
require .Error (t , err )
200
354
} else {
0 commit comments