2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-03-30 16:38:53 +02:00
|
|
|
"bytes"
|
2018-04-03 13:09:21 +02:00
|
|
|
stdContext "context"
|
2020-12-29 11:46:09 +02:00
|
|
|
"crypto/tls"
|
2018-02-21 20:44:17 +02:00
|
|
|
"errors"
|
2020-11-05 06:15:32 +02:00
|
|
|
"fmt"
|
2022-09-14 07:40:39 +02:00
|
|
|
"io"
|
2020-12-17 03:07:41 +02:00
|
|
|
"net"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2021-04-06 09:05:33 +02:00
|
|
|
"net/url"
|
2020-12-29 11:46:09 +02:00
|
|
|
"os"
|
2015-05-30 01:00:02 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2018-02-21 20:44:17 +02:00
|
|
|
"testing"
|
2017-01-18 22:17:44 +02:00
|
|
|
"time"
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-15 19:51:54 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-29 00:46:00 +02:00
|
|
|
"golang.org/x/net/http2"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
user struct {
|
2021-05-25 14:50:49 +02:00
|
|
|
ID int `json:"id" xml:"id" form:"id" query:"id" param:"id" header:"id"`
|
|
|
|
Name string `json:"name" xml:"name" form:"name" query:"name" param:"name" header:"name"`
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
const (
|
2018-09-28 19:41:13 +02:00
|
|
|
userJSON = `{"id":1,"name":"Jon Snow"}`
|
2021-04-06 09:05:33 +02:00
|
|
|
usersJSON = `[{"id":1,"name":"Jon Snow"}]`
|
2018-09-28 19:41:13 +02:00
|
|
|
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
|
|
|
|
userForm = `id=1&name=Jon Snow`
|
|
|
|
invalidContent = "invalid content"
|
|
|
|
userJSONInvalidType = `{"id":"1","name":"Jon Snow"}`
|
|
|
|
userXMLConvertNumberError = `<user><id>Number one</id><name>Jon Snow</name></user>`
|
|
|
|
userXMLUnsupportedTypeError = `<user><>Number one</><name>Jon Snow</name></user>`
|
2016-05-01 21:38:51 +02:00
|
|
|
)
|
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
const userJSONPretty = `{
|
2017-04-28 06:41:46 +02:00
|
|
|
"id": 1,
|
|
|
|
"name": "Jon Snow"
|
2016-12-09 21:13:55 +02:00
|
|
|
}`
|
|
|
|
|
|
|
|
const userXMLPretty = `<user>
|
2017-04-28 06:41:46 +02:00
|
|
|
<id>1</id>
|
|
|
|
<name>Jon Snow</name>
|
2016-12-09 21:13:55 +02:00
|
|
|
</user>`
|
|
|
|
|
2021-04-06 09:05:33 +02:00
|
|
|
var dummyQuery = url.Values{"dummy": []string{"useless"}}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func TestEcho(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2015-05-31 00:20:36 +02:00
|
|
|
|
|
|
|
// Router
|
|
|
|
assert.NotNil(t, e.Router())
|
|
|
|
|
|
|
|
// DefaultHTTPErrorHandler
|
|
|
|
e.DefaultHTTPErrorHandler(errors.New("error"), c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
2015-05-31 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 15:14:15 +02:00
|
|
|
func TestEchoStatic(t *testing.T) {
|
2020-12-13 15:49:11 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenPrefix string
|
|
|
|
givenRoot string
|
|
|
|
whenURL string
|
|
|
|
expectStatus int
|
|
|
|
expectHeaderLocation string
|
|
|
|
expectBodyStartsWith string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
givenPrefix: "/images",
|
|
|
|
givenRoot: "_fixture/images",
|
|
|
|
whenURL: "/images/walle.png",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: string([]byte{0x89, 0x50, 0x4e, 0x47}),
|
|
|
|
},
|
2022-03-13 15:05:12 +02:00
|
|
|
{
|
|
|
|
name: "ok with relative path for root points to directory",
|
|
|
|
givenPrefix: "/images",
|
|
|
|
givenRoot: "./_fixture/images",
|
|
|
|
whenURL: "/images/walle.png",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: string([]byte{0x89, 0x50, 0x4e, 0x47}),
|
|
|
|
},
|
2020-12-13 15:49:11 +02:00
|
|
|
{
|
|
|
|
name: "No file",
|
|
|
|
givenPrefix: "/images",
|
|
|
|
givenRoot: "_fixture/scripts",
|
|
|
|
whenURL: "/images/bolt.png",
|
|
|
|
expectStatus: http.StatusNotFound,
|
|
|
|
expectBodyStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Directory",
|
|
|
|
givenPrefix: "/images",
|
|
|
|
givenRoot: "_fixture/images",
|
|
|
|
whenURL: "/images/",
|
|
|
|
expectStatus: http.StatusNotFound,
|
|
|
|
expectBodyStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Directory Redirect",
|
|
|
|
givenPrefix: "/",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/folder",
|
|
|
|
expectStatus: http.StatusMovedPermanently,
|
2020-12-16 03:37:26 +02:00
|
|
|
expectHeaderLocation: "/folder/",
|
2020-12-16 03:21:26 +02:00
|
|
|
expectBodyStartsWith: "",
|
|
|
|
},
|
2021-01-03 01:25:29 +02:00
|
|
|
{
|
|
|
|
name: "Directory Redirect with non-root path",
|
|
|
|
givenPrefix: "/static",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/static",
|
|
|
|
expectStatus: http.StatusMovedPermanently,
|
|
|
|
expectHeaderLocation: "/static/",
|
|
|
|
expectBodyStartsWith: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Prefixed directory 404 (request URL without slash)",
|
|
|
|
givenPrefix: "/folder/", // trailing slash will intentionally not match "/folder"
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/folder", // no trailing slash
|
|
|
|
expectStatus: http.StatusNotFound,
|
|
|
|
expectBodyStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Prefixed directory redirect (without slash redirect to slash)",
|
|
|
|
givenPrefix: "/folder", // no trailing slash shall match /folder and /folder/*
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/folder", // no trailing slash
|
|
|
|
expectStatus: http.StatusMovedPermanently,
|
|
|
|
expectHeaderLocation: "/folder/",
|
|
|
|
expectBodyStartsWith: "",
|
|
|
|
},
|
2020-12-13 15:49:11 +02:00
|
|
|
{
|
|
|
|
name: "Directory with index.html",
|
|
|
|
givenPrefix: "/",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: "<!doctype html>",
|
|
|
|
},
|
2021-01-03 01:25:29 +02:00
|
|
|
{
|
|
|
|
name: "Prefixed directory with index.html (prefix ending with slash)",
|
|
|
|
givenPrefix: "/assets/",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/assets/",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: "<!doctype html>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Prefixed directory with index.html (prefix ending without slash)",
|
|
|
|
givenPrefix: "/assets",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/assets/",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: "<!doctype html>",
|
|
|
|
},
|
2020-12-13 15:49:11 +02:00
|
|
|
{
|
|
|
|
name: "Sub-directory with index.html",
|
|
|
|
givenPrefix: "/",
|
|
|
|
givenRoot: "_fixture",
|
|
|
|
whenURL: "/folder/",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectBodyStartsWith: "<!doctype html>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "do not allow directory traversal (backslash - windows separator)",
|
|
|
|
givenPrefix: "/",
|
|
|
|
givenRoot: "_fixture/",
|
|
|
|
whenURL: `/..\\middleware/basic_auth.go`,
|
|
|
|
expectStatus: http.StatusNotFound,
|
|
|
|
expectBodyStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "do not allow directory traversal (slash - unix separator)",
|
|
|
|
givenPrefix: "/",
|
|
|
|
givenRoot: "_fixture/",
|
|
|
|
whenURL: `/../middleware/basic_auth.go`,
|
|
|
|
expectStatus: http.StatusNotFound,
|
|
|
|
expectBodyStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
}
|
2016-03-12 15:14:15 +02:00
|
|
|
|
2020-12-13 15:49:11 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.Static(tc.givenPrefix, tc.givenRoot)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
|
|
body := rec.Body.String()
|
|
|
|
if tc.expectBodyStartsWith != "" {
|
|
|
|
assert.True(t, strings.HasPrefix(body, tc.expectBodyStartsWith))
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "", body)
|
|
|
|
}
|
2020-08-28 02:53:48 +02:00
|
|
|
|
2020-12-13 15:49:11 +02:00
|
|
|
if tc.expectHeaderLocation != "" {
|
|
|
|
assert.Equal(t, tc.expectHeaderLocation, rec.Result().Header["Location"][0])
|
|
|
|
} else {
|
|
|
|
_, ok := rec.Result().Header["Location"]
|
|
|
|
assert.False(t, ok)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 01:25:29 +02:00
|
|
|
func TestEchoStaticRedirectIndex(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
// HandlerFunc
|
|
|
|
e.Static("/static", "_fixture")
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
2022-01-08 22:41:34 +02:00
|
|
|
errCh <- e.Start(":0")
|
2021-01-03 01:25:29 +02:00
|
|
|
}()
|
|
|
|
|
2022-01-08 22:41:34 +02:00
|
|
|
err := waitForServerStart(e, errCh, false)
|
|
|
|
assert.NoError(t, err)
|
2021-01-03 01:25:29 +02:00
|
|
|
|
2022-01-08 22:41:34 +02:00
|
|
|
addr := e.ListenerAddr().String()
|
|
|
|
if resp, err := http.Get("http://" + addr + "/static"); err == nil { // http.Get follows redirects by default
|
2022-09-14 07:40:39 +02:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
|
|
|
}(resp.Body)
|
2022-01-08 22:41:34 +02:00
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
2021-01-03 01:25:29 +02:00
|
|
|
|
2022-11-21 14:29:43 +02:00
|
|
|
if body, err := io.ReadAll(resp.Body); err == nil {
|
2022-01-08 22:41:34 +02:00
|
|
|
assert.Equal(t, true, strings.HasPrefix(string(body), "<!doctype html>"))
|
2021-01-03 01:25:29 +02:00
|
|
|
} else {
|
2022-01-08 22:41:34 +02:00
|
|
|
assert.Fail(t, err.Error())
|
2021-01-03 01:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-01-08 22:41:34 +02:00
|
|
|
assert.NoError(t, err)
|
2021-01-03 01:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:14:15 +02:00
|
|
|
func TestEchoFile(t *testing.T) {
|
2022-03-13 15:05:12 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenPath string
|
|
|
|
givenFile string
|
|
|
|
whenPath string
|
|
|
|
expectCode int
|
|
|
|
expectStartsWith string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
givenPath: "/walle",
|
|
|
|
givenFile: "_fixture/images/walle.png",
|
|
|
|
whenPath: "/walle",
|
|
|
|
expectCode: http.StatusOK,
|
|
|
|
expectStartsWith: string([]byte{0x89, 0x50, 0x4e}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok with relative path",
|
|
|
|
givenPath: "/",
|
|
|
|
givenFile: "./go.mod",
|
|
|
|
whenPath: "/",
|
|
|
|
expectCode: http.StatusOK,
|
|
|
|
expectStartsWith: "module github.com/labstack/echo/v",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok file does not exist",
|
|
|
|
givenPath: "/",
|
|
|
|
givenFile: "./this-file-does-not-exist",
|
|
|
|
whenPath: "/",
|
|
|
|
expectCode: http.StatusNotFound,
|
|
|
|
expectStartsWith: "{\"message\":\"Not Found\"}\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New() // we are using echo.defaultFS instance
|
|
|
|
e.File(tc.givenPath, tc.givenFile)
|
|
|
|
|
|
|
|
c, b := request(http.MethodGet, tc.whenPath, e)
|
|
|
|
assert.Equal(t, tc.expectCode, c)
|
|
|
|
|
|
|
|
if len(b) > len(tc.expectStartsWith) {
|
|
|
|
b = b[:len(tc.expectStartsWith)]
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectStartsWith, b)
|
|
|
|
})
|
|
|
|
}
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2015-03-30 05:44:55 +02:00
|
|
|
func TestEchoMiddleware(t *testing.T) {
|
2015-03-30 08:35:08 +02:00
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2015-03-30 05:44:55 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Pre(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
assert.Empty(t, c.Path())
|
|
|
|
buf.WriteString("-1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("2")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("3")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2015-03-30 08:35:08 +02:00
|
|
|
|
2015-03-30 05:44:55 +02:00
|
|
|
// Route
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-03-30 05:44:55 +02:00
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
c, b := request(http.MethodGet, "/", e)
|
2016-03-17 22:24:52 +02:00
|
|
|
assert.Equal(t, "-1123", buf.String())
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Equal(t, http.StatusOK, c)
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, "OK", b)
|
2016-03-17 22:24:52 +02:00
|
|
|
}
|
2015-05-31 00:20:36 +02:00
|
|
|
|
2016-03-17 22:24:52 +02:00
|
|
|
func TestEchoMiddlewareError(t *testing.T) {
|
|
|
|
e := New()
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return errors.New("error")
|
|
|
|
}
|
|
|
|
})
|
2016-07-05 17:43:46 +02:00
|
|
|
e.GET("/", NotFoundHandler)
|
2018-10-14 17:16:58 +02:00
|
|
|
c, _ := request(http.MethodGet, "/", e)
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoHandler(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
2015-04-29 20:22:07 +02:00
|
|
|
// HandlerFunc
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/ok", func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
c, b := request(http.MethodGet, "/ok", e)
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, "OK", b)
|
2015-03-30 05:44:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
func TestEchoWrapHandler(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 14:31:48 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-09-14 07:40:39 +02:00
|
|
|
_, err := w.Write([]byte("test"))
|
|
|
|
if err != nil {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
2016-09-23 14:31:48 +02:00
|
|
|
}))
|
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoWrapMiddleware(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 14:31:48 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := WrapMiddleware(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
buf.Write([]byte("mw"))
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
h := mw(func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, "mw", buf.String())
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "OK", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 01:00:02 +02:00
|
|
|
func TestEchoConnect(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodConnect, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoDelete(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodDelete, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoGet(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodGet, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoHead(t *testing.T) {
|
2015-04-06 00:30:03 +02:00
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodHead, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoOptions(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodOptions, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPatch(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodPatch, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPost(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodPost, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPut(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodPut, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoTrace(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
testMethod(t, http.MethodTrace, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 05:36:15 +02:00
|
|
|
func TestEchoAny(t *testing.T) { // JFC
|
|
|
|
e := New()
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Any("/", func(c Context) error {
|
2015-08-26 05:36:15 +02:00
|
|
|
return c.String(http.StatusOK, "Any")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoMatch(t *testing.T) { // JFC
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
e.Match([]string{http.MethodGet, http.MethodPost}, "/", func(c Context) error {
|
2015-08-26 05:36:15 +02:00
|
|
|
return c.String(http.StatusOK, "Match")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 07:12:41 +02:00
|
|
|
func TestEchoURL(t *testing.T) {
|
|
|
|
e := New()
|
2016-04-02 23:19:39 +02:00
|
|
|
static := func(Context) error { return nil }
|
|
|
|
getUser := func(Context) error { return nil }
|
2020-11-24 05:24:27 +02:00
|
|
|
getAny := func(Context) error { return nil }
|
2016-04-02 23:19:39 +02:00
|
|
|
getFile := func(Context) error { return nil }
|
2015-05-31 18:37:26 +02:00
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/static/file", static)
|
|
|
|
e.GET("/users/:id", getUser)
|
2020-11-24 05:24:27 +02:00
|
|
|
e.GET("/documents/*", getAny)
|
2015-05-31 18:37:26 +02:00
|
|
|
g := e.Group("/group")
|
2016-10-13 22:51:37 +02:00
|
|
|
g.GET("/users/:uid/files/:fid", getFile)
|
2015-05-31 18:37:26 +02:00
|
|
|
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, "/static/file", e.URL(static))
|
|
|
|
assert.Equal(t, "/users/:id", e.URL(getUser))
|
|
|
|
assert.Equal(t, "/users/1", e.URL(getUser, "1"))
|
|
|
|
assert.Equal(t, "/users/1", e.URL(getUser, "1"))
|
|
|
|
assert.Equal(t, "/documents/foo.txt", e.URL(getAny, "foo.txt"))
|
|
|
|
assert.Equal(t, "/documents/*", e.URL(getAny))
|
|
|
|
assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1"))
|
|
|
|
assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1"))
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
func TestEchoRoutes(t *testing.T) {
|
|
|
|
e := New()
|
2017-06-20 17:58:53 +02:00
|
|
|
routes := []*Route{
|
2018-10-14 17:16:58 +02:00
|
|
|
{http.MethodGet, "/users/:user/events", ""},
|
|
|
|
{http.MethodGet, "/users/:user/events/public", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/refs", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/tags", ""},
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
for _, r := range routes {
|
2017-07-04 04:10:58 +02:00
|
|
|
e.Add(r.Method, r.Path, func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 17:58:53 +02:00
|
|
|
if assert.Equal(t, len(routes), len(e.Routes())) {
|
|
|
|
for _, r := range e.Routes() {
|
|
|
|
found := false
|
|
|
|
for _, rr := range routes {
|
|
|
|
if r.Method == rr.Method && r.Path == rr.Path {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Route %s %s not found", r.Method, r.Path)
|
2016-06-01 14:35:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:35:19 +02:00
|
|
|
func TestEchoRoutesHandleAdditionalHosts(t *testing.T) {
|
2021-06-25 22:56:07 +02:00
|
|
|
e := New()
|
2022-11-12 18:35:19 +02:00
|
|
|
domain2Router := e.Host("domain2.router.com")
|
2021-06-25 22:56:07 +02:00
|
|
|
routes := []*Route{
|
|
|
|
{http.MethodGet, "/users/:user/events", ""},
|
|
|
|
{http.MethodGet, "/users/:user/events/public", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/refs", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/tags", ""},
|
|
|
|
}
|
|
|
|
for _, r := range routes {
|
2022-11-12 18:35:19 +02:00
|
|
|
domain2Router.Add(r.Method, r.Path, func(c Context) error {
|
2021-06-25 22:56:07 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
}
|
2022-11-12 18:35:19 +02:00
|
|
|
e.Add(http.MethodGet, "/api", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
2021-06-25 22:56:07 +02:00
|
|
|
|
2022-11-12 18:35:19 +02:00
|
|
|
domain2Routes := e.Routers()["domain2.router.com"].Routes()
|
|
|
|
|
|
|
|
assert.Len(t, domain2Routes, len(routes))
|
|
|
|
for _, r := range domain2Routes {
|
|
|
|
found := false
|
|
|
|
for _, rr := range routes {
|
|
|
|
if r.Method == rr.Method && r.Path == rr.Path {
|
|
|
|
found = true
|
|
|
|
break
|
2021-06-25 22:56:07 +02:00
|
|
|
}
|
2022-11-12 18:35:19 +02:00
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Route %s %s not found", r.Method, r.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoRoutesHandleDefaultHost(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
routes := []*Route{
|
|
|
|
{http.MethodGet, "/users/:user/events", ""},
|
|
|
|
{http.MethodGet, "/users/:user/events/public", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/refs", ""},
|
|
|
|
{http.MethodPost, "/repos/:owner/:repo/git/tags", ""},
|
|
|
|
}
|
|
|
|
for _, r := range routes {
|
|
|
|
e.Add(r.Method, r.Path, func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
e.Host("subdomain.mysite.site").Add(http.MethodGet, "/api", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
defaultRouterRoutes := e.Routes()
|
|
|
|
assert.Len(t, defaultRouterRoutes, len(routes))
|
|
|
|
for _, r := range defaultRouterRoutes {
|
|
|
|
found := false
|
|
|
|
for _, rr := range routes {
|
|
|
|
if r.Method == rr.Method && r.Path == rr.Path {
|
|
|
|
found = true
|
|
|
|
break
|
2021-06-25 22:56:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-12 18:35:19 +02:00
|
|
|
if !found {
|
|
|
|
t.Errorf("Route %s %s not found", r.Method, r.Path)
|
|
|
|
}
|
2021-06-25 22:56:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 03:01:02 +02:00
|
|
|
func TestEchoServeHTTPPathEncoding(t *testing.T) {
|
2017-06-07 17:23:43 +02:00
|
|
|
e := New()
|
2021-03-08 03:01:02 +02:00
|
|
|
e.GET("/with/slash", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "/with/slash")
|
|
|
|
})
|
2017-06-07 17:23:43 +02:00
|
|
|
e.GET("/:id", func(c Context) error {
|
2021-03-08 03:01:02 +02:00
|
|
|
return c.String(http.StatusOK, c.Param("id"))
|
2017-06-07 17:23:43 +02:00
|
|
|
})
|
2021-03-08 03:01:02 +02:00
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenURL string
|
|
|
|
expectURL string
|
|
|
|
expectStatus int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "url with encoding is not decoded for routing",
|
|
|
|
whenURL: "/with%2Fslash",
|
|
|
|
expectURL: "with%2Fslash", // `%2F` is not decoded to `/` for routing
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "url without encoding is used as is",
|
|
|
|
whenURL: "/with/slash",
|
|
|
|
expectURL: "/with/slash",
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
|
|
assert.Equal(t, tc.expectURL, rec.Body.String())
|
|
|
|
})
|
|
|
|
}
|
2017-06-07 17:23:43 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 22:56:07 +02:00
|
|
|
func TestEchoHost(t *testing.T) {
|
|
|
|
okHandler := func(c Context) error { return c.String(http.StatusOK, http.StatusText(http.StatusOK)) }
|
|
|
|
teapotHandler := func(c Context) error { return c.String(http.StatusTeapot, http.StatusText(http.StatusTeapot)) }
|
|
|
|
acceptHandler := func(c Context) error { return c.String(http.StatusAccepted, http.StatusText(http.StatusAccepted)) }
|
|
|
|
teapotMiddleware := MiddlewareFunc(func(next HandlerFunc) HandlerFunc { return teapotHandler })
|
|
|
|
|
|
|
|
e := New()
|
|
|
|
e.GET("/", acceptHandler)
|
|
|
|
e.GET("/foo", acceptHandler)
|
|
|
|
|
|
|
|
ok := e.Host("ok.com")
|
|
|
|
ok.GET("/", okHandler)
|
|
|
|
ok.GET("/foo", okHandler)
|
|
|
|
|
|
|
|
teapot := e.Host("teapot.com")
|
|
|
|
teapot.GET("/", teapotHandler)
|
|
|
|
teapot.GET("/foo", teapotHandler)
|
|
|
|
|
|
|
|
middle := e.Host("middleware.com", teapotMiddleware)
|
|
|
|
middle.GET("/", okHandler)
|
|
|
|
middle.GET("/foo", okHandler)
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenHost string
|
|
|
|
whenPath string
|
|
|
|
expectBody string
|
|
|
|
expectStatus int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No Host Root",
|
|
|
|
whenHost: "",
|
|
|
|
whenPath: "/",
|
|
|
|
expectBody: http.StatusText(http.StatusAccepted),
|
|
|
|
expectStatus: http.StatusAccepted,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No Host Foo",
|
|
|
|
whenHost: "",
|
|
|
|
whenPath: "/foo",
|
|
|
|
expectBody: http.StatusText(http.StatusAccepted),
|
|
|
|
expectStatus: http.StatusAccepted,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK Host Root",
|
|
|
|
whenHost: "ok.com",
|
|
|
|
whenPath: "/",
|
|
|
|
expectBody: http.StatusText(http.StatusOK),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK Host Foo",
|
|
|
|
whenHost: "ok.com",
|
|
|
|
whenPath: "/foo",
|
|
|
|
expectBody: http.StatusText(http.StatusOK),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Teapot Host Root",
|
|
|
|
whenHost: "teapot.com",
|
|
|
|
whenPath: "/",
|
|
|
|
expectBody: http.StatusText(http.StatusTeapot),
|
|
|
|
expectStatus: http.StatusTeapot,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Teapot Host Foo",
|
|
|
|
whenHost: "teapot.com",
|
|
|
|
whenPath: "/foo",
|
|
|
|
expectBody: http.StatusText(http.StatusTeapot),
|
|
|
|
expectStatus: http.StatusTeapot,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Middleware Host",
|
|
|
|
whenHost: "middleware.com",
|
|
|
|
whenPath: "/",
|
|
|
|
expectBody: http.StatusText(http.StatusTeapot),
|
|
|
|
expectStatus: http.StatusTeapot,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Middleware Host Foo",
|
|
|
|
whenHost: "middleware.com",
|
|
|
|
whenPath: "/foo",
|
|
|
|
expectBody: http.StatusText(http.StatusTeapot),
|
|
|
|
expectStatus: http.StatusTeapot,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.whenPath, nil)
|
|
|
|
req.Host = tc.whenHost
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
|
|
assert.Equal(t, tc.expectBody, rec.Body.String())
|
2021-06-25 22:56:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
func TestEchoGroup(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(c Context) error {
|
2016-01-29 09:46:11 +02:00
|
|
|
buf.WriteString("0")
|
2016-09-23 14:31:48 +02:00
|
|
|
return next(c)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-02-16 03:12:15 +02:00
|
|
|
}))
|
2016-04-02 23:19:39 +02:00
|
|
|
h := func(c Context) error {
|
2016-01-29 09:46:11 +02:00
|
|
|
return c.NoContent(http.StatusOK)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
|
|
|
|
//--------
|
|
|
|
// Routes
|
|
|
|
//--------
|
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/users", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
|
|
|
// Group
|
|
|
|
g1 := e.Group("/group1")
|
2016-09-23 14:31:48 +02:00
|
|
|
g1.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-10-13 22:51:37 +02:00
|
|
|
g1.GET("", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
2016-03-17 05:15:38 +02:00
|
|
|
// Nested groups with middleware
|
2016-02-15 18:11:29 +02:00
|
|
|
g2 := e.Group("/group2")
|
2016-09-23 14:31:48 +02:00
|
|
|
g2.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("2")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-02-15 18:11:29 +02:00
|
|
|
g3 := g2.Group("/group3")
|
2016-09-23 14:31:48 +02:00
|
|
|
g3.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("3")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-10-13 22:51:37 +02:00
|
|
|
g3.GET("", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
request(http.MethodGet, "/users", e)
|
2015-06-01 09:07:53 +02:00
|
|
|
assert.Equal(t, "0", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
2018-10-14 17:16:58 +02:00
|
|
|
request(http.MethodGet, "/group1", e)
|
2015-06-01 09:07:53 +02:00
|
|
|
assert.Equal(t, "01", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
2018-10-14 17:16:58 +02:00
|
|
|
request(http.MethodGet, "/group2/group3", e)
|
2016-03-17 05:15:38 +02:00
|
|
|
assert.Equal(t, "023", buf.String())
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
func TestEchoNotFound(t *testing.T) {
|
2015-03-31 05:54:38 +02:00
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/files", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
2015-03-31 05:54:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 20:53:41 +02:00
|
|
|
func TestEcho_RouteNotFound(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenURL string
|
|
|
|
expectRoute interface{}
|
|
|
|
expectCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "404, route to static not found handler /a/c/xx",
|
|
|
|
whenURL: "/a/c/xx",
|
|
|
|
expectRoute: "GET /a/c/xx",
|
|
|
|
expectCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "404, route to path param not found handler /a/:file",
|
|
|
|
whenURL: "/a/echo.exe",
|
|
|
|
expectRoute: "GET /a/:file",
|
|
|
|
expectCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "404, route to any not found handler /*",
|
|
|
|
whenURL: "/b/echo.exe",
|
|
|
|
expectRoute: "GET /*",
|
|
|
|
expectCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "200, route /a/c/df to /a/c/df",
|
|
|
|
whenURL: "/a/c/df",
|
|
|
|
expectRoute: "GET /a/c/df",
|
|
|
|
expectCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
okHandler := func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, c.Request().Method+" "+c.Path())
|
|
|
|
}
|
|
|
|
notFoundHandler := func(c Context) error {
|
|
|
|
return c.String(http.StatusNotFound, c.Request().Method+" "+c.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
e.GET("/", okHandler)
|
|
|
|
e.GET("/a/c/df", okHandler)
|
|
|
|
e.GET("/a/b*", okHandler)
|
|
|
|
e.PUT("/*", okHandler)
|
|
|
|
|
|
|
|
e.RouteNotFound("/a/c/xx", notFoundHandler) // static
|
|
|
|
e.RouteNotFound("/a/:file", notFoundHandler) // param
|
|
|
|
e.RouteNotFound("/*", notFoundHandler) // any
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectCode, rec.Code)
|
|
|
|
assert.Equal(t, tc.expectRoute, rec.Body.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 15:48:33 +02:00
|
|
|
func TestEchoMethodNotAllowed(t *testing.T) {
|
2015-11-04 02:26:20 +02:00
|
|
|
e := New()
|
2021-12-04 20:02:11 +02:00
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c Context) error {
|
2015-11-04 02:26:20 +02:00
|
|
|
return c.String(http.StatusOK, "Echo!")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2021-12-04 20:02:11 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
|
2021-12-04 20:02:11 +02:00
|
|
|
assert.Equal(t, "OPTIONS, GET", rec.Header().Get(HeaderAllow))
|
2015-07-24 21:03:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 01:54:24 +02:00
|
|
|
func TestEchoContext(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.AcquireContext()
|
2016-10-11 02:31:26 +02:00
|
|
|
assert.IsType(t, new(context), c)
|
2016-06-07 01:54:24 +02:00
|
|
|
e.ReleaseContext(c)
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func waitForServerStart(e *Echo, errChan <-chan error, isTLS bool) error {
|
|
|
|
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 200*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(5 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-ticker.C:
|
|
|
|
var addr net.Addr
|
|
|
|
if isTLS {
|
|
|
|
addr = e.TLSListenerAddr()
|
|
|
|
} else {
|
|
|
|
addr = e.ListenerAddr()
|
|
|
|
}
|
|
|
|
if addr != nil && strings.Contains(addr.String(), ":") {
|
|
|
|
return nil // was started
|
|
|
|
}
|
|
|
|
case err := <-errChan:
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 02:48:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func TestEchoStart(t *testing.T) {
|
2016-09-25 02:48:40 +02:00
|
|
|
e := New()
|
2020-12-29 11:46:09 +02:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
2016-09-25 02:48:40 +02:00
|
|
|
go func() {
|
2020-12-29 11:46:09 +02:00
|
|
|
err := e.Start(":0")
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
2018-03-01 19:27:31 +02:00
|
|
|
}
|
2016-09-25 02:48:40 +02:00
|
|
|
}()
|
2018-03-01 19:27:31 +02:00
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err := waitForServerStart(e, errChan, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcho_StartTLS(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
certFile string
|
|
|
|
keyFile string
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
addr: ":0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid certFile",
|
|
|
|
addr: ":0",
|
|
|
|
certFile: "not existing",
|
|
|
|
expectError: "open not existing: no such file or directory",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid keyFile",
|
|
|
|
addr: ":0",
|
|
|
|
keyFile: "not existing",
|
|
|
|
expectError: "open not existing: no such file or directory",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, failed to create cert out of certFile and keyFile",
|
|
|
|
addr: ":0",
|
|
|
|
keyFile: "_fixture/certs/cert.pem", // we are passing cert instead of key
|
|
|
|
expectError: "tls: found a certificate rather than a key in the PEM for the private key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid tls address",
|
|
|
|
addr: "nope",
|
|
|
|
expectError: "listen tcp: address nope: missing port in address",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
certFile := "_fixture/certs/cert.pem"
|
|
|
|
if tc.certFile != "" {
|
|
|
|
certFile = tc.certFile
|
|
|
|
}
|
|
|
|
keyFile := "_fixture/certs/key.pem"
|
|
|
|
if tc.keyFile != "" {
|
|
|
|
keyFile = tc.keyFile
|
|
|
|
}
|
|
|
|
|
|
|
|
err := e.StartTLS(tc.addr, certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errChan, true)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
if _, ok := err.(*os.PathError); ok {
|
|
|
|
assert.Error(t, err) // error messages for unix and windows are different. so test only error type here
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
})
|
|
|
|
}
|
2018-03-01 19:27:31 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 20:13:04 +02:00
|
|
|
func TestEchoStartTLSAndStart(t *testing.T) {
|
|
|
|
// We test if Echo and listeners work correctly when Echo is simultaneously attached to HTTP and HTTPS server
|
|
|
|
e := New()
|
|
|
|
e.GET("/", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
errTLSChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
certFile := "_fixture/certs/cert.pem"
|
|
|
|
keyFile := "_fixture/certs/key.pem"
|
|
|
|
err := e.StartTLS("localhost:", certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
errTLSChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errTLSChan, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
if err := e.Shutdown(stdContext.Background()); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// check if HTTPS works (note: we are using self signed certs so InsecureSkipVerify=true)
|
|
|
|
client := &http.Client{Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}}
|
|
|
|
res, err := client.Get("https://" + e.TLSListenerAddr().String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := e.Start("localhost:")
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = waitForServerStart(e, errChan, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// now we are serving both HTTPS and HTTP listeners. see if HTTP works in addition to HTTPS
|
|
|
|
res, err = http.Get("http://" + e.ListenerAddr().String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
// see if HTTPS works after HTTP listener is also added
|
|
|
|
res, err = client.Get("https://" + e.TLSListenerAddr().String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:51:54 +02:00
|
|
|
func TestEchoStartTLSByteString(t *testing.T) {
|
2022-11-21 14:29:43 +02:00
|
|
|
cert, err := os.ReadFile("_fixture/certs/cert.pem")
|
2019-02-15 19:51:54 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-21 14:29:43 +02:00
|
|
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
2019-02-15 19:51:54 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
cert interface{}
|
|
|
|
key interface{}
|
|
|
|
expectedErr error
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cert: "_fixture/certs/cert.pem",
|
|
|
|
key: "_fixture/certs/key.pem",
|
|
|
|
expectedErr: nil,
|
|
|
|
name: `ValidCertAndKeyFilePath`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cert: cert,
|
|
|
|
key: key,
|
|
|
|
expectedErr: nil,
|
|
|
|
name: `ValidCertAndKeyByteString`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cert: cert,
|
|
|
|
key: 1,
|
|
|
|
expectedErr: ErrInvalidCertOrKeyType,
|
|
|
|
name: `InvalidKeyType`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cert: 0,
|
|
|
|
key: key,
|
|
|
|
expectedErr: ErrInvalidCertOrKeyType,
|
|
|
|
name: `InvalidCertType`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cert: 0,
|
|
|
|
key: 1,
|
|
|
|
expectedErr: ErrInvalidCertOrKeyType,
|
|
|
|
name: `InvalidCertAndKeyTypes`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.HideBanner = true
|
|
|
|
|
2022-02-23 09:22:20 +02:00
|
|
|
errChan := make(chan error)
|
2020-12-29 11:46:09 +02:00
|
|
|
|
2019-02-15 19:51:54 +02:00
|
|
|
go func() {
|
2020-12-29 11:46:09 +02:00
|
|
|
errChan <- e.StartTLS(":0", test.cert, test.key)
|
2019-02-15 19:51:54 +02:00
|
|
|
}()
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err := waitForServerStart(e, errChan, true)
|
|
|
|
if test.expectedErr != nil {
|
|
|
|
assert.EqualError(t, err, test.expectedErr.Error())
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
2019-02-15 19:51:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func TestEcho_StartAutoTLS(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
addr: ":0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid address",
|
|
|
|
addr: "nope",
|
|
|
|
expectError: "listen tcp: address nope: missing port in address",
|
|
|
|
},
|
|
|
|
}
|
2018-03-01 19:27:31 +02:00
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
2022-02-23 09:22:20 +02:00
|
|
|
errChan := make(chan error)
|
2020-12-29 11:46:09 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
errChan <- e.StartAutoTLS(tc.addr)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errChan, true)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-03-01 19:27:31 +02:00
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
})
|
2018-03-01 19:27:31 +02:00
|
|
|
}
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func TestEcho_StartH2CServer(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
addr: ":0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid address",
|
|
|
|
addr: "nope",
|
|
|
|
expectError: "listen tcp: address nope: missing port in address",
|
|
|
|
},
|
|
|
|
}
|
2020-01-29 00:46:00 +02:00
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.Debug = true
|
|
|
|
h2s := &http2.Server{}
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := e.StartH2CServer(tc.addr, h2s)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errChan, false)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
})
|
|
|
|
}
|
2020-01-29 00:46:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func testMethod(t *testing.T, method, path string, e *Echo) {
|
|
|
|
p := reflect.ValueOf(path)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := reflect.ValueOf(func(c Context) error {
|
2015-07-24 21:03:36 +02:00
|
|
|
return c.String(http.StatusOK, method)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-05-31 00:20:36 +02:00
|
|
|
i := interface{}(e)
|
2016-09-23 07:53:44 +02:00
|
|
|
reflect.ValueOf(i).MethodByName(method).Call([]reflect.Value{p, h})
|
2015-05-31 00:20:36 +02:00
|
|
|
_, body := request(method, path, e)
|
2017-06-20 17:58:53 +02:00
|
|
|
assert.Equal(t, method, body)
|
2015-05-31 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func request(method, path string, e *Echo) (int, string) {
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(method, path, nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
return rec.Code, rec.Body.String()
|
2016-04-27 14:57:35 +02:00
|
|
|
}
|
2017-06-20 17:58:53 +02:00
|
|
|
|
|
|
|
func TestHTTPError(t *testing.T) {
|
2020-03-05 16:36:43 +02:00
|
|
|
t.Run("non-internal", func(t *testing.T) {
|
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
|
|
|
|
})
|
2022-11-19 22:05:53 +02:00
|
|
|
|
|
|
|
t.Run("internal and SetInternal", func(t *testing.T) {
|
2020-03-05 16:36:43 +02:00
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
err.SetInternal(errors.New("internal error"))
|
|
|
|
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
|
2017-06-20 17:58:53 +02:00
|
|
|
})
|
2022-11-19 22:05:53 +02:00
|
|
|
|
|
|
|
t.Run("internal and WithInternal", func(t *testing.T) {
|
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
err = err.WithInternal(errors.New("internal error"))
|
|
|
|
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
|
|
|
|
})
|
2017-06-20 17:58:53 +02:00
|
|
|
}
|
2018-04-03 13:09:21 +02:00
|
|
|
|
2021-04-16 11:38:12 +02:00
|
|
|
func TestHTTPError_Unwrap(t *testing.T) {
|
|
|
|
t.Run("non-internal", func(t *testing.T) {
|
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Nil(t, errors.Unwrap(err))
|
|
|
|
})
|
2022-11-19 22:05:53 +02:00
|
|
|
|
|
|
|
t.Run("unwrap internal and SetInternal", func(t *testing.T) {
|
2021-04-16 11:38:12 +02:00
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
err.SetInternal(errors.New("internal error"))
|
|
|
|
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
|
2022-11-19 22:05:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unwrap internal and WithInternal", func(t *testing.T) {
|
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
err = err.WithInternal(errors.New("internal error"))
|
|
|
|
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
|
2021-04-16 11:38:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-05 04:37:15 +02:00
|
|
|
func TestDefaultHTTPErrorHandler(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.Debug = true
|
|
|
|
e.Any("/plain", func(c Context) error {
|
2022-09-14 07:40:39 +02:00
|
|
|
return errors.New("an error occurred")
|
2020-11-05 04:37:15 +02:00
|
|
|
})
|
|
|
|
e.Any("/badrequest", func(c Context) error {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, "Invalid request")
|
|
|
|
})
|
|
|
|
e.Any("/servererror", func(c Context) error {
|
|
|
|
return NewHTTPError(http.StatusInternalServerError, map[string]interface{}{
|
|
|
|
"code": 33,
|
|
|
|
"message": "Something bad happened",
|
|
|
|
"error": "stackinfo",
|
|
|
|
})
|
|
|
|
})
|
2021-08-12 12:56:04 +02:00
|
|
|
e.Any("/early-return", func(c Context) error {
|
2022-09-14 07:40:39 +02:00
|
|
|
err := c.String(http.StatusOK, "OK")
|
|
|
|
if err != nil {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
2021-08-12 12:56:04 +02:00
|
|
|
return errors.New("ERROR")
|
|
|
|
})
|
|
|
|
e.GET("/internal-error", func(c Context) error {
|
|
|
|
err := errors.New("internal error message body")
|
|
|
|
return NewHTTPError(http.StatusBadRequest).SetInternal(err)
|
|
|
|
})
|
|
|
|
|
2020-11-05 04:37:15 +02:00
|
|
|
// With Debug=true plain response contains error message
|
|
|
|
c, b := request(http.MethodGet, "/plain", e)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
2022-09-14 07:40:39 +02:00
|
|
|
assert.Equal(t, "{\n \"error\": \"an error occurred\",\n \"message\": \"Internal Server Error\"\n}\n", b)
|
2020-11-05 04:37:15 +02:00
|
|
|
// and special handling for HTTPError
|
|
|
|
c, b = request(http.MethodGet, "/badrequest", e)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, c)
|
|
|
|
assert.Equal(t, "{\n \"error\": \"code=400, message=Invalid request\",\n \"message\": \"Invalid request\"\n}\n", b)
|
|
|
|
// complex errors are serialized to pretty JSON
|
|
|
|
c, b = request(http.MethodGet, "/servererror", e)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
|
|
|
assert.Equal(t, "{\n \"code\": 33,\n \"error\": \"stackinfo\",\n \"message\": \"Something bad happened\"\n}\n", b)
|
2021-08-12 12:56:04 +02:00
|
|
|
// if the body is already set HTTPErrorHandler should not add anything to response body
|
|
|
|
c, b = request(http.MethodGet, "/early-return", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, "OK", b)
|
|
|
|
// internal error should be reflected in the message
|
|
|
|
c, b = request(http.MethodGet, "/internal-error", e)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, c)
|
|
|
|
assert.Equal(t, "{\n \"error\": \"code=400, message=Bad Request, internal=internal error message body\",\n \"message\": \"Bad Request\"\n}\n", b)
|
2020-11-05 04:37:15 +02:00
|
|
|
|
|
|
|
e.Debug = false
|
|
|
|
// With Debug=false the error response is shortened
|
|
|
|
c, b = request(http.MethodGet, "/plain", e)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
|
|
|
assert.Equal(t, "{\"message\":\"Internal Server Error\"}\n", b)
|
|
|
|
c, b = request(http.MethodGet, "/badrequest", e)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, c)
|
|
|
|
assert.Equal(t, "{\"message\":\"Invalid request\"}\n", b)
|
|
|
|
// No difference for error response with non plain string errors
|
|
|
|
c, b = request(http.MethodGet, "/servererror", e)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
|
|
|
assert.Equal(t, "{\"code\":33,\"error\":\"stackinfo\",\"message\":\"Something bad happened\"}\n", b)
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:09:21 +02:00
|
|
|
func TestEchoClose(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(":0")
|
|
|
|
}()
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err := waitForServerStart(e, errCh, false)
|
|
|
|
assert.NoError(t, err)
|
2018-04-03 13:09:21 +02:00
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err = <-errCh
|
2018-04-03 13:09:21 +02:00
|
|
|
assert.Equal(t, err.Error(), "http: Server closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoShutdown(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(":0")
|
|
|
|
}()
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err := waitForServerStart(e, errCh, false)
|
|
|
|
assert.NoError(t, err)
|
2018-04-03 13:09:21 +02:00
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
assert.NoError(t, e.Shutdown(ctx))
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err = <-errCh
|
2018-04-03 13:09:21 +02:00
|
|
|
assert.Equal(t, err.Error(), "http: Server closed")
|
|
|
|
}
|
2020-11-05 06:15:32 +02:00
|
|
|
|
|
|
|
var listenerNetworkTests = []struct {
|
|
|
|
test string
|
|
|
|
network string
|
|
|
|
address string
|
|
|
|
}{
|
|
|
|
{"tcp ipv4 address", "tcp", "127.0.0.1:1323"},
|
|
|
|
{"tcp ipv6 address", "tcp", "[::1]:1323"},
|
|
|
|
{"tcp4 ipv4 address", "tcp4", "127.0.0.1:1323"},
|
|
|
|
{"tcp6 ipv6 address", "tcp6", "[::1]:1323"},
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:07:41 +02:00
|
|
|
func supportsIPv6() bool {
|
|
|
|
addrs, _ := net.InterfaceAddrs()
|
|
|
|
for _, addr := range addrs {
|
|
|
|
// Check if any interface has local IPv6 assigned
|
|
|
|
if strings.Contains(addr.String(), "::1") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-05 06:15:32 +02:00
|
|
|
func TestEchoListenerNetwork(t *testing.T) {
|
2020-12-17 03:07:41 +02:00
|
|
|
hasIPv6 := supportsIPv6()
|
2020-11-05 06:15:32 +02:00
|
|
|
for _, tt := range listenerNetworkTests {
|
2020-12-17 03:07:41 +02:00
|
|
|
if !hasIPv6 && strings.Contains(tt.address, "::") {
|
|
|
|
t.Skip("Skipping testing IPv6 for " + tt.address + ", not available")
|
|
|
|
continue
|
|
|
|
}
|
2020-11-05 06:15:32 +02:00
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.ListenerNetwork = tt.network
|
|
|
|
|
|
|
|
// HandlerFunc
|
|
|
|
e.GET("/ok", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(tt.address)
|
|
|
|
}()
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
err := waitForServerStart(e, errCh, false)
|
|
|
|
assert.NoError(t, err)
|
2020-11-05 06:15:32 +02:00
|
|
|
|
|
|
|
if resp, err := http.Get(fmt.Sprintf("http://%s/ok", tt.address)); err == nil {
|
2022-09-14 07:40:39 +02:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
|
|
|
}(resp.Body)
|
2020-11-05 06:15:32 +02:00
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2022-11-21 14:29:43 +02:00
|
|
|
if body, err := io.ReadAll(resp.Body); err == nil {
|
2020-11-05 06:15:32 +02:00
|
|
|
assert.Equal(t, "OK", string(body))
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoListenerNetworkInvalid(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.ListenerNetwork = "unix"
|
|
|
|
|
|
|
|
// HandlerFunc
|
|
|
|
e.GET("/ok", func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, ErrInvalidListenerNetwork, e.Start(":1323"))
|
|
|
|
}
|
2020-12-10 06:10:13 +02:00
|
|
|
|
2022-11-12 19:38:02 +02:00
|
|
|
func TestEcho_OnAddRouteHandler(t *testing.T) {
|
|
|
|
type rr struct {
|
|
|
|
host string
|
|
|
|
route Route
|
|
|
|
handler HandlerFunc
|
|
|
|
middleware []MiddlewareFunc
|
|
|
|
}
|
|
|
|
dummyHandler := func(Context) error { return nil }
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
added := make([]rr, 0)
|
|
|
|
e.OnAddRouteHandler = func(host string, route Route, handler HandlerFunc, middleware []MiddlewareFunc) {
|
|
|
|
added = append(added, rr{
|
|
|
|
host: host,
|
|
|
|
route: route,
|
|
|
|
handler: handler,
|
|
|
|
middleware: middleware,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
e.GET("/static", NotFoundHandler)
|
|
|
|
e.Host("domain.site").GET("/static/*", dummyHandler, func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Len(t, added, 2)
|
|
|
|
|
|
|
|
assert.Equal(t, "", added[0].host)
|
|
|
|
assert.Equal(t, Route{Method: http.MethodGet, Path: "/static", Name: "github.com/labstack/echo/v4.glob..func1"}, added[0].route)
|
|
|
|
assert.Len(t, added[0].middleware, 0)
|
|
|
|
|
|
|
|
assert.Equal(t, "domain.site", added[1].host)
|
|
|
|
assert.Equal(t, Route{Method: http.MethodGet, Path: "/static/*", Name: "github.com/labstack/echo/v4.TestEcho_OnAddRouteHandler.func1"}, added[1].route)
|
|
|
|
assert.Len(t, added[1].middleware, 1)
|
|
|
|
}
|
|
|
|
|
2020-11-24 05:24:27 +02:00
|
|
|
func TestEchoReverse(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
dummyHandler := func(Context) error { return nil }
|
|
|
|
|
|
|
|
e.GET("/static", dummyHandler).Name = "/static"
|
|
|
|
e.GET("/static/*", dummyHandler).Name = "/static/*"
|
|
|
|
e.GET("/params/:foo", dummyHandler).Name = "/params/:foo"
|
|
|
|
e.GET("/params/:foo/bar/:qux", dummyHandler).Name = "/params/:foo/bar/:qux"
|
|
|
|
e.GET("/params/:foo/bar/:qux/*", dummyHandler).Name = "/params/:foo/bar/:qux/*"
|
|
|
|
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, "/static", e.Reverse("/static"))
|
|
|
|
assert.Equal(t, "/static", e.Reverse("/static", "missing param"))
|
|
|
|
assert.Equal(t, "/static/*", e.Reverse("/static/*"))
|
|
|
|
assert.Equal(t, "/static/foo.txt", e.Reverse("/static/*", "foo.txt"))
|
|
|
|
|
|
|
|
assert.Equal(t, "/params/:foo", e.Reverse("/params/:foo"))
|
|
|
|
assert.Equal(t, "/params/one", e.Reverse("/params/:foo", "one"))
|
|
|
|
assert.Equal(t, "/params/:foo/bar/:qux", e.Reverse("/params/:foo/bar/:qux"))
|
|
|
|
assert.Equal(t, "/params/one/bar/:qux", e.Reverse("/params/:foo/bar/:qux", "one"))
|
|
|
|
assert.Equal(t, "/params/one/bar/two", e.Reverse("/params/:foo/bar/:qux", "one", "two"))
|
|
|
|
assert.Equal(t, "/params/one/bar/two/three", e.Reverse("/params/:foo/bar/:qux/*", "one", "two", "three"))
|
2020-11-24 05:24:27 +02:00
|
|
|
}
|
2020-12-29 11:46:09 +02:00
|
|
|
|
2021-06-25 22:56:07 +02:00
|
|
|
func TestEchoReverseHandleHostProperly(t *testing.T) {
|
|
|
|
dummyHandler := func(Context) error { return nil }
|
|
|
|
|
|
|
|
e := New()
|
2022-11-12 18:35:19 +02:00
|
|
|
|
|
|
|
// routes added to the default router are different form different hosts
|
|
|
|
e.GET("/static", dummyHandler).Name = "default-host /static"
|
|
|
|
e.GET("/static/*", dummyHandler).Name = "xxx"
|
|
|
|
|
|
|
|
// different host
|
2021-06-25 22:56:07 +02:00
|
|
|
h := e.Host("the_host")
|
2022-11-12 18:35:19 +02:00
|
|
|
h.GET("/static", dummyHandler).Name = "host2 /static"
|
|
|
|
h.GET("/static/v2/*", dummyHandler).Name = "xxx"
|
|
|
|
|
|
|
|
assert.Equal(t, "/static", e.Reverse("default-host /static"))
|
|
|
|
// when actual route does not have params and we provide some to Reverse we should get that route url back
|
|
|
|
assert.Equal(t, "/static", e.Reverse("default-host /static", "missing param"))
|
|
|
|
|
|
|
|
host2Router := e.Routers()["the_host"]
|
|
|
|
assert.Equal(t, "/static", host2Router.Reverse("host2 /static"))
|
|
|
|
assert.Equal(t, "/static", host2Router.Reverse("host2 /static", "missing param"))
|
|
|
|
|
|
|
|
assert.Equal(t, "/static/v2/*", host2Router.Reverse("xxx"))
|
|
|
|
assert.Equal(t, "/static/v2/foo.txt", host2Router.Reverse("xxx", "foo.txt"))
|
2021-06-25 22:56:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func TestEcho_ListenerAddr(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
addr := e.ListenerAddr()
|
|
|
|
assert.Nil(t, addr)
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(":0")
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errCh, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcho_TLSListenerAddr(t *testing.T) {
|
2022-11-21 14:29:43 +02:00
|
|
|
cert, err := os.ReadFile("_fixture/certs/cert.pem")
|
2020-12-29 11:46:09 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-21 14:29:43 +02:00
|
|
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
2020-12-29 11:46:09 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
addr := e.TLSListenerAddr()
|
|
|
|
assert.Nil(t, addr)
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- e.StartTLS(":0", cert, key)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = waitForServerStart(e, errCh, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcho_StartServer(t *testing.T) {
|
2022-11-21 14:29:43 +02:00
|
|
|
cert, err := os.ReadFile("_fixture/certs/cert.pem")
|
2020-12-29 11:46:09 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-21 14:29:43 +02:00
|
|
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
2020-12-29 11:46:09 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
certs, err := tls.X509KeyPair(cert, key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
addr: ":0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok, start with TLS",
|
|
|
|
addr: ":0",
|
|
|
|
TLSConfig: &tls.Config{Certificates: []tls.Certificate{certs}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid address",
|
|
|
|
addr: "nope",
|
|
|
|
expectError: "listen tcp: address nope: missing port in address",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, invalid tls address",
|
|
|
|
addr: "nope",
|
|
|
|
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
expectError: "listen tcp: address nope: missing port in address",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.Debug = true
|
|
|
|
|
|
|
|
server := new(http.Server)
|
|
|
|
server.Addr = tc.addr
|
|
|
|
if tc.TLSConfig != nil {
|
|
|
|
server.TLSConfig = tc.TLSConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- e.StartServer(server)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := waitForServerStart(e, errCh, tc.TLSConfig != nil)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 03:01:02 +02:00
|
|
|
|
|
|
|
func benchmarkEchoRoutes(b *testing.B, routes []*Route) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
u := req.URL
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
// Add routes
|
|
|
|
for _, route := range routes {
|
|
|
|
e.Add(route.Method, route.Path, func(c Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find routes
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, route := range routes {
|
|
|
|
req.Method = route.Method
|
|
|
|
u.Path = route.Path
|
|
|
|
e.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEchoStaticRoutes(b *testing.B) {
|
|
|
|
benchmarkEchoRoutes(b, staticRoutes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEchoStaticRoutesMisses(b *testing.B) {
|
|
|
|
benchmarkEchoRoutes(b, staticRoutes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEchoGitHubAPI(b *testing.B) {
|
|
|
|
benchmarkEchoRoutes(b, gitHubAPI)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEchoGitHubAPIMisses(b *testing.B) {
|
|
|
|
benchmarkEchoRoutes(b, gitHubAPI)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEchoParseAPI(b *testing.B) {
|
|
|
|
benchmarkEchoRoutes(b, parseAPI)
|
|
|
|
}
|