114 lines
3.7 KiB
Go
Raw Normal View History

2024-03-22 15:08:24 +01:00
package web_test
2023-07-18 18:02:57 +02:00
import (
2024-03-27 14:06:13 +01:00
"errors"
"fmt"
"io"
"strings"
2023-07-18 18:02:57 +02:00
"testing"
"git.akyoto.dev/go/assert"
2024-03-22 15:08:24 +01:00
"git.akyoto.dev/go/web"
2023-07-18 18:02:57 +02:00
)
2024-03-27 14:06:13 +01:00
func TestContext(t *testing.T) {
2024-03-22 15:08:24 +01:00
s := web.NewServer()
2023-07-18 18:02:57 +02:00
2024-03-27 14:06:13 +01:00
s.Get("/bytes", func(ctx web.Context) error {
2023-07-18 18:02:57 +02:00
return ctx.Bytes([]byte("Hello"))
})
2024-03-27 14:06:13 +01:00
s.Get("/string", func(ctx web.Context) error {
return ctx.String("Hello")
})
s.Get("/write", func(ctx web.Context) error {
_, err := ctx.Response().Write([]byte("Hello"))
return err
})
s.Get("/writestring", func(ctx web.Context) error {
_, err := io.WriteString(ctx.Response(), "Hello")
return err
})
s.Get("/error", func(ctx web.Context) error {
return ctx.Status(401).Error("Not logged in")
})
s.Get("/error2", func(ctx web.Context) error {
return ctx.Status(401).Error("Not logged in", errors.New("Missing auth token"))
})
s.Get("/request/data", func(ctx web.Context) error {
req := ctx.Request()
method := req.Method()
scheme := req.Scheme()
host := req.Host()
path := req.Path()
return ctx.String(fmt.Sprintf("%s %s %s %s", method, scheme, host, path))
})
s.Router().Add("POST", "/", func(ctx web.Context) error {
return ctx.String("Post")
})
s.Router().Add("DELETE", "/", func(ctx web.Context) error {
return ctx.String("Delete")
})
s.Router().Add("PUT", "/", func(ctx web.Context) error {
return ctx.String("Put")
})
tests := []struct {
Method string
Path string
Body string
Status int
Response string
}{
{Method: "GET", Path: "/bytes", Body: "", Status: 200, Response: "Hello"},
// {Method: "GET", Path: "/context", Body: "", Status: 200, Response: ""},
// {Method: "GET", Path: "/echo", Body: "Echo", Status: 200, Response: "Echo"},
{Method: "GET", Path: "/error", Body: "", Status: 401, Response: ""},
{Method: "GET", Path: "/error2", Body: "", Status: 401, Response: ""},
// {Method: "GET", Path: "/file", Body: "", Status: 200, Response: "Hello File"},
// {Method: "GET", Path: "/flush", Body: "", Status: 200, Response: "Hello 1\nHello 2\n"},
// {Method: "GET", Path: "/not-found", Body: "", Status: 404, Response: ""},
{Method: "GET", Path: "/request/data", Body: "", Status: 200, Response: "GET http example.com /request/data"},
// {Method: "GET", Path: "/request/header", Body: "", Status: 200, Response: ""},
// {Method: "GET", Path: "/response/header", Body: "", Status: 200, Response: "text/plain"},
// {Method: "GET", Path: "/reader", Body: "", Status: 200, Response: "Hello"},
// {Method: "GET", Path: "/redirect", Body: "", Status: 307, Response: ""},
{Method: "GET", Path: "/string", Body: "", Status: 200, Response: "Hello"},
{Method: "GET", Path: "/write", Body: "", Status: 200, Response: "Hello"},
{Method: "GET", Path: "/writestring", Body: "", Status: 200, Response: "Hello"},
// {Method: "GET", Path: "/blog/testing-my-router", Body: "", Status: 200, Response: "testing-my-router"},
// {Method: "GET", Path: "/missing-parameter", Body: "", Status: 200, Response: ""},
{Method: "POST", Path: "/", Body: "", Status: 200, Response: "Post"},
{Method: "DELETE", Path: "/", Body: "", Status: 200, Response: "Delete"},
{Method: "PUT", Path: "/", Body: "", Status: 200, Response: "Put"},
}
for _, test := range tests {
t.Run(test.Path, func(t *testing.T) {
response := s.Request(test.Method, "http://example.com"+test.Path, strings.NewReader(test.Body))
assert.Equal(t, response.Status(), test.Status)
assert.Equal(t, string(response.Body()), test.Response)
})
}
2024-03-13 20:18:01 +01:00
}
2024-03-26 22:46:16 +01:00
func TestString(t *testing.T) {
2024-03-22 15:08:24 +01:00
s := web.NewServer()
2023-07-19 12:31:33 +02:00
2024-03-26 22:46:16 +01:00
s.Get("/", func(ctx web.Context) error {
return ctx.String("Hello")
2023-07-19 12:31:33 +02:00
})
2024-03-12 22:31:45 +01:00
2024-03-27 11:36:12 +01:00
response := s.Request("GET", "/", nil)
2024-03-26 22:46:16 +01:00
assert.Equal(t, response.Status(), 200)
assert.DeepEqual(t, response.Body(), []byte("Hello"))
2024-03-12 22:31:45 +01:00
}