From 80dc3f87ebfe75e2cd165e13173a32e503b851f0 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 13 Mar 2024 22:49:57 +0100 Subject: [PATCH] Implemented the io.Writer interface --- Context.go | 6 ++++++ Server_test.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/Context.go b/Context.go index 1525c78..0f35cd3 100644 --- a/Context.go +++ b/Context.go @@ -26,6 +26,7 @@ type Context interface { ResponseHeader(key string) string Status(status int) Context String(string) error + Write([]byte) (int, error) } // ctx represents a request & response context. @@ -142,6 +143,11 @@ func (ctx *ctx) String(body string) error { return ctx.Bytes(slice) } +// Write implements the io.Writer interface. +func (ctx *ctx) Write(body []byte) (int, error) { + return ctx.response.Write(body) +} + // addParameter adds a new parameter to the context. func (ctx *ctx) addParameter(name string, value string) { ctx.paramNames[ctx.paramCount] = name diff --git a/Server_test.go b/Server_test.go index bb87387..58e62a0 100644 --- a/Server_test.go +++ b/Server_test.go @@ -26,6 +26,11 @@ func TestRouter(t *testing.T) { return ctx.String("Hello") }) + s.Get("/write", func(ctx server.Context) error { + _, err := io.WriteString(ctx, "Hello") + return err + }) + s.Get("/error", func(ctx server.Context) error { return ctx.Status(http.StatusUnauthorized).Error("Not logged in") }) @@ -94,6 +99,7 @@ func TestRouter(t *testing.T) { {Method: "GET", URL: "/response/header", Status: http.StatusOK, Body: "text/plain"}, {Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"}, {Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"}, + {Method: "GET", URL: "/write", 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"},