70 lines
1.5 KiB
Go
Raw Normal View History

2024-03-27 20:41:27 +01:00
package web_test
import (
"errors"
"testing"
2025-02-25 16:48:09 +01:00
"git.urbach.dev/go/assert"
"git.urbach.dev/go/web"
2024-03-27 20:41:27 +01:00
)
func TestBytes(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Bytes([]byte("Hello"))
})
2024-03-28 12:22:45 +01:00
response := s.Request("GET", "/", nil, nil)
2024-03-27 20:41:27 +01:00
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
}
func TestString(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.String("Hello")
})
2024-03-28 12:22:45 +01:00
response := s.Request("GET", "/", nil, nil)
2024-03-27 20:41:27 +01:00
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
}
func TestError(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
return ctx.Status(401).Error("Not logged in")
})
2024-03-28 12:22:45 +01:00
response := s.Request("GET", "/", nil, nil)
2024-03-27 20:41:27 +01:00
assert.Equal(t, response.Status(), 401)
assert.Equal(t, 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"))
})
2024-03-28 12:22:45 +01:00
response := s.Request("GET", "/", nil, nil)
2024-03-27 20:41:27 +01:00
assert.Equal(t, response.Status(), 401)
assert.Equal(t, string(response.Body()), "")
}
2024-04-02 16:17:43 +02:00
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)
assert.Equal(t, response.Status(), 301)
assert.Equal(t, response.Header("Location"), "/target")
}