Add an example for httprouter_test, gofmt everything and misspel fixes.

Former-commit-id: e69ffdf9bba4c7e8c3df45c015b3363cc3fc59b6
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-02-17 10:45:47 +02:00
parent 2eae027e77
commit 9a3c4f4c0a
13 changed files with 307 additions and 19 deletions

View File

@ -138,7 +138,7 @@ func registerRoute(route iris.RouteInfo, gorillaRouter *mux.Router, context iris
if subdomain != "" {
if subdomain == "*." {
// it's an iris wildcard subdomain
// so register it as wildcard on gorilla mux too (hopefuly, it supports these things)
// so register it as wildcard on gorilla mux too
subdomain = "{subdomain}."
} else {
// it's a static subdomain (which contains the dot)

View File

@ -0,0 +1,286 @@
package httprouter_test
import (
"math/rand"
"strconv"
"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"
)
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(httprouter.New())
return app
}
func TestMuxSimple(t *testing.T) {
app := newApp()
testRoutes := []testRoute{
// FOUND - registed
{"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 registed
{"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 registed
{"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 - registed
{"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)
}
}

View File

@ -55,7 +55,7 @@ func RemoveCookie(name string, res http.ResponseWriter, req *http.Request) {
// valid means that is recognised as 'domain' by the browser, so it(the cookie) can be shared with subdomains also
func IsValidCookieDomain(domain string) bool {
if domain == "0.0.0.0" || domain == "127.0.0.1" {
// for these type of hosts, we can't allow subdomains persistance,
// for these type of hosts, we can't allow subdomains persistence,
// the web browser doesn't understand the mysubdomain.0.0.0.0 and mysubdomain.127.0.0.1 mysubdomain.32.196.56.181. as scorrectly ubdomains because of the many dots
// so don't set a cookie domain here, let browser handle this
return false

View File

@ -32,15 +32,17 @@ func (d *Database) Load(sid string) map[string]interface{} {
if err != nil {
if err != nil {
// don't use to get the logger, just prin these to the console... atm
println("Redis Connection error on Connect: " + err.Error())
println("But don't panic, auto-switching to memory store right now!")
///TODO: Find a way to use the iris' defined logger via an optional interface to Database.
// println("Redis Connection error on Connect: " + err.Error())
// println("But don't panic, auto-switching to memory store right now!")
}
}
}
//fetch the values from this session id and copy-> store them
val, err := d.redis.GetBytes(sid)
if err == nil {
err = DeserializeBytes(val, &values)
// err removed because of previous TODO
DeserializeBytes(val, &values)
}
return values

View File

@ -125,7 +125,7 @@ func (s *sessions) Start(res http.ResponseWriter, req *http.Request) iris.Sessio
}
}
// finally set the .localhost.com (for(1-level) || .mysubdomain.localhost.com (for 2-level subdomain allow)
cookie.Domain = "." + requestDomain // . to allow persistance
cookie.Domain = "." + requestDomain // . to allow persistence
}
}

View File

@ -74,7 +74,7 @@ func (h *Adaptor) Adapt(frame *iris.Policies) {
// no need to panic here because we will use the html as the default template engine
// even if the user doesn't asks for
// or no? we had the defaults before... maybe better to give the user
// the oportunity to learn about the template's configuration
// the opportunity to learn about the template's configuration
// (now 6.1.4 ) they are defaulted and users may don't know how and if they can change the configuration
// even if the book and examples covers these things, many times they're asking me on chat.............
// so no defaults? ok no defaults. This file then will be saved to /adaptors as with other template engines.
@ -94,7 +94,7 @@ func (h *Adaptor) Adapt(frame *iris.Policies) {
// adaptor. We also know that it is not a good idea to have two
// policies with the same function or we can? wait. No we can't.
// We can't because:
// - the RenderPolicy should accept any type of render process, not only tempaltes.
// - the RenderPolicy should accept any type of render process, not only templates.
// - I didn't design iris/policy.go to keep logic about implementation, this would make that very limited
// and I don't want to break that just for the templates.
// - We want community to be able to create packages which can adapt template functions but
@ -105,13 +105,13 @@ func (h *Adaptor) Adapt(frame *iris.Policies) {
//
// So when the community wants to create a template adaptor has two options:
// - Use the RenderPolicy which is just a func
// - Use the kataras/iris/adaptors/view.Adaptor adaptor wrapper for RenderPolicy with a compination of kataras/go-template/.Engine
// - Use the kataras/iris/adaptors/view.Adaptor adaptor wrapper for RenderPolicy with a combination of kataras/go-template/.Engine
//
//
// So here is the only place we adapt the iris.TemplateFuncsPolicy to the tempaltes, if and only if templates are used,
// So here is the only place we adapt the iris.TemplateFuncsPolicy to the templates, if and only if templates are used,
// otherwise they are just ignored without any creepy things.
//
// The TemplateFuncsPolicy will work in compination with the specific template adaptor's functions(see html.go and the rest)
// The TemplateFuncsPolicy will work in combination with the specific template adaptor's functions(see html.go and the rest)
if len(frame.TemplateFuncsPolicy) > 0 {
mux.SetFuncMapToEngine(frame.TemplateFuncsPolicy, h.engine)

View File

@ -32,7 +32,7 @@ func Handlebars(directory string, extension string) *HandlebarsAdaptor {
// and optionally {{partial/partial_r/render}} to render other template files like headers and footers
//
// The 'tmplLayoutFile' is a relative path of the templates base directory,
// for the template file whith its extension.
// for the template file with its extension.
//
// Example: Handlebars("./templates", ".html").Layout("layouts/mainLayout.html")
// // mainLayout.html is inside: "./templates/layouts/".

View File

@ -46,7 +46,7 @@ func (h *HTMLAdaptor) Delims(left string, right string) *HTMLAdaptor {
// and optionally {{partial/partial_r/render}} to render other template files like headers and footers
//
// The 'tmplLayoutFile' is a relative path of the templates base directory,
// for the template file whith its extension.
// for the template file with its extension.
//
// Example: HTML("./templates", ".html").Layout("layouts/mainLayout.html")
// // mainLayout.html is inside: "./templates/layouts/".

View File

@ -169,7 +169,7 @@ type Configuration struct {
DisableBodyConsumptionOnUnmarshal bool `yaml:"DisableBodyConsumptionOnUnmarshal"`
// TimeFormat time format for any kind of datetime parsing
// Defauls to "Mon, 02 Jan 2006 15:04:05 GMT".
// Defaults to "Mon, 02 Jan 2006 15:04:05 GMT".
TimeFormat string `yaml:"TimeFormat"`
// Charset character encoding for various rendering
@ -324,7 +324,7 @@ var (
}
// OptionTimeFormat time format for any kind of datetime parsing.
// Defauls to "Mon, 02 Jan 2006 15:04:05 GMT".
// Defaults to "Mon, 02 Jan 2006 15:04:05 GMT".
OptionTimeFormat = func(val string) OptionSet {
return func(c *Configuration) {
c.TimeFormat = val

2
fs.go
View File

@ -44,7 +44,7 @@ func toWebPath(systemPath string) string {
}
// abs calls filepath.Abs but ignores the error and
// returns the original value if any error occured.
// returns the original value if any error occurred.
func abs(path string) string {
absPath, err := filepath.Abs(path)
if err != nil {

View File

@ -244,7 +244,7 @@ func New(setters ...OptionSetter) *Framework {
//
// notes for me: Why not at the build state? in order to be overridable and not only them,
// these are easy to be overriden by external adaptors too, no matter the order,
// these are easy to be overridden by external adaptors too, no matter the order,
// this is why the RenderPolicy last registration executing first and the first last.
//

View File

@ -492,7 +492,7 @@ type (
// a compatible iris.Session interface, type. If the external session manager
// doesn't qualifies, then the user should code the rest of the functions with empty implementation.
//
// A SessionsPolicy should be responsible to Destory a session based
// A SessionsPolicy should be responsible to Destroy a session based
// on the http.ResponseWriter and http.Request, this function should works individually.
//
// No iris.Context required from users. In order to be able to adapt any external session manager.

View File

@ -10,7 +10,7 @@ import (
// This will be removed at the final release
// it's here just for my tests
// it will may transfered to the (advanced) examples repository
// it will may transferred to the (advanced) examples repository
// in order to show the users how they can adapt any third-party router.
// They can also view the ./adaptors/httprouter and ./adaptors/gorillamux.
func newTestNativeRouter() Policies {