2024-03-27 19:41:27 +00:00
|
|
|
package web_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
|
|
"git.akyoto.dev/go/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBytes(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
return ctx.Bytes([]byte("Hello"))
|
|
|
|
})
|
|
|
|
|
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 TestString(t *testing.T) {
|
|
|
|
s := web.NewServer()
|
|
|
|
|
|
|
|
s.Get("/", func(ctx web.Context) error {
|
|
|
|
return ctx.String("Hello")
|
|
|
|
})
|
|
|
|
|
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 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 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00: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 11:22:45 +00:00
|
|
|
response := s.Request("GET", "/", nil, nil)
|
2024-03-27 19:41:27 +00:00
|
|
|
assert.Equal(t, response.Status(), 401)
|
|
|
|
assert.Equal(t, string(response.Body()), "")
|
|
|
|
}
|