Implemented the io.Writer interface
This commit is contained in:
parent
c13dbc55d2
commit
80dc3f87eb
@ -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
|
||||
|
@ -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"},
|
||||
|
Loading…
Reference in New Issue
Block a user