generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
631 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.