Web/tests/context_test.go

89 lines
1.7 KiB
Go

package web_tests
import (
"errors"
"testing"
web "git.sharkk.net/Go/Web"
)
func TestBytes(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Bytes([]byte("Hello"))
})
response := s.Request("GET", "/", nil, nil)
if response.Status() != 200 {
t.Error(response.Status())
}
if string(response.Body()) != "Hello" {
t.Error(string(response.Body()))
}
}
func TestString(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.String("Hello")
})
response := s.Request("GET", "/", nil, nil)
if response.Status() != 200 {
t.Error(response.Status())
}
if string(response.Body()) != "Hello" {
t.Error(string(response.Body()))
}
}
func TestError(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Status(401).Error("Not logged in")
})
response := s.Request("GET", "/", nil, nil)
if response.Status() != 401 {
t.Error(response.Status())
}
if string(response.Body()) != "" {
t.Error(string(response.Body()))
}
}
func TestErrorMultiple(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Status(401).Error("Not logged in", errors.New("Missing auth token"))
})
response := s.Request("GET", "/", nil, nil)
if response.Status() != 401 {
t.Error(response.Status())
}
if string(response.Body()) != "" {
t.Error(string(response.Body()))
}
}
func TestRedirect(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Redirect(301, "/target")
})
response := s.Request("GET", "/", nil, nil)
if response.Status() != 301 {
t.Error(response.Status())
}
if response.Header("Location") != "/target" {
t.Error(response.Header("Location"))
}
}