Added request and response

This commit is contained in:
2023-07-22 12:32:52 +02:00
parent b2874e175e
commit 63ee1655fe
4 changed files with 109 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package server_test
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
@ -35,6 +36,28 @@ func TestRouter(t *testing.T) {
return ctx.Reader(strings.NewReader("Hello"))
})
s.Get("/request/data", func(ctx server.Context) error {
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 {
request := ctx.Request()
acceptEncoding := request.Header("Accept-Encoding")
return ctx.String(acceptEncoding)
})
s.Get("/response/header", func(ctx server.Context) error {
response := ctx.Response()
response.SetHeader("Content-Type", "text/plain")
contentType := response.Header("Content-Type")
return ctx.String(contentType)
})
s.Get("/blog/:article", func(ctx server.Context) error {
article := ctx.Get("article")
return ctx.String(article)
@ -67,6 +90,9 @@ func TestRouter(t *testing.T) {
{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: "/blog/testing-my-router", Status: http.StatusOK, Body: "testing-my-router"},