2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
import (
|
2016-04-12 01:49:20 +02:00
|
|
|
"bytes"
|
2019-02-19 20:18:42 +02:00
|
|
|
"crypto/tls"
|
2019-01-14 20:12:22 +02:00
|
|
|
"encoding/json"
|
2018-02-21 20:44:17 +02:00
|
|
|
"encoding/xml"
|
2015-05-30 02:20:13 +02:00
|
|
|
"errors"
|
2019-02-19 20:18:42 +02:00
|
|
|
"fmt"
|
2015-04-11 06:48:26 +02:00
|
|
|
"io"
|
2019-05-24 07:13:57 +02:00
|
|
|
"math"
|
2016-06-06 07:25:34 +02:00
|
|
|
"mime/multipart"
|
2015-03-31 05:54:38 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2018-02-21 20:44:17 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-03-31 05:54:38 +02:00
|
|
|
"testing"
|
2015-04-11 06:48:26 +02:00
|
|
|
"text/template"
|
2016-05-03 01:19:35 +02:00
|
|
|
"time"
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2020-01-25 19:48:53 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2023-11-07 13:40:22 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-31 05:54:38 +02:00
|
|
|
)
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
2015-04-11 06:48:26 +02:00
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
var testUser = user{1, "Jon Snow"}
|
|
|
|
|
|
|
|
func BenchmarkAllocJSONP(b *testing.B) {
|
|
|
|
e := New()
|
2022-08-19 19:08:38 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.JSONP(http.StatusOK, "callback", testUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAllocJSON(b *testing.B) {
|
|
|
|
e := New()
|
2022-08-19 19:08:38 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.JSON(http.StatusOK, testUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAllocXML(b *testing.B) {
|
|
|
|
e := New()
|
2022-08-19 19:08:38 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.XML(http.StatusOK, testUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:36:43 +02:00
|
|
|
func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) {
|
|
|
|
c := context{request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
|
|
|
|
}}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.RealIP()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 06:03:11 +02:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
|
2015-05-20 23:38:51 +02:00
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextEcho(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
rec := httptest.NewRecorder()
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, e, c.Echo())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextRequest(t *testing.T) {
|
2015-12-01 21:22:45 +02:00
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2023-11-07 13:40:22 +02:00
|
|
|
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NotNil(t, c.Request())
|
|
|
|
assert.Equal(t, req, c.Request())
|
|
|
|
}
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextResponse(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NotNil(t, c.Response())
|
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextRenderTemplate(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
tmpl := &Template{
|
2015-05-30 19:54:55 +02:00
|
|
|
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
c.echo.Renderer = tmpl
|
2016-05-01 21:38:51 +02:00
|
|
|
err := c.Render(http.StatusOK, "hello", "Jon Snow")
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "Hello, Jon Snow!", rec.Body.String())
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2023-11-07 13:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextRenderErrorsOnNoRenderer(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
c.echo.Renderer = nil
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Error(t, c.Render(http.StatusOK, "hello", "Jon Snow"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSON(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, userJSON+"\n", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONErrorsOut(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.JSON(http.StatusOK, make(chan bool))
|
|
|
|
assert.EqualError(t, err, "json: unsupported type: chan bool")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONPrettyURL(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, userJSONPretty+"\n", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONPretty(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, userJSONPretty+"\n", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONWithEmptyIntent(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
u := user{1, "Jon Snow"}
|
|
|
|
emptyIndent := ""
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent(emptyIndent, emptyIndent)
|
|
|
|
_ = enc.Encode(u)
|
|
|
|
err := c.json(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, buf.String(), rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONP(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
callback := "callback"
|
2023-11-07 13:40:22 +02:00
|
|
|
err := c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, callback+"("+userJSON+"\n);", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 20:12:22 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextJSONBlob(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
2019-01-14 20:12:22 +02:00
|
|
|
|
|
|
|
data, err := json.Marshal(user{1, "Jon Snow"})
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.JSONBlob(http.StatusOK, data)
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, userJSON, rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextJSONPBlob(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
callback := "callback"
|
|
|
|
data, err := json.Marshal(user{1, "Jon Snow"})
|
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.JSONPBlob(http.StatusOK, callback, data)
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextXML(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.XML(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextXMLPrettyURL(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.XML(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
2023-11-07 13:40:22 +02:00
|
|
|
}
|
2019-01-14 20:12:22 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
func TestContextXMLPretty(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextXMLBlob(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
data, err := xml.Marshal(user{1, "Jon Snow"})
|
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.XMLBlob(http.StatusOK, data)
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextXMLWithEmptyIntent(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
u := user{1, "Jon Snow"}
|
|
|
|
emptyIndent := ""
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
enc := xml.NewEncoder(buf)
|
|
|
|
enc.Indent(emptyIndent, emptyIndent)
|
|
|
|
_ = enc.Encode(u)
|
|
|
|
err := c.xml(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+buf.String(), rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseWriterErr struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) Header() http.Header {
|
|
|
|
return http.Header{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) Write([]byte) (int, error) {
|
|
|
|
return 0, errors.New("responseWriterErr")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) WriteHeader(statusCode int) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextXMLError(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
c.response.Writer = responseWriterErr{}
|
|
|
|
|
|
|
|
err := c.XML(http.StatusOK, make(chan bool))
|
|
|
|
assert.EqualError(t, err, "responseWriterErr")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextString(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.String(http.StatusOK, "Hello, World!")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "Hello, World!", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextHTML(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextStream(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
2016-09-10 00:30:07 +02:00
|
|
|
r := strings.NewReader("response from a stream")
|
2023-11-07 13:40:22 +02:00
|
|
|
err := c.Stream(http.StatusOK, "application/octet-stream", r)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "application/octet-stream", rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "response from a stream", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextAttachment(t *testing.T) {
|
2023-11-07 14:10:06 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenName string
|
|
|
|
expectHeader string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
whenName: "walle.png",
|
|
|
|
expectHeader: `attachment; filename="walle.png"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok, escape quotes in malicious filename",
|
|
|
|
whenName: `malicious.sh"; \"; dummy=.txt`,
|
|
|
|
expectHeader: `attachment; filename="malicious.sh\"; \\\"; dummy=.txt"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.Attachment("_fixture/images/walle.png", tc.whenName)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, tc.expectHeader, rec.Header().Get(HeaderContentDisposition))
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
|
|
|
}
|
|
|
|
})
|
2023-11-07 13:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextInline(t *testing.T) {
|
2023-11-07 14:10:06 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenName string
|
|
|
|
expectHeader string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
whenName: "walle.png",
|
|
|
|
expectHeader: `inline; filename="walle.png"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok, escape quotes in malicious filename",
|
|
|
|
whenName: `malicious.sh"; \"; dummy=.txt`,
|
|
|
|
expectHeader: `inline; filename="malicious.sh\"; \\\"; dummy=.txt"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
|
|
|
err := c.Inline("_fixture/images/walle.png", tc.whenName)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, tc.expectHeader, rec.Header().Get(HeaderContentDisposition))
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
|
|
|
}
|
|
|
|
})
|
2023-11-07 13:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextNoContent(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextError(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
c.Error(errors.New("error"))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
assert.True(t, c.Response().Committed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextReset(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2017-03-30 12:05:24 +02:00
|
|
|
c.SetParamNames("foo")
|
|
|
|
c.SetParamValues("bar")
|
|
|
|
c.Set("foe", "ban")
|
2018-02-21 20:44:17 +02:00
|
|
|
c.query = url.Values(map[string][]string{"fon": {"baz"}})
|
2023-11-07 13:40:22 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
c.Reset(req, httptest.NewRecorder())
|
2023-11-07 13:40:22 +02:00
|
|
|
|
|
|
|
assert.Len(t, c.ParamValues(), 0)
|
|
|
|
assert.Len(t, c.ParamNames(), 0)
|
|
|
|
assert.Len(t, c.Path(), 0)
|
|
|
|
assert.Len(t, c.QueryParams(), 0)
|
|
|
|
assert.Len(t, c.store, 0)
|
2015-05-30 19:54:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 07:13:57 +02:00
|
|
|
func TestContext_JSON_CommitsCustomResponseCode(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
err := c.JSON(http.StatusCreated, user{1, "Jon Snow"})
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
2024-02-06 07:41:33 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSON, rec.Header().Get(HeaderContentType))
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, userJSON+"\n", rec.Body.String())
|
2019-05-24 07:13:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_JSON_DoesntCommitResponseCodePrematurely(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec).(*context)
|
|
|
|
err := c.JSON(http.StatusCreated, map[string]float64{"a": math.NaN()})
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.False(t, c.response.Committed)
|
2019-05-24 07:13:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
func TestContextCookie(t *testing.T) {
|
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-05-03 01:19:35 +02:00
|
|
|
theme := "theme=light"
|
|
|
|
user := "user=Jon Snow"
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(HeaderCookie, theme)
|
|
|
|
req.Header.Add(HeaderCookie, user)
|
|
|
|
rec := httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Read single
|
2016-05-03 07:41:07 +02:00
|
|
|
cookie, err := c.Cookie("theme")
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "theme", cookie.Name)
|
|
|
|
assert.Equal(t, "light", cookie.Value)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Read multiple
|
|
|
|
for _, cookie := range c.Cookies() {
|
2016-09-23 07:53:44 +02:00
|
|
|
switch cookie.Name {
|
2016-05-03 01:19:35 +02:00
|
|
|
case "theme":
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "light", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
case "user":
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "Jon Snow", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write
|
2016-09-23 07:53:44 +02:00
|
|
|
cookie = &http.Cookie{
|
2016-05-03 01:19:35 +02:00
|
|
|
Name: "SSID",
|
|
|
|
Value: "Ap4PGTEq",
|
|
|
|
Domain: "labstack.com",
|
|
|
|
Path: "/",
|
|
|
|
Expires: time.Now(),
|
|
|
|
Secure: true,
|
|
|
|
HttpOnly: true,
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2016-05-03 01:19:35 +02:00
|
|
|
c.SetCookie(cookie)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "SSID")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Ap4PGTEq")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "labstack.com")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Secure")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "HttpOnly")
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
func TestContextPath(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
r := e.Router()
|
|
|
|
|
2021-04-27 09:55:31 +02:00
|
|
|
handler := func(c Context) error { return c.String(http.StatusOK, "OK") }
|
|
|
|
|
|
|
|
r.Add(http.MethodGet, "/users/:id", handler)
|
2016-04-17 00:53:27 +02:00
|
|
|
c := e.NewContext(nil, nil)
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Find(http.MethodGet, "/users/1", c)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "/users/:id", c.Path())
|
2015-11-13 06:23:14 +02:00
|
|
|
|
2021-04-27 09:55:31 +02:00
|
|
|
r.Add(http.MethodGet, "/users/:uid/files/:fid", handler)
|
2016-04-17 00:53:27 +02:00
|
|
|
c = e.NewContext(nil, nil)
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Find(http.MethodGet, "/users/1/files/1", c)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "/users/:uid/files/:fid", c.Path())
|
2015-11-13 06:23:14 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
func TestContextPathParam(t *testing.T) {
|
2016-04-17 00:53:27 +02:00
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// ParamNames
|
|
|
|
c.SetParamNames("uid", "fid")
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// ParamValues
|
|
|
|
c.SetParamValues("101", "501")
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.EqualValues(t, []string{"101", "501"}, c.ParamValues())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// Param
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "501", c.Param("fid"))
|
|
|
|
assert.Equal(t, "", c.Param("undefined"))
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 04:32:17 +02:00
|
|
|
func TestContextGetAndSetParam(t *testing.T) {
|
|
|
|
e := New()
|
2020-03-30 21:28:07 +02:00
|
|
|
r := e.Router()
|
|
|
|
r.Add(http.MethodGet, "/:foo", func(Context) error { return nil })
|
2020-01-24 04:32:17 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
c.SetParamNames("foo")
|
|
|
|
|
|
|
|
// round-trip param values with modification
|
|
|
|
paramVals := c.ParamValues()
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.EqualValues(t, []string{""}, c.ParamValues())
|
2020-01-24 04:32:17 +02:00
|
|
|
paramVals[0] = "bar"
|
|
|
|
c.SetParamValues(paramVals...)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.EqualValues(t, []string{"bar"}, c.ParamValues())
|
2020-01-24 04:32:17 +02:00
|
|
|
|
|
|
|
// shouldn't explode during Reset() afterwards!
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NotPanics(t, func() {
|
2020-01-24 04:32:17 +02:00
|
|
|
c.Reset(nil, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-21 23:42:16 +02:00
|
|
|
func TestContextSetParamNamesEchoMaxParam(t *testing.T) {
|
2020-10-28 06:30:41 +02:00
|
|
|
e := New()
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, 0, *e.maxParam)
|
2020-10-28 06:30:41 +02:00
|
|
|
|
|
|
|
expectedOneParam := []string{"one"}
|
|
|
|
expectedTwoParams := []string{"one", "two"}
|
|
|
|
expectedThreeParams := []string{"one", "two", ""}
|
|
|
|
|
2024-03-21 23:42:16 +02:00
|
|
|
{
|
|
|
|
c := e.AcquireContext()
|
|
|
|
c.SetParamNames("1", "2")
|
|
|
|
c.SetParamValues(expectedTwoParams...)
|
|
|
|
assert.Equal(t, 0, *e.maxParam) // has not been changed
|
|
|
|
assert.EqualValues(t, expectedTwoParams, c.ParamValues())
|
|
|
|
e.ReleaseContext(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
c := e.AcquireContext()
|
|
|
|
c.SetParamNames("1", "2", "3")
|
|
|
|
c.SetParamValues(expectedThreeParams...)
|
|
|
|
assert.Equal(t, 0, *e.maxParam) // has not been changed
|
|
|
|
assert.EqualValues(t, expectedThreeParams, c.ParamValues())
|
|
|
|
e.ReleaseContext(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // values is always same size as names length
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
c.SetParamValues([]string{"one", "two"}...) // more values than names should be ok
|
|
|
|
c.SetParamNames("1")
|
|
|
|
assert.Equal(t, 0, *e.maxParam) // has not been changed
|
|
|
|
assert.EqualValues(t, expectedOneParam, c.ParamValues())
|
|
|
|
}
|
|
|
|
|
|
|
|
e.GET("/:id", handlerFunc)
|
|
|
|
assert.Equal(t, 1, *e.maxParam) // has not been changed
|
|
|
|
|
|
|
|
{
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
c.SetParamValues([]string{"one", "two"}...)
|
|
|
|
c.SetParamNames("1")
|
|
|
|
assert.Equal(t, 1, *e.maxParam) // has not been changed
|
|
|
|
assert.EqualValues(t, expectedOneParam, c.ParamValues())
|
|
|
|
}
|
2020-10-28 06:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
func TestContextFormValue(t *testing.T) {
|
2015-07-05 20:08:17 +02:00
|
|
|
f := make(url.Values)
|
2016-06-06 07:25:34 +02:00
|
|
|
f.Set("name", "Jon Snow")
|
|
|
|
f.Set("email", "jon@labstack.com")
|
2015-07-05 20:08:17 +02:00
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
e := New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(HeaderContentType, MIMEApplicationForm)
|
2016-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2015-07-05 20:08:17 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// FormValue
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "Jon Snow", c.FormValue("name"))
|
|
|
|
assert.Equal(t, "jon@labstack.com", c.FormValue("email"))
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// FormParams
|
2016-09-23 07:53:44 +02:00
|
|
|
params, err := c.FormParams()
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, url.Values{
|
2016-09-23 07:53:44 +02:00
|
|
|
"name": []string{"Jon Snow"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
}, params)
|
|
|
|
}
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
// Multipart FormParams error
|
|
|
|
req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
|
|
|
|
req.Header.Add(HeaderContentType, MIMEMultipartForm)
|
|
|
|
c = e.NewContext(req, nil)
|
|
|
|
params, err = c.FormParams()
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Nil(t, params)
|
|
|
|
assert.Error(t, err)
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextQueryParam(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("name", "Jon Snow")
|
|
|
|
q.Set("email", "jon@labstack.com")
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
e := New()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// QueryParam
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "Jon Snow", c.QueryParam("name"))
|
|
|
|
assert.Equal(t, "jon@labstack.com", c.QueryParam("email"))
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// QueryParams
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, url.Values{
|
2016-06-06 07:25:34 +02:00
|
|
|
"name": []string{"Jon Snow"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
}, c.QueryParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextFormFile(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mr := multipart.NewWriter(buf)
|
|
|
|
w, err := mr.CreateFormFile("file", "test")
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-06-06 07:25:34 +02:00
|
|
|
w.Write([]byte("test"))
|
|
|
|
}
|
|
|
|
mr.Close()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", buf)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(HeaderContentType, mr.FormDataContentType())
|
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
f, err := c.FormFile("file")
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "test", f.Filename)
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextMultipartForm(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(buf)
|
|
|
|
mw.WriteField("name", "Jon Snow")
|
|
|
|
mw.Close()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", buf)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(HeaderContentType, mw.FormDataContentType())
|
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
f, err := c.MultipartForm()
|
2023-11-07 13:40:22 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.NotNil(t, f)
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextRedirect(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-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
|
|
|
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
|
|
|
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextStore(t *testing.T) {
|
2021-02-26 12:04:34 +02:00
|
|
|
var c Context = new(context)
|
2016-06-06 07:25:34 +02:00
|
|
|
c.Set("name", "Jon Snow")
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "Jon Snow", c.Get("name"))
|
2015-11-22 20:38:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 20:56:26 +02:00
|
|
|
func BenchmarkContext_Store(b *testing.B) {
|
|
|
|
e := &Echo{}
|
|
|
|
|
|
|
|
c := &context{
|
|
|
|
echo: e,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
c.Set("name", "Jon Snow")
|
|
|
|
if c.Get("name") != "Jon Snow" {
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 01:49:20 +02:00
|
|
|
func TestContextHandler(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
r := e.Router()
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/handler", func(Context) error {
|
2016-04-12 01:49:20 +02:00
|
|
|
_, err := b.Write([]byte("handler"))
|
|
|
|
return err
|
2016-10-22 05:36:49 +02:00
|
|
|
})
|
2016-04-17 00:53:27 +02:00
|
|
|
c := e.NewContext(nil, nil)
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Find(http.MethodGet, "/handler", c)
|
2019-02-19 20:18:42 +02:00
|
|
|
err := c.Handler()(c)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, "handler", b.String())
|
|
|
|
assert.NoError(t, err)
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_SetHandler(t *testing.T) {
|
2021-02-26 12:04:34 +02:00
|
|
|
var c Context = new(context)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Nil(t, c.Handler())
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
c.SetHandler(func(c Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NotNil(t, c.Handler())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Path(t *testing.T) {
|
|
|
|
path := "/pa/th"
|
|
|
|
|
2021-02-26 12:04:34 +02:00
|
|
|
var c Context = new(context)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
c.SetPath(path)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, path, c.Path())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type validator struct{}
|
|
|
|
|
|
|
|
func (*validator) Validate(i interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Validate(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Error(t, c.Validate(struct{}{}))
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
e.Validator = &validator{}
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NoError(t, c.Validate(struct{}{}))
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_QueryString(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
queryString := "query=string&var=val"
|
|
|
|
|
2022-08-19 19:06:40 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?"+queryString, nil)
|
2019-02-19 20:18:42 +02:00
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, queryString, c.QueryString())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Request(t *testing.T) {
|
2021-02-26 12:04:34 +02:00
|
|
|
var c Context = new(context)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Nil(t, c.Request())
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2022-08-19 19:06:40 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/path", nil)
|
2019-02-19 20:18:42 +02:00
|
|
|
c.SetRequest(req)
|
|
|
|
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, req, c.Request())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Scheme(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
c Context
|
|
|
|
s string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
TLS: &tls.ConnectionState{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedProto: []string{"https"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedProtocol: []string{"http"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"http",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedSsl: []string{"on"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXUrlScheme: []string{"https"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{},
|
|
|
|
},
|
|
|
|
"http",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, tt.s, tt.c.Scheme())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_IsWebSocket(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
c Context
|
2023-11-07 13:40:22 +02:00
|
|
|
ws assert.BoolAssertionFunc
|
2019-02-19 20:18:42 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"websocket"}},
|
|
|
|
},
|
|
|
|
},
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.True,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"Websocket"}},
|
|
|
|
},
|
|
|
|
},
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.True,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{},
|
|
|
|
},
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.False,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"other"}},
|
|
|
|
},
|
|
|
|
},
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.False,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("test %d", i+1), func(t *testing.T) {
|
|
|
|
tt.ws(t, tt.c.IsWebSocket())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Bind(t *testing.T) {
|
|
|
|
e := New()
|
2022-08-19 19:08:38 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-02-19 20:18:42 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2019-06-27 19:52:17 +02:00
|
|
|
u := new(user)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
req.Header.Add(HeaderContentType, MIMEApplicationJSON)
|
2019-06-27 19:52:17 +02:00
|
|
|
err := c.Bind(u)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &user{1, "Jon Snow"}, u)
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Logger(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
|
2019-11-11 22:34:13 +02:00
|
|
|
log1 := c.Logger()
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.NotNil(t, log1)
|
2019-11-11 22:34:13 +02:00
|
|
|
|
|
|
|
log2 := log.New("echo2")
|
|
|
|
c.SetLogger(log2)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, log2, c.Logger())
|
2019-11-11 22:34:13 +02:00
|
|
|
|
|
|
|
// Resetting the context returns the initial logger
|
|
|
|
c.Reset(nil, nil)
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, log1, c.Logger())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_RealIP(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
c Context
|
|
|
|
s string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"127.0.0.1",
|
|
|
|
},
|
2021-05-20 16:51:15 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1,127.0.1.1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"127.0.0.1",
|
|
|
|
},
|
2020-09-16 04:36:43 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"127.0.0.1",
|
|
|
|
},
|
2022-11-24 22:17:31 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"[2001:db8:85a3:8d3:1319:8a2e:370:7348], 2001:db8::1, "}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"2001:db8:85a3:8d3:1319:8a2e:370:7348",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"[2001:db8:85a3:8d3:1319:8a2e:370:7348],[2001:db8::1]"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"2001:db8:85a3:8d3:1319:8a2e:370:7348",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"2001:db8:85a3:8d3:1319:8a2e:370:7348"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"2001:db8:85a3:8d3:1319:8a2e:370:7348",
|
|
|
|
},
|
2019-02-19 20:18:42 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"X-Real-Ip": []string{"192.168.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"192.168.0.1",
|
|
|
|
},
|
2022-11-24 22:17:31 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"X-Real-Ip": []string{"[2001:db8::1]"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"2001:db8::1",
|
|
|
|
},
|
|
|
|
|
2019-02-19 20:18:42 +02:00
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
RemoteAddr: "89.89.89.89:1654",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"89.89.89.89",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2023-11-07 13:40:22 +02:00
|
|
|
assert.Equal(t, tt.s, tt.c.RealIP())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
2016-04-12 01:49:20 +02:00
|
|
|
}
|