diff --git a/test/mux_test.go b/test/mux_test.go index 17dde71a..aa1512e5 100644 --- a/test/mux_test.go +++ b/test/mux_test.go @@ -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") } } diff --git a/test/render_test.go b/test/render_test.go new file mode 100644 index 00000000..e04d64a4 --- /dev/null +++ b/test/render_test.go @@ -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 + `">` + JSONXMLContents.Name + `` + JSONXMLContents.Birth + `` + strconv.Itoa(JSONXMLContents.Stars) + ``) + + markdownT := e.GET("/markdown").Expect() + markdownT.Header("Content-Type").Equal("text/html; charset=UTF-8") + markdownT.Body().Equal("

" + markdownContents[2:] + "

\n") + +} diff --git a/test/sessions_test.go b/test/sessions_test.go index 89807ca5..47d8a078 100644 --- a/test/sessions_test.go +++ b/test/sessions_test.go @@ -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() }