Skip to content

Commit

Permalink
Merge pull request #159 from kaleido-io/gzip-test
Browse files Browse the repository at this point in the history
adding test to ensure ffresty client handles gzip by default
  • Loading branch information
EnriqueL8 authored Dec 18, 2024
2 parents 4be8d55 + da50f7a commit 0caf4ee
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/ffresty/ffresty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package ffresty

import (
"bytes"
"compress/gzip"
"context"
"crypto/rand"
"crypto/rsa"
Expand All @@ -27,6 +29,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"math/big"
"net"
Expand Down Expand Up @@ -91,6 +94,51 @@ func TestRequestOK(t *testing.T) {
assert.Equal(t, 1, httpmock.GetTotalCallCount())
}

func TestRequestOKForGzip(t *testing.T) {

customClient := &http.Client{}

resetConf()
utConf.Set(HTTPConfigURL, "http://localhost:12345")
utConf.Set(HTTPConfigHeaders, map[string]interface{}{
"someheader": "headervalue",
})
utConf.Set(HTTPConfigAuthUsername, "user")
utConf.Set(HTTPConfigAuthPassword, "pass")
utConf.Set(HTTPConfigRetryEnabled, true)
utConf.Set(HTTPCustomClient, customClient)

c, err := New(context.Background(), utConf)
assert.Nil(t, err)
httpmock.ActivateNonDefault(customClient)
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", "http://localhost:12345/test",
func(req *http.Request) (*http.Response, error) {
assert.Equal(t, "headervalue", req.Header.Get("someheader"))
assert.Equal(t, "Basic dXNlcjpwYXNz", req.Header.Get("Authorization"))
resp := httpmock.NewStringResponse(200, `{"some": "data"}`)
resp.Header.Set("Content-Encoding", "gzip")
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(`{"some": "data"}`)); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
resp.Body = io.NopCloser(&b)
return resp, nil
})

resp, err := c.R().Get("/test")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode())
assert.Equal(t, `{"some": "data"}`, resp.String())

assert.Equal(t, 1, httpmock.GetTotalCallCount())
}

func TestRequestWithRateLimiter(t *testing.T) {
rps := 5
expectedNumberOfRequest := 20 // should take longer than 3 seconds less than 4 seconds
Expand Down

0 comments on commit 0caf4ee

Please sign in to comment.