mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add some tests
This commit is contained in:
parent
9be49bbfee
commit
f4a2f35191
|
@ -1,5 +1,7 @@
|
|||
package test
|
||||
|
||||
// Contains tests for the mux(router)
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
@ -21,7 +23,7 @@ type route struct {
|
|||
Status int
|
||||
Register bool
|
||||
Params []param
|
||||
UrlParams []param
|
||||
URLParams []param
|
||||
}
|
||||
|
||||
func TestMuxSimple(t *testing.T) {
|
||||
|
@ -64,15 +66,15 @@ func TestMuxSimple(t *testing.T) {
|
|||
ctx.SetStatusCode(r.Status)
|
||||
if r.Params != nil && len(r.Params) > 0 {
|
||||
ctx.SetBodyString(ctx.Params.String())
|
||||
} else if r.UrlParams != nil && len(r.UrlParams) > 0 {
|
||||
if len(r.UrlParams) != len(ctx.URLParams()) {
|
||||
t.Fatalf("Error when comparing length of url parameters %d != %d", len(r.UrlParams), len(ctx.URLParams()))
|
||||
} else if r.URLParams != nil && len(r.URLParams) > 0 {
|
||||
if len(r.URLParams) != len(ctx.URLParams()) {
|
||||
t.Fatalf("Error when comparing length of url parameters %d != %d", len(r.URLParams), len(ctx.URLParams()))
|
||||
}
|
||||
paramsKeyVal := ""
|
||||
for idxp, p := range r.UrlParams {
|
||||
for idxp, p := range r.URLParams {
|
||||
val := ctx.URLParam(p.Key)
|
||||
paramsKeyVal += p.Key + "=" + val + ","
|
||||
if idxp == len(r.UrlParams)-1 {
|
||||
if idxp == len(r.URLParams)-1 {
|
||||
paramsKeyVal = paramsKeyVal[0 : len(paramsKeyVal)-1]
|
||||
}
|
||||
}
|
||||
|
@ -140,17 +142,17 @@ func TestMuxSimpleParty(t *testing.T) {
|
|||
request("/party1/namedpath/theparam1/something/theparam2/else")
|
||||
|
||||
if enable_subdomain_tests {
|
||||
subdomain_request := func(reqPath string) {
|
||||
subdomainRequest := func(reqPath string) {
|
||||
e.Request("GET", subdomainURL+reqPath).
|
||||
Expect().
|
||||
Status(iris.StatusOK).Body().Equal(subdomainHost + reqPath)
|
||||
}
|
||||
|
||||
subdomain_request("/")
|
||||
subdomain_request("/path1")
|
||||
subdomain_request("/path2")
|
||||
subdomain_request("/namedpath/theparam1/something/theparam2")
|
||||
subdomain_request("/namedpath/theparam1/something/theparam2/else")
|
||||
subdomainRequest("/")
|
||||
subdomainRequest("/path1")
|
||||
subdomainRequest("/path2")
|
||||
subdomainRequest("/namedpath/theparam1/something/theparam2")
|
||||
subdomainRequest("/namedpath/theparam1/something/theparam2/else")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
87
test/render_test.go
Normal file
87
test/render_test.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package test
|
||||
|
||||
// Contains tests for render/rest
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
type renderTestInformationType 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 TestREST(t *testing.T) {
|
||||
api := iris.New()
|
||||
|
||||
dataContents := []byte("Some binary data here.")
|
||||
textContents := "Plain text here"
|
||||
JSONPContents := map[string]string{"hello": "jsonp"}
|
||||
JSONPCallback := "callbackName"
|
||||
JSONXMLContents := renderTestInformationType{
|
||||
XMLName: xml.Name{Local: "info", Space: "info"}, // only need to verify that later
|
||||
FirstAttr: "this is the first attr",
|
||||
SecondAttr: "this is the second attr",
|
||||
Name: "Iris web framework",
|
||||
Birth: "13 March 2016",
|
||||
Stars: 4064,
|
||||
}
|
||||
markdownContents := "# Hello dynamic markdown from Iris"
|
||||
|
||||
api.Get("/data", func(ctx *iris.Context) {
|
||||
ctx.Data(iris.StatusOK, dataContents)
|
||||
})
|
||||
|
||||
api.Get("/text", func(ctx *iris.Context) {
|
||||
ctx.Text(iris.StatusOK, textContents)
|
||||
})
|
||||
|
||||
api.Get("/jsonp", func(ctx *iris.Context) {
|
||||
ctx.JSONP(iris.StatusOK, JSONPCallback, JSONPContents)
|
||||
})
|
||||
|
||||
api.Get("/json", func(ctx *iris.Context) {
|
||||
ctx.JSON(iris.StatusOK, JSONXMLContents)
|
||||
})
|
||||
api.Get("/xml", func(ctx *iris.Context) {
|
||||
ctx.XML(iris.StatusOK, JSONXMLContents)
|
||||
})
|
||||
|
||||
api.Get("/markdown", func(ctx *iris.Context) {
|
||||
ctx.Markdown(iris.StatusOK, markdownContents)
|
||||
})
|
||||
|
||||
e := tester(api, t)
|
||||
dataT := e.GET("/data").Expect()
|
||||
dataT.Header("Content-Type").Equal("application/octet-stream")
|
||||
dataT.Body().Equal(string(dataContents))
|
||||
|
||||
textT := e.GET("/text").Expect()
|
||||
textT.Header("Content-Type").Equal("text/plain; charset=UTF-8")
|
||||
textT.Body().Equal(textContents)
|
||||
|
||||
/* JSONPT := e.GET("/jsonp").Expect()
|
||||
dataT.Header("Content-Type").Equal("application/javascript; charset=UTF-8")
|
||||
dataT.Body().Equal(dataContents)*/
|
||||
|
||||
JSONT := e.GET("/json").Expect()
|
||||
JSONT.Header("Content-Type").Equal("application/json; charset=UTF-8")
|
||||
JSONT.JSON().Object().Equal(JSONXMLContents)
|
||||
|
||||
XMLT := e.GET("/xml").Expect()
|
||||
XMLT.Header("Content-Type").Equal("text/xml; charset=UTF-8")
|
||||
XMLT.Body().Equal(`<` + JSONXMLContents.XMLName.Local + ` first="` + JSONXMLContents.FirstAttr + `" second="` + JSONXMLContents.SecondAttr + `"><name>` + JSONXMLContents.Name + `</name><birth>` + JSONXMLContents.Birth + `</birth><stars>` + strconv.Itoa(JSONXMLContents.Stars) + `</stars></info>`)
|
||||
|
||||
markdownT := e.GET("/markdown").Expect()
|
||||
markdownT.Header("Content-Type").Equal("text/html; charset=UTF-8")
|
||||
markdownT.Body().Equal("<h1>" + markdownContents[2:] + "</h1>\n")
|
||||
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
//Package test -v ./... builds all tests
|
||||
package test
|
||||
|
||||
// Contains tests for sessions
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
|
@ -8,7 +10,6 @@ import (
|
|||
)
|
||||
|
||||
func TestSessions(t *testing.T) {
|
||||
sessionId := "mycustomsessionid"
|
||||
|
||||
values := map[string]interface{}{
|
||||
"Name": "iris",
|
||||
|
@ -17,8 +18,8 @@ func TestSessions(t *testing.T) {
|
|||
}
|
||||
|
||||
api := iris.New()
|
||||
api.Config.Sessions.Cookie = "mycustomsessionid"
|
||||
|
||||
api.Config.Sessions.Cookie = sessionId
|
||||
writeValues := func(ctx *iris.Context) {
|
||||
sessValues := ctx.Session().GetAll()
|
||||
ctx.JSON(iris.StatusOK, sessValues)
|
||||
|
@ -55,19 +56,19 @@ func TestSessions(t *testing.T) {
|
|||
// the cookie and all values should be empty
|
||||
})
|
||||
|
||||
h := tester(api, t)
|
||||
e := tester(api, t)
|
||||
|
||||
h.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
|
||||
h.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
|
||||
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
|
||||
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
|
||||
if enable_subdomain_tests {
|
||||
h.Request("GET", subdomainURL+"/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
|
||||
e.Request("GET", subdomainURL+"/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
|
||||
}
|
||||
|
||||
// test destory which also clears first
|
||||
d := h.GET("/destroy").Expect().Status(iris.StatusOK)
|
||||
d := e.GET("/destroy").Expect().Status(iris.StatusOK)
|
||||
d.JSON().Object().Empty()
|
||||
d.Cookies().ContainsOnly(sessionId)
|
||||
d.Cookies().ContainsOnly(api.Config.Sessions.Cookie)
|
||||
// set and clear again
|
||||
h.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
|
||||
h.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
|
||||
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
|
||||
e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user