Add tests for context body reader and for mux api (.API)

This commit is contained in:
Makis Maropoulos 2016-07-02 17:44:01 +02:00
parent 50b1366e9d
commit 263fc77e5c
3 changed files with 149 additions and 1 deletions

View File

@ -0,0 +1,90 @@
package test
import (
"encoding/xml"
"net/url"
"strconv"
"testing"
"github.com/kataras/iris"
)
/* Contains tests for context.ReadJSON/ReadXML/ReadFORM */
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"`
}
func TestBindForm(t *testing.T) {
api := iris.New()
api.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 := tester(api, 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 TestBindJSON(t *testing.T) {
api := iris.New()
api.Post("/json", func(ctx *iris.Context) {
obj := testBinderData{}
err := ctx.ReadJSON(&obj)
if err != nil {
t.Fatalf("Error when parsing the JSON body: %s", err.Error())
}
ctx.JSON(iris.StatusOK, obj)
})
e := tester(api, 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"}}
e.POST("/json").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
}
func TestBindXML(t *testing.T) {
api := iris.New()
api.Post("/xml", func(ctx *iris.Context) {
obj := testBinderXMLData{}
err := ctx.ReadXML(&obj)
if err != nil {
t.Fatalf("Error when parsing the XML body: %s", err.Error())
}
ctx.XML(iris.StatusOK, obj)
})
e := tester(api, t)
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: 4064,
}
// so far no WithXML or .XML like WithJSON and .JSON on httpexpect I added a feature request as post issue and we're waiting
expectedBody := `<` + expectedObj.XMLName.Local + ` first="` + expectedObj.FirstAttr + `" second="` + expectedObj.SecondAttr + `"><name>` + expectedObj.Name + `</name><birth>` + expectedObj.Birth + `</birth><stars>` + strconv.Itoa(expectedObj.Stars) + `</stars></info>`
e.POST("/xml").WithText(expectedBody).Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
}

View File

@ -229,3 +229,61 @@ func TestMuxCustomErrors(t *testing.T) {
Status(r.Status).Body().Equal(r.Body)
}
}
type testUserAPI struct {
*iris.Context
}
// GET /users
func (u testUserAPI) Get() {
u.Write("Get Users\n")
}
// GET /users/:param1 which its value passed to the id argument
func (u testUserAPI) GetBy(id string) { // id equals to u.Param("param1")
u.Write("Get By %s\n", id)
}
// PUT /users
func (u testUserAPI) Put() {
u.Write("Put, name: %s\n", u.FormValue("name"))
}
// POST /users/:param1
func (u testUserAPI) PostBy(id string) {
u.Write("Post By %s, name: %s\n", id, u.FormValue("name"))
}
// DELETE /users/:param1
func (u testUserAPI) DeleteBy(id string) {
u.Write("Delete By %s\n", id)
}
func TestMuxAPI(t *testing.T) {
api := iris.New()
middlewareResponseText := "I assume that you are authenticated\n"
api.API("/users", testUserAPI{}, func(ctx *iris.Context) { // optional middleware for .API
// do your work here, or render a login window if not logged in, get the user and send it to the next middleware, or do all here
ctx.Set("user", "username")
ctx.Next()
}, func(ctx *iris.Context) {
if ctx.Get("user") == "username" {
ctx.Write(middlewareResponseText)
ctx.Next()
} else {
ctx.SetStatusCode(iris.StatusUnauthorized)
}
})
e := tester(api, t)
userID := "4077"
formname := "kataras"
e.GET("/users").Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get Users\n")
e.GET("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get By " + userID + "\n")
e.PUT("/users").WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Put, name: " + formname + "\n")
e.POST("/users/"+userID).WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n")
e.DELETE("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Delete By " + userID + "\n")
}

View File

@ -1,7 +1,7 @@
//Package test -v ./... builds all tests
package test
// Contains tests for sessions
// Contains tests for sessions(sessions package) & flash messages(context)
import (
"testing"