diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-12-29 09:46:21 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-12-29 09:46:21 +0200 |
commit | 23774a8a1870dbdc992492b4f656902b0aef875e (patch) | |
tree | f237c3b82c5ba99148e219408fb5ff3fa6ed5b04 /compress_test.go | |
parent | Optimization: reuse gzip/flate readers and writers. This should improve respo... (diff) | |
download | fasthttp-23774a8a1870dbdc992492b4f656902b0aef875e.tar.gz fasthttp-23774a8a1870dbdc992492b4f656902b0aef875e.tar.bz2 fasthttp-23774a8a1870dbdc992492b4f656902b0aef875e.zip |
Added tests for compress.go
Diffstat (limited to 'compress_test.go')
-rw-r--r-- | compress_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/compress_test.go b/compress_test.go new file mode 100644 index 0000000..c42a408 --- /dev/null +++ b/compress_test.go @@ -0,0 +1,63 @@ +package fasthttp + +import ( + "bytes" + "io/ioutil" + "testing" +) + +func TestGzipCompress(t *testing.T) { + testGzipCompress(t, "") + testGzipCompress(t, "foobar") + testGzipCompress(t, "ajjnkn asdlkjfqoijfw jfqkwj foj eowjiq") +} + +func TestFlateCompress(t *testing.T) { + testFlateCompress(t, "") + testFlateCompress(t, "foobar") + testFlateCompress(t, "adf asd asd fasd fasd") +} + +func testGzipCompress(t *testing.T, s string) { + var buf bytes.Buffer + zw := acquireGzipWriter(&buf, CompressDefaultCompression) + if _, err := zw.Write([]byte(s)); err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + releaseGzipWriter(zw) + + zr, err := acquireGzipReader(&buf) + if err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + body, err := ioutil.ReadAll(zr) + if err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + if string(body) != s { + t.Fatalf("unexpected string after decompression: %q. Expecting %q", body, s) + } + releaseGzipReader(zr) +} + +func testFlateCompress(t *testing.T, s string) { + var buf bytes.Buffer + zw := acquireFlateWriter(&buf, CompressDefaultCompression) + if _, err := zw.Write([]byte(s)); err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + releaseFlateWriter(zw) + + zr, err := acquireFlateReader(&buf) + if err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + body, err := ioutil.ReadAll(zr) + if err != nil { + t.Fatalf("unexpected error: %s. s=%q", err, s) + } + if string(body) != s { + t.Fatalf("unexpected string after decompression: %q. Expecting %q", body, s) + } + releaseFlateReader(zr) +} |