Add entity-body compression to http output (#4807)
This commit is contained in:
committed by
Daniel Nelson
parent
fafe9d30bf
commit
f3da717a88
@@ -3,8 +3,10 @@ package internal
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"math/big"
|
||||
"os"
|
||||
@@ -208,3 +210,23 @@ func ExitStatus(err error) (int, bool) {
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// CompressWithGzip takes an io.Reader as input and pipes
|
||||
// it through a gzip.Writer returning an io.Reader containing
|
||||
// the gzipped data.
|
||||
// An error is returned if passing data to the gzip.Writer fails
|
||||
func CompressWithGzip(data io.Reader) (io.Reader, error) {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
gzipWriter := gzip.NewWriter(pipeWriter)
|
||||
|
||||
var err error
|
||||
go func() {
|
||||
_, err = io.Copy(gzipWriter, data)
|
||||
gzipWriter.Close()
|
||||
// subsequent reads from the read half of the pipe will
|
||||
// return no bytes and the error err, or EOF if err is nil.
|
||||
pipeWriter.CloseWithError(err)
|
||||
}()
|
||||
|
||||
return pipeReader, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io/ioutil"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -162,3 +165,20 @@ func TestDuration(t *testing.T) {
|
||||
d.UnmarshalTOML([]byte(`1.5`))
|
||||
assert.Equal(t, time.Second, d.Duration)
|
||||
}
|
||||
|
||||
func TestCompressWithGzip(t *testing.T) {
|
||||
testData := "the quick brown fox jumps over the lazy dog"
|
||||
inputBuffer := bytes.NewBuffer([]byte(testData))
|
||||
|
||||
outputBuffer, err := CompressWithGzip(inputBuffer)
|
||||
assert.NoError(t, err)
|
||||
|
||||
gzipReader, err := gzip.NewReader(outputBuffer)
|
||||
assert.NoError(t, err)
|
||||
defer gzipReader.Close()
|
||||
|
||||
output, err := ioutil.ReadAll(gzipReader)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, testData, string(output))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user