1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 04:17:03 +00:00

Add tests for context binders, render policy and readform

Former-commit-id: 66d7c86e8c0ec1cb9fcc2d77b40aae5335ff5961
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-02-21 13:26:30 +02:00
parent 5872eb1b12
commit 6fca78d12a
3 changed files with 260 additions and 3 deletions

View File

@@ -1,13 +1,18 @@
package iris_test
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/iris-contrib/httpexpect"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/httptest"
@@ -240,6 +245,173 @@ func TestLimitRequestBodySizeMiddleware(t *testing.T) {
}
type testBinderData struct {
Username string
Mail string
Data []string `form:"mydata" json:"mydata"`
}
type testBinderXMLData struct {
XMLName xml.Name `xml:"info"`
FirstAttr string `xml:"first,attr"`
SecondAttr string `xml:"second,attr"`
Name string `xml:"name" json:"name"`
Birth string `xml:"birth" json:"birth"`
Stars int `xml:"stars" json:"stars"`
}
type testBinder struct {
//pointer of testBinderDataJSON or testBinderXMLData
vp interface{}
m iris.Unmarshaler
shouldError bool
}
func (tj *testBinder) Decode(data []byte) error {
if tj.shouldError {
return fmt.Errorf("Should error")
}
return tj.m.Unmarshal(data, tj.vp)
}
func testUnmarshaler(app *iris.Framework, t *testing.T, tb *testBinder,
write func(ctx *iris.Context)) *httpexpect.Request {
// a very dirty and awful way but here we must test in deep
// the custom object's decoder error with the custom
// unmarshaler result whenever the testUnmarshaler called.
if tb.shouldError == false {
tb.shouldError = true
testUnmarshaler(app, t, tb, write)
tb.shouldError = false
}
h := func(ctx *iris.Context) {
err := ctx.UnmarshalBody(tb.vp, tb.m)
if tb.shouldError && err == nil {
t.Fatalf("Should prompted for error 'Should error' but not error returned from the custom decoder!")
} else if err != nil {
t.Fatalf("Error when parsing the body: %s", err.Error())
}
if write != nil {
write(ctx)
}
if app.Config.DisableBodyConsumptionOnUnmarshal {
rawData, _ := ioutil.ReadAll(ctx.Request.Body)
if len(rawData) == 0 {
t.Fatalf("Expected data to NOT BE consumed by the previous UnmarshalBody call but we got empty body.")
}
}
}
app.Post("/bind_req_body", h)
e := httptest.New(app, t)
return e.POST("/bind_req_body")
}
// same as DecodeBody
// JSON, XML by DecodeBody passing the default unmarshalers
func TestContextBinders(t *testing.T) {
passed := map[string]interface{}{"Username": "myusername",
"Mail": "mymail@iris-go.com",
"mydata": []string{"mydata1", "mydata2"}}
expectedObject := testBinderData{Username: "myusername",
Mail: "mymail@iris-go.com",
Data: []string{"mydata1", "mydata2"}}
// JSON
vJSON := &testBinder{&testBinderData{},
iris.UnmarshalerFunc(json.Unmarshal), false}
// XML
expectedObj := testBinderXMLData{
XMLName: xml.Name{Local: "info", Space: "info"},
FirstAttr: "this is the first attr",
SecondAttr: "this is the second attr",
Name: "Iris web framework",
Birth: "13 March 2016",
Stars: 5758,
}
expectedAndPassedObjText := `<` + expectedObj.XMLName.Local + ` first="` +
expectedObj.FirstAttr + `" second="` +
expectedObj.SecondAttr + `"><name>` +
expectedObj.Name + `</name><birth>` +
expectedObj.Birth + `</birth><stars>` +
strconv.Itoa(expectedObj.Stars) + `</stars></info>`
vXML := &testBinder{&testBinderXMLData{},
iris.UnmarshalerFunc(xml.Unmarshal), false}
app := iris.New()
app.Adapt(httprouter.New())
testUnmarshaler(app,
t,
vXML,
func(ctx *iris.Context) {
ctx.XML(iris.StatusOK, vXML.vp)
}).
WithText(expectedAndPassedObjText).
Expect().
Status(iris.StatusOK).
Body().Equal(expectedAndPassedObjText)
app2 := iris.New()
app2.Adapt(httprouter.New())
testUnmarshaler(app2,
t,
vJSON,
func(ctx *iris.Context) {
ctx.JSON(iris.StatusOK, vJSON.vp)
}).
WithJSON(passed).
Expect().
Status(iris.StatusOK).
JSON().Object().Equal(expectedObject)
// JSON with DisableBodyConsumptionOnUnmarshal
app3 := iris.New()
app3.Adapt(httprouter.New())
app3.Config.DisableBodyConsumptionOnUnmarshal = true
testUnmarshaler(app3,
t,
vJSON,
func(ctx *iris.Context) {
ctx.JSON(iris.StatusOK, vJSON.vp)
}).
WithJSON(passed).
Expect().
Status(iris.StatusOK).
JSON().Object().Equal(expectedObject)
}
func TestContextReadForm(t *testing.T) {
app := iris.New()
app.Adapt(httprouter.New())
app.Post("/form", func(ctx *iris.Context) {
obj := testBinderData{}
err := ctx.ReadForm(&obj)
if err != nil {
t.Fatalf("Error when parsing the FORM: %s", err.Error())
}
ctx.JSON(iris.StatusOK, obj)
})
e := httptest.New(app, t)
passed := map[string]interface{}{"Username": "myusername", "Mail": "mymail@iris-go.com", "mydata": url.Values{"[0]": []string{"mydata1"},
"[1]": []string{"mydata2"}}}
expectedObject := testBinderData{Username: "myusername", Mail: "mymail@iris-go.com", Data: []string{"mydata1", "mydata2"}}
e.POST("/form").WithForm(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
}
func TestRedirectHTTP(t *testing.T) {
host := "localhost:" + strconv.Itoa(getRandomNumber(1717, 9281))
@@ -341,7 +513,6 @@ func TestContextUserValues(t *testing.T) {
e := httptest.New(app, t)
e.GET("/test").Expect().Status(iris.StatusOK)
}
func TestContextCookieSetGetRemove(t *testing.T) {