142 lines
4.0 KiB
Go
Raw Normal View History

2023-07-18 19:48:38 +00:00
package server_test
2023-07-18 16:02:57 +00:00
import (
"errors"
2023-07-22 10:32:52 +00:00
"fmt"
2023-07-18 16:02:57 +00:00
"io"
"net/http"
"net/http/httptest"
2023-07-19 11:01:51 +00:00
"strings"
2023-07-18 16:02:57 +00:00
"testing"
"git.akyoto.dev/go/assert"
2023-07-18 19:48:38 +00:00
"git.akyoto.dev/go/server"
2023-07-18 16:02:57 +00:00
)
2023-07-19 10:31:33 +00:00
func TestRouter(t *testing.T) {
2023-07-18 19:48:38 +00:00
s := server.New()
2023-07-18 16:02:57 +00:00
2023-07-18 19:48:38 +00:00
s.Get("/", func(ctx server.Context) error {
2023-07-18 16:02:57 +00:00
return ctx.Bytes([]byte("Hello"))
})
2023-07-22 09:48:35 +00:00
s.Get("/string", func(ctx server.Context) error {
return ctx.String("Hello")
2023-07-18 16:02:57 +00:00
})
2023-07-18 19:48:38 +00:00
s.Get("/error", func(ctx server.Context) error {
2023-07-21 21:23:49 +00:00
return ctx.Error(http.StatusUnauthorized, "Not logged in")
})
s.Get("/error2", func(ctx server.Context) error {
return ctx.Error(http.StatusUnauthorized, "Not logged in", errors.New("Missing auth token"))
2023-07-18 16:02:57 +00:00
})
2023-07-19 11:01:51 +00:00
s.Get("/reader", func(ctx server.Context) error {
return ctx.Reader(strings.NewReader("Hello"))
})
2023-07-22 10:32:52 +00:00
s.Get("/request/data", func(ctx server.Context) error {
request := ctx.Request()
method := request.Method()
protocol := request.Protocol()
host := request.Host()
path := request.Path()
return ctx.String(fmt.Sprintf("%s %s %s %s", method, protocol, host, path))
})
s.Get("/request/header", func(ctx server.Context) error {
request := ctx.Request()
acceptEncoding := request.Header("Accept-Encoding")
return ctx.String(acceptEncoding)
})
s.Get("/response/header", func(ctx server.Context) error {
response := ctx.Response()
response.SetHeader("Content-Type", "text/plain")
contentType := response.Header("Content-Type")
return ctx.String(contentType)
})
2023-07-22 09:48:35 +00:00
s.Get("/blog/:article", func(ctx server.Context) error {
article := ctx.Get("article")
return ctx.String(article)
})
s.Get("/missing-parameter", func(ctx server.Context) error {
missing := ctx.Get("missing")
return ctx.String(missing)
2023-07-19 11:01:51 +00:00
})
2023-07-22 09:36:28 +00:00
s.Post("/", func(ctx server.Context) error {
return ctx.String("Post")
})
s.Delete("/", func(ctx server.Context) error {
return ctx.String("Delete")
})
s.Put("/", func(ctx server.Context) error {
return ctx.String("Put")
})
2023-07-18 16:02:57 +00:00
tests := []struct {
2023-07-22 09:36:28 +00:00
Method string
2023-07-18 16:02:57 +00:00
URL string
Status int
Body string
}{
2023-07-22 09:36:28 +00:00
{Method: "GET", URL: "/", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"},
{Method: "GET", URL: "/error2", Status: http.StatusUnauthorized, Body: "Not logged in\nMissing auth token"},
{Method: "GET", URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
2023-07-22 10:32:52 +00:00
{Method: "GET", URL: "/request/data", Status: http.StatusOK, Body: "GET HTTP/1.1 example.com /request/data"},
{Method: "GET", URL: "/request/header", Status: http.StatusOK, Body: ""},
{Method: "GET", URL: "/response/header", Status: http.StatusOK, Body: "text/plain"},
2023-07-22 09:36:28 +00:00
{Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"},
2023-07-22 09:48:35 +00:00
{Method: "GET", URL: "/blog/testing-my-router", Status: http.StatusOK, Body: "testing-my-router"},
{Method: "GET", URL: "/missing-parameter", Status: http.StatusOK, Body: ""},
2023-07-22 09:36:28 +00:00
{Method: "POST", URL: "/", Status: http.StatusOK, Body: "Post"},
{Method: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"},
{Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"},
2023-07-18 16:02:57 +00:00
}
for _, test := range tests {
t.Run("example.com"+test.URL, func(t *testing.T) {
2023-07-22 09:36:28 +00:00
request := httptest.NewRequest(test.Method, test.URL, nil)
2023-07-18 16:02:57 +00:00
response := httptest.NewRecorder()
2023-07-18 19:48:38 +00:00
s.ServeHTTP(response, request)
2023-07-18 16:02:57 +00:00
result := response.Result()
assert.Equal(t, result.StatusCode, test.Status)
body, err := io.ReadAll(result.Body)
assert.Nil(t, err)
assert.DeepEqual(t, string(body), test.Body)
})
}
}
2023-07-19 10:31:33 +00:00
func TestPanic(t *testing.T) {
s := server.New()
s.Get("/panic", func(ctx server.Context) error {
panic("Something unbelievable happened")
})
t.Run("example.com/panic", func(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Error("Didn't panic")
}
}()
request := httptest.NewRequest(http.MethodGet, "/panic", nil)
response := httptest.NewRecorder()
s.ServeHTTP(response, request)
})
}