Added parameter retrieval

This commit is contained in:
Eduard Urbach 2023-07-22 11:48:35 +02:00
parent 968687849f
commit 01f8b2d5be
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 24 additions and 5 deletions

View File

@ -14,6 +14,7 @@ const maxParams = 16
type Context interface { type Context interface {
Bytes([]byte) error Bytes([]byte) error
Error(status int, messages ...any) error Error(status int, messages ...any) error
Get(param string) string
Reader(io.Reader) error Reader(io.Reader) error
SetStatus(status int) SetStatus(status int)
String(string) error String(string) error
@ -60,6 +61,17 @@ func (ctx *context) Error(status int, messages ...any) error {
return errors.Join(combined...) return errors.Join(combined...)
} }
// Get retrieves a parameter.
func (ctx *context) Get(param string) string {
for i := 0; i < ctx.paramCount; i++ {
if ctx.paramNames[i] == param {
return ctx.paramValues[i]
}
}
return ""
}
// Reader sends the contents of the io.Reader without creating an in-memory copy. // Reader sends the contents of the io.Reader without creating an in-memory copy.
func (ctx *context) Reader(reader io.Reader) error { func (ctx *context) Reader(reader io.Reader) error {
_, err := io.Copy(ctx.response, reader) _, err := io.Copy(ctx.response, reader)

View File

@ -19,8 +19,8 @@ func TestRouter(t *testing.T) {
return ctx.Bytes([]byte("Hello")) return ctx.Bytes([]byte("Hello"))
}) })
s.Get("/blog/:post", func(ctx server.Context) error { s.Get("/string", func(ctx server.Context) error {
return ctx.Bytes([]byte("Hello")) return ctx.String("Hello")
}) })
s.Get("/error", func(ctx server.Context) error { s.Get("/error", func(ctx server.Context) error {
@ -35,8 +35,14 @@ func TestRouter(t *testing.T) {
return ctx.Reader(strings.NewReader("Hello")) return ctx.Reader(strings.NewReader("Hello"))
}) })
s.Get("/string", func(ctx server.Context) error { s.Get("/blog/:article", func(ctx server.Context) error {
return ctx.String("Hello") 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)
}) })
s.Post("/", func(ctx server.Context) error { s.Post("/", func(ctx server.Context) error {
@ -58,12 +64,13 @@ func TestRouter(t *testing.T) {
Body string Body string
}{ }{
{Method: "GET", URL: "/", Status: http.StatusOK, Body: "Hello"}, {Method: "GET", URL: "/", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/blog/post", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"}, {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: "/error2", Status: http.StatusUnauthorized, Body: "Not logged in\nMissing auth token"},
{Method: "GET", URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)}, {Method: "GET", URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
{Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"}, {Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"}, {Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/blog/testing-my-router", Status: http.StatusOK, Body: "testing-my-router"},
{Method: "GET", URL: "/missing-parameter", Status: http.StatusOK, Body: ""},
{Method: "POST", URL: "/", Status: http.StatusOK, Body: "Post"}, {Method: "POST", URL: "/", Status: http.StatusOK, Body: "Post"},
{Method: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"}, {Method: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"},
{Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"}, {Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"},