2024-03-27 20:41:27 +01:00

67 lines
1.6 KiB
Go

package web_test
import (
"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
})
response := s.Request("GET", "/", nil)
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
})
response := s.Request("GET", "/", nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
}
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)
})
response := s.Request("GET", "/", nil)
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
})
response := s.Request("GET", "/", nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, response.Header("Content-Type"), "text/html")
assert.Equal(t, string(response.Body()), "")
}