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"
|
2019-01-14 20:12:22 +02:00
|
|
|
testify "github.com/stretchr/testify/assert"
|
2015-03-31 05:54:38 +02:00
|
|
|
)
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
type (
|
|
|
|
Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
var testUser = user{1, "Jon Snow"}
|
|
|
|
|
|
|
|
func BenchmarkAllocJSONP(b *testing.B) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
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()
|
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
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()
|
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-19 20:18:42 +02:00
|
|
|
type responseWriterErr struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) Header() http.Header {
|
|
|
|
return http.Header{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) Write([]byte) (int, error) {
|
|
|
|
return 0, errors.New("err")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (responseWriterErr) WriteHeader(statusCode int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
func TestContext(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()
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
assert := testify.New(t)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// Echo
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(e, c.Echo())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// Request
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.NotNil(c.Request())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Response
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.NotNil(c.Response())
|
2015-05-30 19:54:55 +02:00
|
|
|
|
|
|
|
//--------
|
2015-04-11 06:48:26 +02:00
|
|
|
// Render
|
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")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal("Hello, Jon Snow!", rec.Body.String())
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
c.echo.Renderer = nil
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.Render(http.StatusOK, "hello", "Jon Snow")
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Error(err)
|
2015-04-11 06:48:26 +02:00
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// JSON
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
2019-01-14 20:12:22 +02:00
|
|
|
assert.Equal(userJSON+"\n", rec.Body.String())
|
2015-07-24 21:28:35 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:46 +02:00
|
|
|
// JSON with "?pretty"
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
2017-04-28 06:41:46 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
2019-01-14 20:12:22 +02:00
|
|
|
assert.Equal(userJSONPretty+"\n", rec.Body.String())
|
2017-04-28 06:41:46 +02:00
|
|
|
}
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil) // reset
|
2017-04-28 06:41:46 +02:00
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
// JSONPretty
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
2019-01-14 20:12:22 +02:00
|
|
|
assert.Equal(userJSONPretty+"\n", rec.Body.String())
|
2016-12-09 21:13:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// JSON (error)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.JSON(http.StatusOK, make(chan bool))
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Error(err)
|
2015-11-08 01:59:08 +02:00
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSONP
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-07-24 21:28:35 +02:00
|
|
|
callback := "callback"
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
2019-01-14 20:12:22 +02:00
|
|
|
assert.Equal(callback+"("+userJSON+"\n);", rec.Body.String())
|
2015-07-11 21:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XML
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(xml.Header+userXML, rec.Body.String())
|
2015-11-08 01:39:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:46 +02:00
|
|
|
// XML with "?pretty"
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/?pretty", nil)
|
2017-04-28 06:41:46 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(xml.Header+userXMLPretty, rec.Body.String())
|
2017-04-28 06:41:46 +02:00
|
|
|
}
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
2017-04-28 06:41:46 +02:00
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// XML (error)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.XML(http.StatusOK, make(chan bool))
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Error(err)
|
2015-11-08 01:59:08 +02:00
|
|
|
|
2019-02-19 20:18:42 +02:00
|
|
|
// XML response write error
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
c.response.Writer = responseWriterErr{}
|
|
|
|
err = c.XML(0, 0)
|
|
|
|
testify.Error(t, err)
|
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
// XMLPretty
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(xml.Header+userXMLPretty, rec.Body.String())
|
2016-12-09 21:13:55 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
t.Run("empty indent", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
u = user{1, "Jon Snow"}
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
emptyIndent = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
|
|
|
assert := testify.New(t)
|
|
|
|
|
|
|
|
// New JSONBlob with empty indent
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent(emptyIndent, emptyIndent)
|
|
|
|
err = enc.Encode(u)
|
2019-01-14 20:56:17 +02:00
|
|
|
err = c.json(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
|
2019-01-14 20:12:22 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(buf.String(), rec.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("xml", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
|
|
|
assert := testify.New(t)
|
|
|
|
|
|
|
|
// New XMLBlob with empty indent
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
enc := xml.NewEncoder(buf)
|
|
|
|
enc.Indent(emptyIndent, emptyIndent)
|
|
|
|
err = enc.Encode(u)
|
2019-01-14 20:56:17 +02:00
|
|
|
err = c.xml(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
|
2019-01-14 20:12:22 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(xml.Header+buf.String(), rec.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Legacy JSONBlob
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
data, err := json.Marshal(user{1, "Jon Snow"})
|
|
|
|
assert.NoError(err)
|
|
|
|
err = c.JSONBlob(http.StatusOK, data)
|
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(userJSON, rec.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy JSONPBlob
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
callback = "callback"
|
|
|
|
data, err = json.Marshal(user{1, "Jon Snow"})
|
|
|
|
assert.NoError(err)
|
|
|
|
err = c.JSONPBlob(http.StatusOK, callback, data)
|
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(callback+"("+userJSON+");", rec.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy XMLBlob
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
data, err = xml.Marshal(user{1, "Jon Snow"})
|
|
|
|
assert.NoError(err)
|
|
|
|
err = c.XMLBlob(http.StatusOK, data)
|
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(xml.Header+userXML, rec.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal("Hello, World!", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +02:00
|
|
|
// HTML
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal("Hello, <strong>World!</strong>", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:30:07 +02:00
|
|
|
// Stream
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-09-10 00:30:07 +02:00
|
|
|
r := strings.NewReader("response from a stream")
|
|
|
|
err = c.Stream(http.StatusOK, "application/octet-stream", r)
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal("application/octet-stream", rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal("response from a stream", rec.Body.String())
|
2016-09-10 00:30:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Attachment
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Attachment("_fixture/images/walle.png", "walle.png")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal("attachment; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(219885, rec.Body.Len())
|
2015-08-25 07:01:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 23:46:21 +02:00
|
|
|
// Inline
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Inline("_fixture/images/walle.png", "walle.png")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal("inline; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(219885, rec.Body.Len())
|
2016-08-22 23:46:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// NoContent
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Error
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.Error(errors.New("error"))
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(http.StatusInternalServerError, rec.Code)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Reset
|
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"}})
|
2016-09-23 07:53:44 +02:00
|
|
|
c.Reset(req, httptest.NewRecorder())
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(0, len(c.ParamValues()))
|
|
|
|
assert.Equal(0, len(c.ParamNames()))
|
|
|
|
assert.Equal(0, len(c.store))
|
|
|
|
assert.Equal("", c.Path())
|
|
|
|
assert.Equal(0, len(c.QueryParams()))
|
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"})
|
|
|
|
|
|
|
|
assert := testify.New(t)
|
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal(http.StatusCreated, rec.Code)
|
|
|
|
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(userJSON+"\n", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()})
|
|
|
|
|
|
|
|
assert := testify.New(t)
|
|
|
|
if assert.Error(err) {
|
|
|
|
assert.False(c.response.Committed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
assert := testify.New(t)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// Read single
|
2016-05-03 07:41:07 +02:00
|
|
|
cookie, err := c.Cookie("theme")
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal("theme", cookie.Name)
|
|
|
|
assert.Equal("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":
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal("light", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
case "user":
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal("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)
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Contains(rec.Header().Get(HeaderSetCookie), "SSID")
|
|
|
|
assert.Contains(rec.Header().Get(HeaderSetCookie), "Ap4PGTEq")
|
|
|
|
assert.Contains(rec.Header().Get(HeaderSetCookie), "labstack.com")
|
|
|
|
assert.Contains(rec.Header().Get(HeaderSetCookie), "Secure")
|
|
|
|
assert.Contains(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()
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/users/:id", nil)
|
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
|
|
|
|
2019-01-14 20:12:22 +02:00
|
|
|
assert := testify.New(t)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
|
|
|
assert.Equal("/users/:id", c.Path())
|
2015-11-13 06:23:14 +02:00
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/users/:uid/files/:fid", nil)
|
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)
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal("/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")
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// ParamValues
|
|
|
|
c.SetParamValues("101", "501")
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.EqualValues(t, []string{"101", "501"}, c.ParamValues())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// Param
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.Equal(t, "501", c.Param("fid"))
|
2019-02-19 20:18:42 +02:00
|
|
|
testify.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()
|
|
|
|
testify.EqualValues(t, []string{""}, c.ParamValues())
|
|
|
|
paramVals[0] = "bar"
|
|
|
|
c.SetParamValues(paramVals...)
|
|
|
|
testify.EqualValues(t, []string{"bar"}, c.ParamValues())
|
|
|
|
|
|
|
|
// shouldn't explode during Reset() afterwards!
|
|
|
|
testify.NotPanics(t, func() {
|
|
|
|
c.Reset(nil, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.Equal(t, "Jon Snow", c.FormValue("name"))
|
|
|
|
testify.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()
|
2019-01-14 20:12:22 +02:00
|
|
|
if testify.NoError(t, err) {
|
|
|
|
testify.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()
|
|
|
|
testify.Nil(t, params)
|
|
|
|
testify.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
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.Equal(t, "Jon Snow", c.QueryParam("name"))
|
|
|
|
testify.Equal(t, "jon@labstack.com", c.QueryParam("email"))
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// QueryParams
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.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")
|
2019-01-14 20:12:22 +02:00
|
|
|
if testify.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")
|
2019-01-14 20:12:22 +02:00
|
|
|
if testify.NoError(t, err) {
|
|
|
|
testify.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()
|
2019-01-14 20:12:22 +02:00
|
|
|
if testify.NoError(t, err) {
|
|
|
|
testify.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)
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
|
|
|
testify.Equal(t, http.StatusMovedPermanently, rec.Code)
|
|
|
|
testify.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
|
|
|
testify.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextStore(t *testing.T) {
|
2016-08-22 17:30:46 +02:00
|
|
|
var c Context
|
2016-10-11 02:31:26 +02:00
|
|
|
c = new(context)
|
2016-06-06 07:25:34 +02:00
|
|
|
c.Set("name", "Jon Snow")
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.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)
|
2019-01-14 20:12:22 +02:00
|
|
|
testify.Equal(t, "handler", b.String())
|
2019-02-19 20:18:42 +02:00
|
|
|
testify.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_SetHandler(t *testing.T) {
|
|
|
|
var c Context
|
|
|
|
c = new(context)
|
|
|
|
|
|
|
|
testify.Nil(t, c.Handler())
|
|
|
|
|
|
|
|
c.SetHandler(func(c Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
testify.NotNil(t, c.Handler())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Path(t *testing.T) {
|
|
|
|
path := "/pa/th"
|
|
|
|
|
|
|
|
var c Context
|
|
|
|
c = new(context)
|
|
|
|
|
|
|
|
c.SetPath(path)
|
|
|
|
testify.Equal(t, path, c.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
type validator struct{}
|
|
|
|
|
|
|
|
func (*validator) Validate(i interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Validate(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
|
|
|
|
testify.Error(t, c.Validate(struct{}{}))
|
|
|
|
|
|
|
|
e.Validator = &validator{}
|
|
|
|
testify.NoError(t, c.Validate(struct{}{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_QueryString(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
queryString := "query=string&var=val"
|
|
|
|
|
|
|
|
req := httptest.NewRequest(GET, "/?"+queryString, nil)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
testify.Equal(t, queryString, c.QueryString())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Request(t *testing.T) {
|
|
|
|
var c Context
|
|
|
|
c = new(context)
|
|
|
|
|
|
|
|
testify.Nil(t, c.Request())
|
|
|
|
|
|
|
|
req := httptest.NewRequest(GET, "/path", nil)
|
|
|
|
c.SetRequest(req)
|
|
|
|
|
|
|
|
testify.Equal(t, req, c.Request())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
testify.Equal(t, tt.s, tt.c.Scheme())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_IsWebSocket(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
c Context
|
|
|
|
ws testify.BoolAssertionFunc
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"websocket"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testify.True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"Websocket"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testify.True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{},
|
|
|
|
},
|
|
|
|
testify.False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"other"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testify.False,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
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)
|
2019-02-19 20:18:42 +02:00
|
|
|
testify.NoError(t, err)
|
|
|
|
testify.Equal(t, &user{1, "Jon Snow"}, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Logger(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(nil, nil)
|
|
|
|
|
2019-11-11 22:34:13 +02:00
|
|
|
log1 := c.Logger()
|
|
|
|
testify.NotNil(t, log1)
|
|
|
|
|
|
|
|
log2 := log.New("echo2")
|
|
|
|
c.SetLogger(log2)
|
|
|
|
testify.Equal(t, log2, c.Logger())
|
|
|
|
|
|
|
|
// Resetting the context returns the initial logger
|
|
|
|
c.Reset(nil, nil)
|
|
|
|
testify.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",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"X-Real-Ip": []string{"192.168.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"192.168.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&context{
|
|
|
|
request: &http.Request{
|
|
|
|
RemoteAddr: "89.89.89.89:1654",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"89.89.89.89",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
testify.Equal(t, tt.s, tt.c.RealIP())
|
|
|
|
}
|
2016-04-12 01:49:20 +02:00
|
|
|
}
|