mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
(#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
This commit is contained in:
41
_examples/compression/main_test.go
Normal file
41
_examples/compression/main_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
)
|
||||
|
||||
func TestCompression(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
var expectedReply = payload{Username: "Makis"}
|
||||
body := e.GET("/").WithHeader(context.AcceptEncodingHeaderKey, context.GZIP).Expect().
|
||||
Status(httptest.StatusOK).
|
||||
ContentEncoding(context.GZIP).
|
||||
ContentType(context.ContentJSONHeaderValue).Body().Raw()
|
||||
|
||||
// Note that .Expect() consumes the response body
|
||||
// and stores it to unexported "contents" field
|
||||
// therefore, we retrieve it as string and put it to a new buffer.
|
||||
r := strings.NewReader(body)
|
||||
cr, err := context.NewCompressReader(r, context.GZIP)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cr.Close()
|
||||
|
||||
var got payload
|
||||
if err = json.NewDecoder(cr).Decode(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expectedReply, got) {
|
||||
t.Fatalf("expected %#+v but got %#+v", expectedReply, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user