Added more tests

This commit is contained in:
2024-03-27 20:41:27 +01:00
parent 98e1a47328
commit b6c208c75b
7 changed files with 211 additions and 144 deletions

57
Context_test.go Normal file
View File

@ -0,0 +1,57 @@
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"))
})
response := s.Request("GET", "/", nil)
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")
})
response := s.Request("GET", "/", nil)
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")
})
response := s.Request("GET", "/", nil)
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"))
})
response := s.Request("GET", "/", nil)
assert.Equal(t, response.Status(), 401)
assert.Equal(t, string(response.Body()), "")
}