Improved API
This commit is contained in:
@ -27,12 +27,12 @@ func TestRouter(t *testing.T) {
|
||||
})
|
||||
|
||||
s.Get("/write", func(ctx server.Context) error {
|
||||
_, err := ctx.Write([]byte("Hello"))
|
||||
_, err := ctx.Response().Write([]byte("Hello"))
|
||||
return err
|
||||
})
|
||||
|
||||
s.Get("/writestring", func(ctx server.Context) error {
|
||||
_, err := io.WriteString(ctx, "Hello")
|
||||
_, err := io.WriteString(ctx.Response(), "Hello")
|
||||
return err
|
||||
})
|
||||
|
||||
@ -45,25 +45,38 @@ func TestRouter(t *testing.T) {
|
||||
})
|
||||
|
||||
s.Get("/reader", func(ctx server.Context) error {
|
||||
return ctx.Reader(strings.NewReader("Hello"))
|
||||
return ctx.Copy(strings.NewReader("Hello"))
|
||||
})
|
||||
|
||||
s.Get("/file", func(ctx server.Context) error {
|
||||
return ctx.File("testdata/file.txt")
|
||||
})
|
||||
|
||||
s.Get("/echo", func(ctx server.Context) error {
|
||||
return ctx.Copy(ctx.Request())
|
||||
})
|
||||
|
||||
s.Get("/context", func(ctx server.Context) error {
|
||||
return ctx.Request().Context().Err()
|
||||
})
|
||||
|
||||
s.Get("/request/data", func(ctx server.Context) error {
|
||||
method := ctx.Method()
|
||||
protocol := ctx.Protocol()
|
||||
host := ctx.Host()
|
||||
path := ctx.Path()
|
||||
request := ctx.Request()
|
||||
method := request.Method()
|
||||
protocol := request.Protocol()
|
||||
host := request.Host()
|
||||
path := request.Path()
|
||||
return ctx.String(fmt.Sprintf("%s %s %s %s", method, protocol, host, path))
|
||||
})
|
||||
|
||||
s.Get("/request/header", func(ctx server.Context) error {
|
||||
acceptEncoding := ctx.RequestHeader("Accept-Encoding")
|
||||
acceptEncoding := ctx.Request().Header("Accept-Encoding")
|
||||
return ctx.String(acceptEncoding)
|
||||
})
|
||||
|
||||
s.Get("/response/header", func(ctx server.Context) error {
|
||||
ctx.Header("Content-Type", "text/plain")
|
||||
contentType := ctx.ResponseHeader("Content-Type")
|
||||
ctx.Response().SetHeader("Content-Type", "text/plain")
|
||||
contentType := ctx.Response().Header("Content-Type")
|
||||
return ctx.String(contentType)
|
||||
})
|
||||
|
||||
@ -78,7 +91,7 @@ func TestRouter(t *testing.T) {
|
||||
})
|
||||
|
||||
s.Get("/scheme", func(ctx server.Context) error {
|
||||
return ctx.String(ctx.Scheme())
|
||||
return ctx.String(ctx.Request().Scheme())
|
||||
})
|
||||
|
||||
s.Post("/", func(ctx server.Context) error {
|
||||
@ -94,33 +107,37 @@ func TestRouter(t *testing.T) {
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
Method string
|
||||
URL string
|
||||
Status int
|
||||
Body string
|
||||
Method string
|
||||
URL string
|
||||
Body string
|
||||
Status int
|
||||
Response string
|
||||
}{
|
||||
{Method: "GET", URL: "/", 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: "/request/data", Status: http.StatusOK, Body: "GET HTTP/1.1 example.com /request/data"},
|
||||
{Method: "GET", URL: "/request/header", Status: http.StatusOK, Body: ""},
|
||||
{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: "/scheme", Status: http.StatusOK, Body: "http"},
|
||||
{Method: "GET", URL: "/write", Status: http.StatusOK, Body: "Hello"},
|
||||
{Method: "GET", URL: "/writestring", 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"},
|
||||
{Method: "GET", URL: "/", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||
{Method: "GET", URL: "/context", Body: "", Status: http.StatusOK, Response: ""},
|
||||
{Method: "GET", URL: "/echo", Body: "Echo", Status: http.StatusOK, Response: "Echo"},
|
||||
{Method: "GET", URL: "/error", Body: "", Status: http.StatusUnauthorized, Response: "Not logged in"},
|
||||
{Method: "GET", URL: "/error2", Body: "", Status: http.StatusUnauthorized, Response: "Not logged in\nMissing auth token"},
|
||||
{Method: "GET", URL: "/file", Body: "", Status: http.StatusOK, Response: "Hello File"},
|
||||
{Method: "GET", URL: "/not-found", Body: "", Status: http.StatusNotFound, Response: http.StatusText(http.StatusNotFound)},
|
||||
{Method: "GET", URL: "/request/data", Body: "", Status: http.StatusOK, Response: "GET HTTP/1.1 example.com /request/data"},
|
||||
{Method: "GET", URL: "/request/header", Body: "", Status: http.StatusOK, Response: ""},
|
||||
{Method: "GET", URL: "/response/header", Body: "", Status: http.StatusOK, Response: "text/plain"},
|
||||
{Method: "GET", URL: "/reader", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||
{Method: "GET", URL: "/string", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||
{Method: "GET", URL: "/scheme", Body: "", Status: http.StatusOK, Response: "http"},
|
||||
{Method: "GET", URL: "/write", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||
{Method: "GET", URL: "/writestring", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||
{Method: "GET", URL: "/blog/testing-my-router", Body: "", Status: http.StatusOK, Response: "testing-my-router"},
|
||||
{Method: "GET", URL: "/missing-parameter", Body: "", Status: http.StatusOK, Response: ""},
|
||||
{Method: "POST", URL: "/", Body: "", Status: http.StatusOK, Response: "Post"},
|
||||
{Method: "DELETE", URL: "/", Body: "", Status: http.StatusOK, Response: "Delete"},
|
||||
{Method: "PUT", URL: "/", Body: "", Status: http.StatusOK, Response: "Put"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run("example.com"+test.URL, func(t *testing.T) {
|
||||
request := httptest.NewRequest(test.Method, "http://example.com"+test.URL, nil)
|
||||
request := httptest.NewRequest(test.Method, "http://example.com"+test.URL, strings.NewReader(test.Body))
|
||||
response := httptest.NewRecorder()
|
||||
s.ServeHTTP(response, request)
|
||||
|
||||
@ -129,7 +146,7 @@ func TestRouter(t *testing.T) {
|
||||
|
||||
body, err := io.ReadAll(result.Body)
|
||||
assert.Nil(t, err)
|
||||
assert.DeepEqual(t, string(body), test.Body)
|
||||
assert.Equal(t, string(body), test.Response)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -138,7 +155,7 @@ func TestMiddleware(t *testing.T) {
|
||||
s := server.New()
|
||||
|
||||
s.Use(func(ctx server.Context) error {
|
||||
ctx.Header("Middleware", "true")
|
||||
ctx.Response().SetHeader("Middleware", "true")
|
||||
return ctx.Next()
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user