diff --git a/README.md b/README.md index 5bd2e1dc..925a7d13 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Iris is an efficient and well-designed toolbox with robust set of features.
CHANGELOG/HISTORY -Examples +Examples for new Gophers Docs @@ -190,11 +190,20 @@ Documentation - - The most important is to know where to find the [details](https://godoc.org/gopkg.in/kataras/iris.v6) +- New GOpher? Welcome! Take a look at the [GopherBOOk](http://gopherbook.iris-go.com/) - - [./adaptors](https://github.com/kataras/iris/tree/v6/adaptors) and [./middleware](https://github.com/kataras/iris/tree/v6/middleware) contains examples for their usage. +- Numerous of examples can be found [here](https://github.com/iris-contrib/examples) - - [HISTORY.md](https://github.com//kataras/iris/tree/v6/HISTORY.md) is your best friend, version migrations are released there. +- [./adaptors](https://github.com/kataras/iris/tree/v6/adaptors) and [./middleware](https://github.com/kataras/iris/tree/v6/middleware) contains examples of their usage. + +- [HISTORY.md](https://github.com//kataras/iris/tree/v6/HISTORY.md) is your best friend, version migrations are released there. + +- The most important is to know where to find the [details](https://godoc.org/gopkg.in/kataras/iris.v6) + + +### FAQ + +Explore [these questions](https://github.com/kataras/iris/issues?q=label%3Aquestion) and join to our [community chat][Chat]! Testing @@ -208,10 +217,9 @@ A simple test is located to [./httptest/_example/main_test.go](https://github.co Read more about [gavv's httpexpect](https://github.com/gavv/httpexpect). -FAQ ------------ -Explore [these questions](https://github.com/kataras/iris/issues?q=label%3Aquestion) and join to our [community chat][Chat]! + + Philosophy @@ -242,7 +250,7 @@ Today I spend all my days and nights coding for Iris, and I'm happy about this, - Star the project, will help you to follow the upcoming features. - [Donate](https://github.com/kataras/iris/blob/master/DONATIONS.md), if you can afford any cost. - Write an article about Iris or even post a Tweet. -- Do Pull Requests on the [iris-contrib](https://github.com/iris-contrib) organisation's repositories, like book and examples. +- Do Pull Requests on the [iris-contrib](https://github.com/iris-contrib) organisation's repositories, like [book](https://github.com/iris-contrib/gitbook), [examples](https://github.com/iris-contrib/examples) and to [gopherbook](http://gopherbook.iris-go.com/). If you are interested in contributing to the Iris project, please see the document [CONTRIBUTING](https://github.com/kataras/iris/blob/master/.github/CONTRIBUTING.md). diff --git a/adaptors/gorillamux/gorillamux_test.go b/adaptors/gorillamux/gorillamux_test.go index 7a3ef3c6..3ad6b999 100644 --- a/adaptors/gorillamux/gorillamux_test.go +++ b/adaptors/gorillamux/gorillamux_test.go @@ -1,12 +1,290 @@ package gorillamux_test import ( + "math/rand" + "strconv" "testing" + "time" + "github.com/iris-contrib/httpexpect" "gopkg.in/kataras/iris.v6" "gopkg.in/kataras/iris.v6/adaptors/gorillamux" + "gopkg.in/kataras/iris.v6/httptest" ) +func getRandomNumber(min int, max int) int { + rand.Seed(time.Now().Unix()) + return rand.Intn(max-min) + min +} + +const ( + testEnableSubdomain = true + testSubdomain = "mysubdomain" +) + +func testSubdomainHost(host string) string { + s := testSubdomain + "." + host + return s +} + +func testSubdomainURL(scheme string, host string) string { + subdomainHost := testSubdomainHost(host) + return scheme + subdomainHost +} + +func subdomainTester(e *httpexpect.Expect, app *iris.Framework) *httpexpect.Expect { + es := e.Builder(func(req *httpexpect.Request) { + req.WithURL(testSubdomainURL(app.Config.VScheme, app.Config.VHost)) + }) + return es +} + +type param struct { + Key string + Value string +} + +type testRoute struct { + Method string + Path string + RequestPath string + RequestQuery string + Body string + Status int + Register bool + Params []param + URLParams []param +} + +func newApp() *iris.Framework { + app := iris.New() + app.Adapt(gorillamux.New()) + + return app +} + +func TestMuxSimple(t *testing.T) { + app := newApp() + + testRoutes := []testRoute{ + // FOUND - registered + {"GET", "/test_get", "/test_get", "", "hello, get!", 200, true, nil, nil}, + {"POST", "/test_post", "/test_post", "", "hello, post!", 200, true, nil, nil}, + {"PUT", "/test_put", "/test_put", "", "hello, put!", 200, true, nil, nil}, + {"DELETE", "/test_delete", "/test_delete", "", "hello, delete!", 200, true, nil, nil}, + {"HEAD", "/test_head", "/test_head", "", "hello, head!", 200, true, nil, nil}, + {"OPTIONS", "/test_options", "/test_options", "", "hello, options!", 200, true, nil, nil}, + {"CONNECT", "/test_connect", "/test_connect", "", "hello, connect!", 200, true, nil, nil}, + {"PATCH", "/test_patch", "/test_patch", "", "hello, patch!", 200, true, nil, nil}, + {"TRACE", "/test_trace", "/test_trace", "", "hello, trace!", 200, true, nil, nil}, + // NOT FOUND - not registered + {"GET", "/test_get_nofound", "/test_get_nofound", "", "Not Found", 404, false, nil, nil}, + {"POST", "/test_post_nofound", "/test_post_nofound", "", "Not Found", 404, false, nil, nil}, + {"PUT", "/test_put_nofound", "/test_put_nofound", "", "Not Found", 404, false, nil, nil}, + {"DELETE", "/test_delete_nofound", "/test_delete_nofound", "", "Not Found", 404, false, nil, nil}, + {"HEAD", "/test_head_nofound", "/test_head_nofound", "", "Not Found", 404, false, nil, nil}, + {"OPTIONS", "/test_options_nofound", "/test_options_nofound", "", "Not Found", 404, false, nil, nil}, + {"CONNECT", "/test_connect_nofound", "/test_connect_nofound", "", "Not Found", 404, false, nil, nil}, + {"PATCH", "/test_patch_nofound", "/test_patch_nofound", "", "Not Found", 404, false, nil, nil}, + {"TRACE", "/test_trace_nofound", "/test_trace_nofound", "", "Not Found", 404, false, nil, nil}, + // Parameters + {"GET", "/test_get_parameter1/{name}", "/test_get_parameter1/iris", "", "name=iris", 200, true, []param{{"name", "iris"}}, nil}, + {"GET", "/test_get_parameter2/{name}/details/{something}", "/test_get_parameter2/iris/details/anything", "", "name=iris,something=anything", 200, true, []param{{"name", "iris"}, {"something", "anything"}}, nil}, + {"GET", "/test_get_parameter2/{name}/details/{something}/{else:.*}", "/test_get_parameter2/iris/details/anything/elsehere", "", "name=iris,something=anything,else=elsehere", 200, true, []param{{"name", "iris"}, {"something", "anything"}, {"else", "elsehere"}}, nil}, + // URL Parameters + {"GET", "/test_get_urlparameter1/first", "/test_get_urlparameter1/first", "name=irisurl", "name=irisurl", 200, true, nil, []param{{"name", "irisurl"}}}, + {"GET", "/test_get_urlparameter2/second", "/test_get_urlparameter2/second", "name=irisurl&something=anything", "name=irisurl,something=anything", 200, true, nil, []param{{"name", "irisurl"}, {"something", "anything"}}}, + {"GET", "/test_get_urlparameter2/first/second/third", "/test_get_urlparameter2/first/second/third", "name=irisurl&something=anything&else=elsehere", "name=irisurl,something=anything,else=elsehere", 200, true, nil, []param{{"name", "irisurl"}, {"something", "anything"}, {"else", "elsehere"}}}, + } + + for idx := range testRoutes { + r := testRoutes[idx] + if r.Register { + app.HandleFunc(r.Method, r.Path, func(ctx *iris.Context) { + ctx.SetStatusCode(r.Status) + if r.Params != nil && len(r.Params) > 0 { + ctx.Writef(ctx.ParamsSentence()) + } 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 { + val := ctx.URLParam(p.Key) + paramsKeyVal += p.Key + "=" + val + "," + if idxp == len(r.URLParams)-1 { + paramsKeyVal = paramsKeyVal[0 : len(paramsKeyVal)-1] + } + } + ctx.Writef(paramsKeyVal) + } else { + ctx.Writef(r.Body) + } + + }) + } + } + + e := httptest.New(app, t) + + // run the tests (1) + for idx := range testRoutes { + r := testRoutes[idx] + e.Request(r.Method, r.RequestPath).WithQueryString(r.RequestQuery). + Expect(). + Status(r.Status).Body().Equal(r.Body) + } + +} + +func TestMuxSimpleParty(t *testing.T) { + app := newApp() + + h := func(ctx *iris.Context) { ctx.WriteString(ctx.Host() + ctx.Path()) } + + if testEnableSubdomain { + subdomainParty := app.Party(testSubdomain + ".") + { + subdomainParty.Get("/", h) + subdomainParty.Get("/path1", h) + subdomainParty.Get("/path2", h) + subdomainParty.Get("/namedpath/{param1}/something/{param2}", h) + subdomainParty.Get("/namedpath/{param1}/something/{param2}/else", h) + } + } + + // simple + p := app.Party("/party1") + { + p.Get("/", h) + p.Get("/path1", h) + p.Get("/path2", h) + p.Get("/namedpath/{param1}/something/{param2}", h) + p.Get("/namedpath/{param1}/something/{param2}/else", h) + } + + app.Config.VHost = "0.0.0.0:" + strconv.Itoa(getRandomNumber(2222, 2399)) + // app.Config.Tester.Debug = true + // app.Config.Tester.ExplicitURL = true + e := httptest.New(app, t) + + request := func(reqPath string) { + + e.Request("GET", reqPath). + Expect(). + Status(iris.StatusOK).Body().Equal(app.Config.VHost + reqPath) + } + + // run the tests + request("/party1/") + request("/party1/path1") + request("/party1/path2") + request("/party1/namedpath/theparam1/something/theparam2") + request("/party1/namedpath/theparam1/something/theparam2/else") + + if testEnableSubdomain { + es := subdomainTester(e, app) + subdomainRequest := func(reqPath string) { + es.Request("GET", reqPath). + Expect(). + Status(iris.StatusOK).Body().Equal(testSubdomainHost(app.Config.VHost) + reqPath) + } + + subdomainRequest("/") + subdomainRequest("/path1") + subdomainRequest("/path2") + subdomainRequest("/namedpath/theparam1/something/theparam2") + subdomainRequest("/namedpath/theparam1/something/theparam2/else") + } +} + +func TestMuxPathEscape(t *testing.T) { + app := newApp() + + app.Get("/details/{name}", func(ctx *iris.Context) { + name := ctx.Param("name") + highlight := ctx.URLParam("highlight") + ctx.Writef("name=%s,highlight=%s", name, highlight) + }) + + e := httptest.New(app, t) + + e.GET("/details/Sakamoto desu ga"). + WithQuery("highlight", "text"). + Expect().Status(iris.StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text") +} + +func TestMuxParamDecodedDecodeURL(t *testing.T) { + app := newApp() + + app.Get("/encoding/{url}", func(ctx *iris.Context) { + url := iris.DecodeURL(ctx.ParamDecoded("url")) + ctx.SetStatusCode(iris.StatusOK) + ctx.WriteString(url) + }) + + e := httptest.New(app, t) + + e.GET("/encoding/http%3A%2F%2Fsome-url.com").Expect().Status(iris.StatusOK).Body().Equal("http://some-url.com") +} + +func TestMuxCustomErrors(t *testing.T) { + var ( + notFoundMessage = "Iris custom message for 404 not found" + internalServerMessage = "Iris custom message for 500 internal server error" + testRoutesCustomErrors = []testRoute{ + // NOT FOUND CUSTOM ERRORS - not registered + {"GET", "/test_get_nofound_custom", "/test_get_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"POST", "/test_post_nofound_custom", "/test_post_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"PUT", "/test_put_nofound_custom", "/test_put_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"DELETE", "/test_delete_nofound_custom", "/test_delete_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"HEAD", "/test_head_nofound_custom", "/test_head_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"OPTIONS", "/test_options_nofound_custom", "/test_options_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"CONNECT", "/test_connect_nofound_custom", "/test_connect_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"PATCH", "/test_patch_nofound_custom", "/test_patch_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + {"TRACE", "/test_trace_nofound_custom", "/test_trace_nofound_custom", "", notFoundMessage, 404, false, nil, nil}, + // SERVER INTERNAL ERROR 500 PANIC CUSTOM ERRORS - registered + {"GET", "/test_get_panic_custom", "/test_get_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"POST", "/test_post_panic_custom", "/test_post_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"PUT", "/test_put_panic_custom", "/test_put_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"DELETE", "/test_delete_panic_custom", "/test_delete_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"HEAD", "/test_head_panic_custom", "/test_head_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"OPTIONS", "/test_options_panic_custom", "/test_options_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"CONNECT", "/test_connect_panic_custom", "/test_connect_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"PATCH", "/test_patch_panic_custom", "/test_patch_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + {"TRACE", "/test_trace_panic_custom", "/test_trace_panic_custom", "", internalServerMessage, 500, true, nil, nil}, + } + ) + app := newApp() + // first register the testRoutes needed + for _, r := range testRoutesCustomErrors { + if r.Register { + app.HandleFunc(r.Method, r.Path, func(ctx *iris.Context) { + ctx.EmitError(r.Status) + }) + } + } + + // register the custom errors + app.OnError(iris.StatusNotFound, func(ctx *iris.Context) { + ctx.Writef("%s", notFoundMessage) + }) + + app.OnError(iris.StatusInternalServerError, func(ctx *iris.Context) { + ctx.Writef("%s", internalServerMessage) + }) + + // create httpexpect instance that will call fasthtpp.RequestHandler directly + e := httptest.New(app, t) + + // run the tests + for _, r := range testRoutesCustomErrors { + e.Request(r.Method, r.RequestPath). + Expect(). + Status(r.Status).Body().Equal(r.Body) + } +} + func TestRouteURLPath(t *testing.T) { app := iris.New() app.Adapt(gorillamux.New())