From 8edd3cd83f14c92a21f9f23593be1103a4815b3e Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 22 Jul 2023 11:48:35 +0200 Subject: [PATCH] Added parameter retrieval --- Context.go | 12 ++++++++++++ Server_test.go | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Context.go b/Context.go index ab68f05..460a648 100644 --- a/Context.go +++ b/Context.go @@ -14,6 +14,7 @@ const maxParams = 16 type Context interface { Bytes([]byte) error Error(status int, messages ...any) error + Get(param string) string Reader(io.Reader) error SetStatus(status int) String(string) error @@ -60,6 +61,17 @@ func (ctx *context) Error(status int, messages ...any) error { 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. func (ctx *context) Reader(reader io.Reader) error { _, err := io.Copy(ctx.response, reader) diff --git a/Server_test.go b/Server_test.go index 185f5b6..2823690 100644 --- a/Server_test.go +++ b/Server_test.go @@ -19,8 +19,8 @@ func TestRouter(t *testing.T) { return ctx.Bytes([]byte("Hello")) }) - s.Get("/blog/:post", func(ctx server.Context) error { - return ctx.Bytes([]byte("Hello")) + s.Get("/string", func(ctx server.Context) error { + return ctx.String("Hello") }) s.Get("/error", func(ctx server.Context) error { @@ -35,8 +35,14 @@ func TestRouter(t *testing.T) { return ctx.Reader(strings.NewReader("Hello")) }) - s.Get("/string", func(ctx server.Context) error { - return ctx.String("Hello") + 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) }) s.Post("/", func(ctx server.Context) error { @@ -58,12 +64,13 @@ func TestRouter(t *testing.T) { Body string }{ {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: "/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: "/reader", 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: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"}, {Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"},