package web_test import ( "errors" "fmt" "io" "strings" "testing" "git.akyoto.dev/go/assert" "git.akyoto.dev/go/web" ) func TestContext(t *testing.T) { s := web.NewServer() s.Get("/bytes", func(ctx web.Context) error { return ctx.Bytes([]byte("Hello")) }) s.Get("/string", func(ctx web.Context) error { return ctx.String("Hello") }) s.Get("/write", func(ctx web.Context) error { _, err := ctx.Response().Write([]byte("Hello")) return err }) s.Get("/writestring", func(ctx web.Context) error { _, err := io.WriteString(ctx.Response(), "Hello") return err }) s.Get("/error", func(ctx web.Context) error { return ctx.Status(401).Error("Not logged in") }) s.Get("/error2", func(ctx web.Context) error { return ctx.Status(401).Error("Not logged in", errors.New("Missing auth token")) }) s.Get("/request/data", 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)) }) s.Router().Add("POST", "/", func(ctx web.Context) error { return ctx.String("Post") }) s.Router().Add("DELETE", "/", func(ctx web.Context) error { return ctx.String("Delete") }) s.Router().Add("PUT", "/", func(ctx web.Context) error { return ctx.String("Put") }) tests := []struct { Method string Path string Body string Status int Response string }{ {Method: "GET", Path: "/bytes", Body: "", Status: 200, Response: "Hello"}, // {Method: "GET", Path: "/context", Body: "", Status: 200, Response: ""}, // {Method: "GET", Path: "/echo", Body: "Echo", Status: 200, Response: "Echo"}, {Method: "GET", Path: "/error", Body: "", Status: 401, Response: ""}, {Method: "GET", Path: "/error2", Body: "", Status: 401, Response: ""}, // {Method: "GET", Path: "/file", Body: "", Status: 200, Response: "Hello File"}, // {Method: "GET", Path: "/flush", Body: "", Status: 200, Response: "Hello 1\nHello 2\n"}, // {Method: "GET", Path: "/not-found", Body: "", Status: 404, Response: ""}, {Method: "GET", Path: "/request/data", Body: "", Status: 200, Response: "GET http example.com /request/data"}, // {Method: "GET", Path: "/request/header", Body: "", Status: 200, Response: ""}, // {Method: "GET", Path: "/response/header", Body: "", Status: 200, Response: "text/plain"}, // {Method: "GET", Path: "/reader", Body: "", Status: 200, Response: "Hello"}, // {Method: "GET", Path: "/redirect", Body: "", Status: 307, Response: ""}, {Method: "GET", Path: "/string", Body: "", Status: 200, Response: "Hello"}, {Method: "GET", Path: "/write", Body: "", Status: 200, Response: "Hello"}, {Method: "GET", Path: "/writestring", Body: "", Status: 200, Response: "Hello"}, // {Method: "GET", Path: "/blog/testing-my-router", Body: "", Status: 200, Response: "testing-my-router"}, // {Method: "GET", Path: "/missing-parameter", Body: "", Status: 200, Response: ""}, {Method: "POST", Path: "/", Body: "", Status: 200, Response: "Post"}, {Method: "DELETE", Path: "/", Body: "", Status: 200, Response: "Delete"}, {Method: "PUT", Path: "/", Body: "", Status: 200, Response: "Put"}, } for _, test := range tests { t.Run(test.Path, func(t *testing.T) { response := s.Request(test.Method, "http://example.com"+test.Path, strings.NewReader(test.Body)) assert.Equal(t, response.Status(), test.Status) assert.Equal(t, string(response.Body()), test.Response) }) } } func TestString(t *testing.T) { s := web.NewServer() s.Get("/", func(ctx web.Context) error { return ctx.String("Hello") }) response := s.Request("GET", "/", nil) assert.Equal(t, response.Status(), 200) assert.DeepEqual(t, response.Body(), []byte("Hello")) }