55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
|
package aero_test
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
|
||
|
"git.akyoto.dev/go/aero"
|
||
|
"git.akyoto.dev/go/assert"
|
||
|
)
|
||
|
|
||
|
func TestServer(t *testing.T) {
|
||
|
server := aero.New()
|
||
|
|
||
|
server.Get("/", func(ctx aero.Context) error {
|
||
|
return ctx.Bytes([]byte("Hello"))
|
||
|
})
|
||
|
|
||
|
server.Get("/blog/:post", func(ctx aero.Context) error {
|
||
|
return ctx.Bytes([]byte("Hello"))
|
||
|
})
|
||
|
|
||
|
server.Get("/error", func(ctx aero.Context) error {
|
||
|
return ctx.Error(http.StatusUnauthorized, errors.New("Not logged in"))
|
||
|
})
|
||
|
|
||
|
tests := []struct {
|
||
|
URL string
|
||
|
Status int
|
||
|
Body string
|
||
|
}{
|
||
|
{URL: "/", Status: http.StatusOK, Body: "Hello"},
|
||
|
{URL: "/blog/post", Status: http.StatusOK, Body: "Hello"},
|
||
|
{URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"},
|
||
|
{URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
t.Run("example.com"+test.URL, func(t *testing.T) {
|
||
|
request := httptest.NewRequest(http.MethodGet, test.URL, nil)
|
||
|
response := httptest.NewRecorder()
|
||
|
server.ServeHTTP(response, request)
|
||
|
|
||
|
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)
|
||
|
})
|
||
|
}
|
||
|
}
|