Web/tests/request_test.go

71 lines
1.6 KiB
Go

package web_tests
import (
"fmt"
"testing"
web "git.sharkk.net/Go/Web"
)
func TestRequest(t *testing.T) {
s := web.NewServer()
s.Get("/request", func(ctx web.Context) error {
req := ctx.Request()
method := req.Method()
scheme := req.Scheme()
host := req.Host()
path := req.Path()
return ctx.String(fmt.Sprintf("%s %s %s %s", method, scheme, host, path))
})
response := s.Request("GET", "http://example.com/request?x=1", []web.Header{{"Accept", "*/*"}}, nil)
if response.Status() != 200 {
t.Errorf("Error: %s", response.Body())
}
if string(response.Body()) != "GET http example.com /request" {
t.Errorf("Error: %s", response.Body())
}
}
func TestRequestHeader(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
accept := ctx.Request().Header("Accept")
empty := ctx.Request().Header("")
return ctx.String(accept + empty)
})
response := s.Request("GET", "/", []web.Header{{"Accept", "*/*"}}, nil)
if response.Status() != 200 {
t.Errorf("Error: %s", response.Body())
}
if string(response.Body()) != "*/*" {
t.Errorf("Error: %s", response.Body())
}
}
func TestRequestParam(t *testing.T) {
s := web.NewServer()
s.Get("/blog/:article", func(ctx web.Context) error {
article := ctx.Request().Param("article")
empty := ctx.Request().Param("")
return ctx.String(article + empty)
})
response := s.Request("GET", "/blog/my-article", nil, nil)
if response.Status() != 200 {
t.Errorf("Error: %s", response.Body())
}
if string(response.Body()) != "my-article" {
t.Errorf("Error: %s", response.Body())
}
}