Skip to content

Commit

Permalink
iter9 features
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0rcist committed Jul 25, 2024
1 parent fd9be8c commit 42a295d
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 10 deletions.
1 change: 0 additions & 1 deletion db

This file was deleted.

1 change: 0 additions & 1 deletion db2

This file was deleted.

31 changes: 31 additions & 0 deletions internal/compression/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package compression

import (
"bytes"
"compress/gzip"
"io"
"testing"
)

func TestPack_Success(t *testing.T) {
data := []byte("test data")
buffer, err := Pack(data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

reader, err := gzip.NewReader(buffer)
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer reader.Close()

unpackedData, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("expected no error reading from gzip reader, got %v", err)
}

if !bytes.Equal(data, unpackedData) {
t.Fatalf("expected %s, got %s", data, unpackedData)
}
}
2 changes: 2 additions & 0 deletions internal/compression/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ func (c *Compressor) Close() {
if err := c.encoder.Close(); err != nil {
logging.LogErrorCtx(c.context, err, "error closing compressor encoder", err.Error())
}

c.encoder = nil
}
97 changes: 97 additions & 0 deletions internal/compression/compressor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package compression

import (
"bytes"
"compress/gzip"
"context"
"net/http/httptest"
"testing"
)

func TestCompressor_Write_SupportedContent(t *testing.T) {
ctx := context.Background()
recorder := httptest.NewRecorder()

compressor := NewCompressor(recorder, ctx)
compressor.Header().Set("Content-Type", "application/json")

data := []byte(`{"message": "test"}`)
n, err := compressor.Write(data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if n != len(data) {
t.Fatalf("expected %d bytes written, got %d", len(data), n)
}

compressor.Close()

resp := recorder.Result()
defer resp.Body.Close()

if resp.Header.Get("Content-Encoding") != "gzip" {
t.Fatalf("expected Content-Encoding to be gzip, got %s", resp.Header.Get("Content-Encoding"))
}

gr, err := gzip.NewReader(resp.Body)
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer gr.Close()

uncompressedData := new(bytes.Buffer)
_, err = uncompressedData.ReadFrom(gr)
if err != nil {
t.Fatalf("expected no error decompressing dara, got %v", err)
}
if !bytes.Equal(data, uncompressedData.Bytes()) {
t.Fatalf("expected %s, got %s", data, uncompressedData.Bytes())
}
}

func TestCompressor_Write_UnsupportedContent(t *testing.T) {
recorder := httptest.NewRecorder()
ctx := context.Background()
compressor := NewCompressor(recorder, ctx)
compressor.Header().Set("Content-Type", "text/plain")

data := []byte("test data")
n, err := compressor.Write(data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if n != len(data) {
t.Fatalf("expected %d bytes written, got %d", len(data), n)
}

resp := recorder.Result()
defer resp.Body.Close()

if resp.Header.Get("Content-Encoding") == "gzip" {
t.Fatalf("expected Content-Encoding to not be gzip")
}

body := recorder.Body.Bytes()
if !bytes.Equal(data, body) {
t.Fatalf("expected %s, got %s", data, body)
}
}

func TestCompressor_Close(t *testing.T) {
recorder := httptest.NewRecorder()
ctx := context.Background()
compressor := NewCompressor(recorder, ctx)
compressor.Header().Set("Content-Type", "application/json")

data := []byte(`{"message": "test"}`)
_, err := compressor.Write(data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

compressor.Close()

if compressor.encoder != nil {
t.Fatalf("expected encoder to be nil after close")
}
}
2 changes: 2 additions & 0 deletions internal/compression/decompressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ func (d *Decompressor) Close() {
if err := d.reader.Close(); err != nil {
logging.LogErrorCtx(d.context, err, "error closing decompressor reader", err.Error())
}

d.reader = nil
}
130 changes: 130 additions & 0 deletions internal/compression/decompressor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package compression

import (
"bytes"
"compress/gzip"
"context"
"errors"
"io"

"github.com/ex0rcist/metflix/internal/entities"

"net/http"
"net/http/httptest"
"testing"
)

func TestDecompressor_Decompress_SupportedEncoding(t *testing.T) {
data := []byte("test data")
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
_, err := writer.Write(data)
if err != nil {
t.Fatalf("expected no error on writer.Write(), got %v", err)
}

writer.Close()

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")

ctx := context.Background()
decompressor := NewDecompressor(req, ctx)

err = decompressor.Decompress()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

decompressedData, err := io.ReadAll(decompressor.request.Body)
if err != nil {
t.Fatalf("expected no error reading decompressed data, got %v", err)
}

if !bytes.Equal(data, decompressedData) {
t.Fatalf("expected %s, got %s", data, decompressedData)
}

decompressor.Close()
}

func TestDecompressor_Decompress_NoEncoding(t *testing.T) {
data := []byte("test data")
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(data))

ctx := context.Background()
decompressor := NewDecompressor(req, ctx)

err := decompressor.Decompress()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

decompressedData, err := io.ReadAll(decompressor.request.Body)
if err != nil {
t.Fatalf("expected no error reading data, got %v", err)
}

if !bytes.Equal(data, decompressedData) {
t.Fatalf("expected %s, got %s", data, decompressedData)
}

decompressor.Close()
}

func TestDecompressor_Decompress_UnsupportedEncoding(t *testing.T) {
data := []byte("test data")
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(data))
req.Header.Set("Content-Encoding", "deflate")

ctx := context.Background()
decompressor := NewDecompressor(req, ctx)

err := decompressor.Decompress()
if !errors.Is(err, entities.ErrEncodingUnsupported) {
t.Fatalf("expected %v, got %v", entities.ErrEncodingUnsupported, err)
}
}

func TestDecompressor_Close(t *testing.T) {
data := []byte("test data")
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
_, err := writer.Write(data)
if err != nil {
t.Fatalf("expected no error on writer.Write, got %v", err)
}
writer.Close()

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")

ctx := context.Background()
decompressor := NewDecompressor(req, ctx)

err = decompressor.Decompress()
if err != nil {
t.Fatalf("expected no error on Decompress(), got %v", err)
}

decompressor.Close()

if decompressor.reader != nil {
t.Fatalf("expected reader to be nil after close")
}
}

func TestDecompressor_Decompress_ErrorOnInit(t *testing.T) {
// providing invalid gzip data to simulate error on NewReader
data := []byte("invalid gzip data")
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(data))
req.Header.Set("Content-Encoding", "gzip")

ctx := context.Background()
decompressor := NewDecompressor(req, ctx)

err := decompressor.Decompress()
if !errors.Is(err, entities.ErrEncodingInternal) {
t.Fatalf("expected %v, got %v", entities.ErrEncodingInternal, err)
}
}
Loading

0 comments on commit 42a295d

Please sign in to comment.