2024-03-27 19:41:27 +00:00
|
|
|
package web_test
|
|
|
|
|
|
|
|
import (
|
2024-03-28 10:22:52 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2024-03-27 19:41:27 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
|
|
"git.akyoto.dev/go/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
_, err := ctx.Response().Write([]byte("Hello"))
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2024-03-28 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00:00
|
|
|
assert.Equal(t, response.Status(), 200)
|
|
|
|
assert.Equal(t, string(response.Body()), "Hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteString(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
_, err := io.WriteString(ctx.Response(), "Hello")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2024-03-28 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00:00
|
|
|
assert.Equal(t, response.Status(), 200)
|
|
|
|
assert.Equal(t, string(response.Body()), "Hello")
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:22:52 +00:00
|
|
|
func TestResponseCompression(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
uncompressed := bytes.Repeat([]byte("This text should be compressed to a size smaller than the original."), 5)
|
|
|
|
|
2024-03-28 10:27:06 +00:00
|
|
|
s.Use(func(ctx web.Context) error {
|
2024-03-28 11:22:45 +00:00
|
|
|
defer func() {
|
|
|
|
body := ctx.Response().Body()
|
|
|
|
ctx.Response().SetBody(nil)
|
|
|
|
zip := gzip.NewWriter(ctx.Response())
|
|
|
|
zip.Write(body)
|
|
|
|
zip.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx.Next()
|
2024-03-28 10:22:52 +00:00
|
|
|
})
|
|
|
|
|
2024-03-28 10:27:06 +00:00
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
return ctx.Bytes(uncompressed)
|
|
|
|
})
|
|
|
|
|
2024-03-28 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-28 10:22:52 +00:00
|
|
|
assert.Equal(t, response.Status(), 200)
|
|
|
|
assert.True(t, len(response.Body()) < len(uncompressed))
|
|
|
|
|
|
|
|
reader, err := gzip.NewReader(bytes.NewReader(response.Body()))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
decompressed, err := io.ReadAll(reader)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.DeepEqual(t, decompressed, uncompressed)
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:41:27 +00:00
|
|
|
func TestResponseHeader(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
ctx.Response().SetHeader("Content-Type", "text/plain")
|
|
|
|
contentType := ctx.Response().Header("Content-Type")
|
|
|
|
return ctx.String(contentType)
|
|
|
|
})
|
|
|
|
|
2024-03-28 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00:00
|
|
|
assert.Equal(t, response.Status(), 200)
|
|
|
|
assert.Equal(t, response.Header("Content-Type"), "text/plain")
|
|
|
|
assert.Equal(t, response.Header("Non existent header"), "")
|
|
|
|
assert.Equal(t, string(response.Body()), "text/plain")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResponseHeaderOverwrite(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
ctx.Response().SetHeader("Content-Type", "text/plain")
|
|
|
|
ctx.Response().SetHeader("Content-Type", "text/html")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-03-28 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00:00
|
|
|
assert.Equal(t, response.Status(), 200)
|
|
|
|
assert.Equal(t, response.Header("Content-Type"), "text/html")
|
|
|
|
assert.Equal(t, string(response.Body()), "")
|
|
|
|
}
|