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"
|
2021-07-15 22:34:01 +02:00
|
|
|
"io/fs"
|
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"
|
2021-07-15 22:34:01 +02:00
|
|
|
"os"
|
2018-02-21 20:44:17 +02:00
|
|
|
"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
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-31 05:54:38 +02:00
|
|
|
)
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2021-07-15 22:34:01 +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()
|
2021-07-15 22:34:01 +02:00
|
|
|
e.Logger = &noOpLogger{}
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.JSONP(http.StatusOK, "callback", testUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAllocJSON(b *testing.B) {
|
|
|
|
e := New()
|
2021-07-15 22:34:01 +02:00
|
|
|
e.Logger = &noOpLogger{}
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.JSON(http.StatusOK, testUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAllocXML(b *testing.B) {
|
|
|
|
e := New()
|
2021-07-15 22:34:01 +02:00
|
|
|
e.Logger = &noOpLogger{}
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
2019-01-14 20:12:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
|
|
|
|
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) {
|
2021-07-15 22:34:01 +02:00
|
|
|
c := DefaultContext{request: &http.Request{
|
2020-09-16 04:36:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// Echo
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, e, c.Echo())
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// Request
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NotNil(t, c.Request())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Response
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NotNil(t, 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")
|
2021-07-15 22:34:01 +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
|
|
|
}
|
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")
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Error(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.JSON(http.StatusOK, make(chan bool))
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Error(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
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"})
|
2021-07-15 22:34:01 +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+"\n);", rec.Body.String())
|
2015-07-11 21:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XML
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +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())
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +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+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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.XML(http.StatusOK, make(chan bool))
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Error(t, err)
|
2015-11-08 01:59:08 +02:00
|
|
|
|
2019-02-19 20:18:42 +02:00
|
|
|
// XML response write error
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-02-19 20:18:42 +02:00
|
|
|
c.response.Writer = responseWriterErr{}
|
|
|
|
err = c.XML(0, 0)
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Error(t, err)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
// XMLPretty
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2021-07-15 22:34:01 +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+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()
|
|
|
|
|
|
|
|
// New JSONBlob with empty indent
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
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)
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, buf.String(), rec.Body.String())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("xml", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
// New XMLBlob with empty indent
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
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)
|
2021-07-15 22:34:01 +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+buf.String(), rec.Body.String())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Legacy JSONBlob
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
data, err := json.Marshal(user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.JSONBlob(http.StatusOK, data)
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, userJSON, rec.Body.String())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy JSONPBlob
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
callback = "callback"
|
|
|
|
data, err = json.Marshal(user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.JSONPBlob(http.StatusOK, callback, data)
|
2021-07-15 22:34:01 +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())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy XMLBlob
|
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2019-01-14 20:12:22 +02:00
|
|
|
data, err = xml.Marshal(user{1, "Jon Snow"})
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NoError(t, err)
|
2019-01-14 20:12:22 +02:00
|
|
|
err = c.XMLBlob(http.StatusOK, data)
|
2021-07-15 22:34:01 +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())
|
2019-01-14 20:12:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
2021-07-15 22:34:01 +02:00
|
|
|
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())
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
2021-07-15 22:34:01 +02:00
|
|
|
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())
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-09-10 00:30:07 +02:00
|
|
|
r := strings.NewReader("response from a stream")
|
|
|
|
err = c.Stream(http.StatusOK, "application/octet-stream", r)
|
2021-07-15 22:34:01 +02:00
|
|
|
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())
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Attachment("_fixture/images/walle.png", "walle.png")
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "attachment; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Inline("_fixture/images/walle.png", "walle.png")
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "inline; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c = e.NewContext(req, rec).(*DefaultContext)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Reset
|
2021-07-15 22:34:01 +02:00
|
|
|
c.pathParams = &PathParams{
|
|
|
|
{Name: "foo", Value: "bar"},
|
|
|
|
}
|
2017-03-30 12:05:24 +02:00
|
|
|
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())
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, 0, len(c.PathParams()))
|
|
|
|
assert.Equal(t, 0, len(c.store))
|
|
|
|
assert.Equal(t, nil, c.RouteInfo())
|
|
|
|
assert.Equal(t, 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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2019-05-24 07:13:57 +02:00
|
|
|
err := c.JSON(http.StatusCreated, user{1, "Jon Snow"})
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
2019-05-24 07:13:57 +02:00
|
|
|
err := c.JSON(http.StatusCreated, map[string]float64{"a": math.NaN()})
|
|
|
|
|
2021-07-15 22:34:01 +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()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(req, rec).(*DefaultContext)
|
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")
|
2021-07-15 22:34:01 +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":
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, "light", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
case "user":
|
2021-07-15 22:34:01 +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)
|
2021-07-15 22:34:01 +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")
|
2015-11-13 06:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_PathParams(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
given *PathParams
|
|
|
|
expect PathParams
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "param exists",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
expect: PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "params is empty",
|
|
|
|
given: &PathParams{},
|
|
|
|
expect: PathParams{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
c.(RoutableContext).SetRawPathParams(tc.given)
|
|
|
|
|
|
|
|
assert.EqualValues(t, tc.expect, c.PathParams())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_PathParam(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
given *PathParams
|
|
|
|
whenParamName string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "param exists",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
whenParamName: "uid",
|
|
|
|
expect: "101",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple same param values exists - return first",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
{Name: "uid", Value: "202"},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
whenParamName: "uid",
|
|
|
|
expect: "101",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "param does not exists",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
},
|
|
|
|
whenParamName: "nope",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
c.(RoutableContext).SetRawPathParams(tc.given)
|
|
|
|
|
|
|
|
assert.EqualValues(t, tc.expect, c.PathParam(tc.whenParamName))
|
|
|
|
})
|
2021-07-15 22:34:01 +02:00
|
|
|
}
|
2022-05-04 13:32:09 +02:00
|
|
|
}
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_PathParamDefault(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
given *PathParams
|
|
|
|
whenParamName string
|
|
|
|
whenDefaultValue string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "param exists",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
whenParamName: "uid",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "101",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "param exists and is empty",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: ""},
|
|
|
|
{Name: "fid", Value: "501"},
|
|
|
|
},
|
|
|
|
whenParamName: "uid",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "", // <-- this is different from QueryParamDefault behaviour
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "param does not exists",
|
|
|
|
given: &PathParams{
|
|
|
|
{Name: "uid", Value: "101"},
|
|
|
|
},
|
|
|
|
whenParamName: "nope",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "999",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
c.(RoutableContext).SetRawPathParams(tc.given)
|
|
|
|
|
|
|
|
assert.EqualValues(t, tc.expect, c.PathParamDefault(tc.whenParamName, tc.whenDefaultValue))
|
|
|
|
})
|
|
|
|
}
|
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()
|
2021-07-15 22:34:01 +02:00
|
|
|
_, err := r.Add(Route{
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/:foo",
|
|
|
|
Name: "",
|
|
|
|
Handler: func(Context) error { return nil },
|
|
|
|
Middlewares: nil,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-01-24 04:32:17 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
|
|
|
|
c := e.NewContext(req, nil)
|
2021-07-15 22:34:01 +02:00
|
|
|
|
|
|
|
params := &PathParams{{Name: "foo", Value: "101"}}
|
|
|
|
// ParamNames
|
|
|
|
c.(*DefaultContext).pathParams = params
|
2020-01-24 04:32:17 +02:00
|
|
|
|
|
|
|
// round-trip param values with modification
|
2021-07-15 22:34:01 +02:00
|
|
|
paramVals := c.PathParams()
|
|
|
|
assert.Equal(t, *params, c.PathParams())
|
|
|
|
|
|
|
|
paramVals[0] = PathParam{Name: "xxx", Value: "yyy"} // PathParams() returns copy and modifying it does nothing to context
|
|
|
|
assert.Equal(t, PathParams{{Name: "foo", Value: "101"}}, c.PathParams())
|
|
|
|
|
|
|
|
pathParams := PathParams{
|
|
|
|
{Name: "aaa", Value: "bbb"},
|
|
|
|
{Name: "ccc", Value: "ddd"},
|
|
|
|
}
|
|
|
|
c.SetPathParams(pathParams)
|
|
|
|
assert.Equal(t, pathParams, c.PathParams())
|
2020-01-24 04:32:17 +02:00
|
|
|
|
|
|
|
// shouldn't explode during Reset() afterwards!
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
c.(ServableContext).Reset(nil, nil)
|
2020-01-24 04:32:17 +02:00
|
|
|
})
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, PathParams{}, c.PathParams())
|
|
|
|
assert.Len(t, *c.(*DefaultContext).pathParams, 0)
|
|
|
|
assert.Equal(t, cap(*c.(*DefaultContext).pathParams), 1)
|
2020-01-24 04:32:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:30:41 +02:00
|
|
|
// Issue #1655
|
2021-07-15 22:34:01 +02:00
|
|
|
func TestContext_SetParamNamesShouldNotModifyPathParams(t *testing.T) {
|
2020-10-28 06:30:41 +02:00
|
|
|
e := New()
|
2021-07-15 22:34:01 +02:00
|
|
|
c := e.NewContext(nil, nil).(*DefaultContext)
|
2020-10-28 06:30:41 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, 0, e.contextPathParamAllocSize)
|
|
|
|
expectedTwoParams := &PathParams{
|
|
|
|
{Name: "1", Value: "one"},
|
|
|
|
{Name: "2", Value: "two"},
|
|
|
|
}
|
|
|
|
c.SetRawPathParams(expectedTwoParams)
|
|
|
|
assert.Equal(t, 0, e.contextPathParamAllocSize)
|
|
|
|
assert.Equal(t, *expectedTwoParams, c.PathParams())
|
2020-10-28 06:30:41 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
expectedThreeParams := PathParams{
|
|
|
|
{Name: "1", Value: "one"},
|
|
|
|
{Name: "2", Value: "two"},
|
|
|
|
{Name: "3", Value: "three"},
|
|
|
|
}
|
|
|
|
c.SetPathParams(expectedThreeParams)
|
|
|
|
assert.Equal(t, 0, e.contextPathParamAllocSize)
|
|
|
|
assert.Equal(t, expectedThreeParams, c.PathParams())
|
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
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, "Jon Snow", c.FormValue("name"))
|
|
|
|
assert.Equal(t, "jon@labstack.com", c.FormValue("email"))
|
|
|
|
|
|
|
|
// FormValueDefault
|
|
|
|
assert.Equal(t, "Jon Snow", c.FormValueDefault("name", "nope"))
|
|
|
|
assert.Equal(t, "default", c.FormValueDefault("missing", "default"))
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// FormValues
|
|
|
|
values, err := c.FormValues()
|
|
|
|
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"},
|
2021-07-15 22:34:01 +02:00
|
|
|
}, values)
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
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)
|
2021-07-15 22:34:01 +02:00
|
|
|
values, err = c.FormValues()
|
|
|
|
assert.Nil(t, values)
|
|
|
|
assert.Error(t, err)
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_QueryParams(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenURL string
|
|
|
|
expect url.Values
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "multiple values in url",
|
|
|
|
givenURL: "/?test=1&test=2&email=jon%40labstack.com",
|
|
|
|
expect: url.Values{
|
|
|
|
"test": []string{"1", "2"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single value in url",
|
|
|
|
givenURL: "/?nope=1",
|
|
|
|
expect: url.Values{
|
|
|
|
"nope": []string{"1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no query params in url",
|
|
|
|
givenURL: "/?",
|
|
|
|
expect: url.Values{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.givenURL, nil)
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expect, c.QueryParams())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 22:34:01 +02:00
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_QueryParam(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenURL string
|
|
|
|
whenParamName string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "value exists in url",
|
|
|
|
givenURL: "/?test=1",
|
|
|
|
whenParamName: "test",
|
|
|
|
expect: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple values exists in url",
|
|
|
|
givenURL: "/?test=9&test=8",
|
|
|
|
whenParamName: "test",
|
|
|
|
expect: "9", // <-- first value in returned
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value does not exists in url",
|
|
|
|
givenURL: "/?nope=1",
|
|
|
|
whenParamName: "test",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value is empty in url",
|
|
|
|
givenURL: "/?test=",
|
|
|
|
whenParamName: "test",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.givenURL, nil)
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expect, c.QueryParam(tc.whenParamName))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 07:25:34 +02:00
|
|
|
|
2022-05-04 13:32:09 +02:00
|
|
|
func TestContext_QueryParamDefault(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenURL string
|
|
|
|
whenParamName string
|
|
|
|
whenDefaultValue string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "value exists in url",
|
|
|
|
givenURL: "/?test=1",
|
|
|
|
whenParamName: "test",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value does not exists in url",
|
|
|
|
givenURL: "/?nope=1",
|
|
|
|
whenParamName: "test",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "999",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value is empty in url",
|
|
|
|
givenURL: "/?test=",
|
|
|
|
whenParamName: "test",
|
|
|
|
whenDefaultValue: "999",
|
|
|
|
expect: "999",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.givenURL, nil)
|
|
|
|
e := New()
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expect, c.QueryParamDefault(tc.whenParamName, tc.whenDefaultValue))
|
|
|
|
})
|
|
|
|
}
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextFormFile(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mr := multipart.NewWriter(buf)
|
|
|
|
w, err := mr.CreateFormFile("file", "test")
|
2021-07-15 22:34:01 +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")
|
2021-07-15 22:34:01 +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()
|
2021-07-15 22:34:01 +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)
|
2021-07-15 22:34:01 +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-07-15 22:34:01 +02:00
|
|
|
var c Context = new(DefaultContext)
|
2016-06-06 07:25:34 +02:00
|
|
|
c.Set("name", "Jon Snow")
|
2021-07-15 22:34:01 +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{}
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
c := &DefaultContext{
|
2019-02-12 20:56:26 +02:00
|
|
|
echo: e,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
c.Set("name", "Jon Snow")
|
|
|
|
if c.Get("name") != "Jon Snow" {
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Error(t, c.Validate(struct{}{}))
|
2019-02-19 20:18:42 +02:00
|
|
|
|
|
|
|
e.Validator = &validator{}
|
2021-07-15 22:34:01 +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"
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?"+queryString, nil)
|
2019-02-19 20:18:42 +02:00
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, queryString, c.QueryString())
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Request(t *testing.T) {
|
2021-07-15 22:34:01 +02:00
|
|
|
var c Context = new(DefaultContext)
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Nil(t, c.Request())
|
2019-02-19 20:18:42 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/path", nil)
|
2019-02-19 20:18:42 +02:00
|
|
|
c.SetRequest(req)
|
|
|
|
|
2021-07-15 22:34:01 +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
|
|
|
|
}{
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
TLS: &tls.ConnectionState{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedProto: []string{"https"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedProtocol: []string{"http"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"http",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedSsl: []string{"on"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXUrlScheme: []string{"https"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{},
|
|
|
|
},
|
|
|
|
"http",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-07-15 22:34:01 +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
|
2021-07-15 22:34:01 +02:00
|
|
|
ws assert.BoolAssertionFunc
|
2019-02-19 20:18:42 +02:00
|
|
|
}{
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"websocket"}},
|
|
|
|
},
|
|
|
|
},
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.True,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"Websocket"}},
|
|
|
|
},
|
|
|
|
},
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.True,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{},
|
|
|
|
},
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.False,
|
2019-02-19 20:18:42 +02:00
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderUpgrade: []string{"other"}},
|
|
|
|
},
|
|
|
|
},
|
2021-07-15 22:34:01 +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()
|
2021-07-15 22:34:01 +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)
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &user{1, "Jon Snow"}, u)
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_RealIP(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
c Context
|
|
|
|
s string
|
|
|
|
}{
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
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
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2021-05-20 16:51:15 +02:00
|
|
|
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
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2020-09-16 04:36:43 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"127.0.0.1",
|
|
|
|
},
|
2019-02-19 20:18:42 +02:00
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"X-Real-Ip": []string{"192.168.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"192.168.0.1",
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 22:34:01 +02:00
|
|
|
&DefaultContext{
|
2019-02-19 20:18:42 +02:00
|
|
|
request: &http.Request{
|
|
|
|
RemoteAddr: "89.89.89.89:1654",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"89.89.89.89",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.Equal(t, tt.s, tt.c.RealIP())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_File(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenFile string
|
|
|
|
whenFS fs.FS
|
|
|
|
expectStatus int
|
|
|
|
expectStartsWith []byte
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok, from default file system",
|
|
|
|
whenFile: "_fixture/images/walle.png",
|
|
|
|
whenFS: nil,
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok, from custom file system",
|
|
|
|
whenFile: "walle.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, not existent file",
|
|
|
|
whenFile: "not.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: nil,
|
|
|
|
expectError: "code=404, message=Not Found",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
if tc.whenFS != nil {
|
|
|
|
e.Filesystem = tc.whenFS
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := func(ec Context) error {
|
|
|
|
return ec.(*DefaultContext).File(tc.whenFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
err := handler(c)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := rec.Body.Bytes()
|
|
|
|
if len(body) > len(tc.expectStartsWith) {
|
|
|
|
body = body[:len(tc.expectStartsWith)]
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectStartsWith, body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_FileFS(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenFile string
|
|
|
|
whenFS fs.FS
|
|
|
|
expectStatus int
|
|
|
|
expectStartsWith []byte
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
whenFile: "walle.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, not existent file",
|
|
|
|
whenFile: "not.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: nil,
|
|
|
|
expectError: "code=404, message=Not Found",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
handler := func(ec Context) error {
|
|
|
|
return ec.(*DefaultContext).FileFS(tc.whenFile, tc.whenFS)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
err := handler(c)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
|
|
if tc.expectError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectError)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := rec.Body.Bytes()
|
|
|
|
if len(body) > len(tc.expectStartsWith) {
|
|
|
|
body = body[:len(tc.expectStartsWith)]
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectStartsWith, body)
|
|
|
|
})
|
2019-02-19 20:18:42 +02:00
|
|
|
}
|
2016-04-12 01:49:20 +02:00
|
|
|
}
|