mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
366 lines
13 KiB
Go
366 lines
13 KiB
Go
|
package iris
|
||
|
|
||
|
// Contains tests for the mux(Router)
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/gavv/httpexpect"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
testEnableSubdomain = false
|
||
|
testSubdomain = "mysubdomain.com"
|
||
|
)
|
||
|
|
||
|
func testSubdomainHost() string {
|
||
|
return testSubdomain + strconv.Itoa(HTTPServer.Port())
|
||
|
}
|
||
|
|
||
|
func testSubdomainURL() (subdomainURL string) {
|
||
|
subdomainHost := testSubdomainHost()
|
||
|
if HTTPServer.IsSecure() {
|
||
|
subdomainURL = "https://" + subdomainHost
|
||
|
} else {
|
||
|
subdomainURL = "http://" + subdomainHost
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func subdomainTester(e *httpexpect.Expect) *httpexpect.Expect {
|
||
|
es := e.Builder(func(req *httpexpect.Request) {
|
||
|
req.WithURL(testSubdomainURL())
|
||
|
})
|
||
|
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 TestMuxSimple(t *testing.T) {
|
||
|
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"}}},
|
||
|
}
|
||
|
|
||
|
initDefault()
|
||
|
|
||
|
for idx := range testRoutes {
|
||
|
r := testRoutes[idx]
|
||
|
if r.Register {
|
||
|
HandleFunc(r.Method, r.Path, func(ctx *Context) {
|
||
|
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()))
|
||
|
}
|
||
|
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.SetBodyString(paramsKeyVal)
|
||
|
} else {
|
||
|
ctx.SetBodyString(r.Body)
|
||
|
}
|
||
|
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
e := Tester(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) {
|
||
|
|
||
|
initDefault()
|
||
|
|
||
|
h := func(c *Context) { c.WriteString(c.HostString() + c.PathString()) }
|
||
|
|
||
|
if testEnableSubdomain {
|
||
|
subdomainParty := 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 := 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)
|
||
|
}
|
||
|
|
||
|
e := Tester(t)
|
||
|
|
||
|
request := func(reqPath string) {
|
||
|
e.Request("GET", reqPath).
|
||
|
Expect().
|
||
|
Status(StatusOK).Body().Equal(HTTPServer.Host() + 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)
|
||
|
subdomainRequest := func(reqPath string) {
|
||
|
es.Request("GET", reqPath).
|
||
|
Expect().
|
||
|
Status(StatusOK).Body().Equal(testSubdomainHost() + reqPath)
|
||
|
}
|
||
|
|
||
|
subdomainRequest("/")
|
||
|
subdomainRequest("/path1")
|
||
|
subdomainRequest("/path2")
|
||
|
subdomainRequest("/namedpath/theparam1/something/theparam2")
|
||
|
subdomainRequest("/namedpath/theparam1/something/theparam2/else")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestMuxPathEscape(t *testing.T) {
|
||
|
initDefault()
|
||
|
|
||
|
Get("/details/:name", func(ctx *Context) {
|
||
|
name := ctx.Param("name")
|
||
|
highlight := ctx.URLParam("highlight")
|
||
|
ctx.Text(StatusOK, fmt.Sprintf("name=%s,highlight=%s", name, highlight))
|
||
|
})
|
||
|
|
||
|
e := Tester(t)
|
||
|
|
||
|
e.GET("/details/Sakamoto desu ga").
|
||
|
WithQuery("highlight", "text").
|
||
|
Expect().Status(StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text")
|
||
|
}
|
||
|
|
||
|
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},
|
||
|
}
|
||
|
)
|
||
|
initDefault()
|
||
|
// first register the testRoutes needed
|
||
|
for _, r := range testRoutesCustomErrors {
|
||
|
if r.Register {
|
||
|
HandleFunc(r.Method, r.Path, func(ctx *Context) {
|
||
|
ctx.EmitError(r.Status)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// register the custom errors
|
||
|
OnError(404, func(ctx *Context) {
|
||
|
ctx.Write("%s", notFoundMessage)
|
||
|
})
|
||
|
|
||
|
OnError(500, func(ctx *Context) {
|
||
|
ctx.Write("%s", internalServerMessage)
|
||
|
})
|
||
|
|
||
|
// create httpexpect instance that will call fasthtpp.RequestHandler directly
|
||
|
e := Tester(t)
|
||
|
|
||
|
// run the tests
|
||
|
for _, r := range testRoutesCustomErrors {
|
||
|
e.Request(r.Method, r.RequestPath).
|
||
|
Expect().
|
||
|
Status(r.Status).Body().Equal(r.Body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type testUserAPI struct {
|
||
|
*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) {
|
||
|
initDefault()
|
||
|
|
||
|
middlewareResponseText := "I assume that you are authenticated\n"
|
||
|
API("/users", testUserAPI{}, func(ctx *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 *Context) {
|
||
|
if ctx.Get("user") == "username" {
|
||
|
ctx.Write(middlewareResponseText)
|
||
|
ctx.Next()
|
||
|
} else {
|
||
|
ctx.SetStatusCode(StatusUnauthorized)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
e := Tester(t)
|
||
|
|
||
|
userID := "4077"
|
||
|
formname := "kataras"
|
||
|
|
||
|
e.GET("/users").Expect().Status(StatusOK).Body().Equal(middlewareResponseText + "Get Users\n")
|
||
|
e.GET("/users/" + userID).Expect().Status(StatusOK).Body().Equal(middlewareResponseText + "Get By " + userID + "\n")
|
||
|
e.PUT("/users").WithFormField("name", formname).Expect().Status(StatusOK).Body().Equal(middlewareResponseText + "Put, name: " + formname + "\n")
|
||
|
e.POST("/users/"+userID).WithFormField("name", formname).Expect().Status(StatusOK).Body().Equal(middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n")
|
||
|
e.DELETE("/users/" + userID).Expect().Status(StatusOK).Body().Equal(middlewareResponseText + "Delete By " + userID + "\n")
|
||
|
}
|
||
|
|
||
|
type myTestHandlerData struct {
|
||
|
Sysname string // this will be the same for all requests
|
||
|
Version int // this will be the same for all requests
|
||
|
DynamicPathParameter string // this will be different for each request
|
||
|
}
|
||
|
|
||
|
type myTestCustomHandler struct {
|
||
|
data myTestHandlerData
|
||
|
}
|
||
|
|
||
|
func (m *myTestCustomHandler) Serve(ctx *Context) {
|
||
|
data := &m.data
|
||
|
data.DynamicPathParameter = ctx.Param("myparam")
|
||
|
ctx.JSON(StatusOK, data)
|
||
|
}
|
||
|
|
||
|
func TestMuxCustomHandler(t *testing.T) {
|
||
|
initDefault()
|
||
|
myData := myTestHandlerData{
|
||
|
Sysname: "Redhat",
|
||
|
Version: 1,
|
||
|
}
|
||
|
Handle("GET", "/custom_handler_1/:myparam", &myTestCustomHandler{myData})
|
||
|
Handle("GET", "/custom_handler_2/:myparam", &myTestCustomHandler{myData})
|
||
|
|
||
|
e := Tester(t)
|
||
|
// two times per testRoute
|
||
|
param1 := "thisimyparam1"
|
||
|
expectedData1 := myData
|
||
|
expectedData1.DynamicPathParameter = param1
|
||
|
e.GET("/custom_handler_1/" + param1).Expect().Status(StatusOK).JSON().Equal(expectedData1)
|
||
|
|
||
|
param2 := "thisimyparam2"
|
||
|
expectedData2 := myData
|
||
|
expectedData2.DynamicPathParameter = param2
|
||
|
e.GET("/custom_handler_1/" + param2).Expect().Status(StatusOK).JSON().Equal(expectedData2)
|
||
|
|
||
|
param3 := "thisimyparam3"
|
||
|
expectedData3 := myData
|
||
|
expectedData3.DynamicPathParameter = param3
|
||
|
e.GET("/custom_handler_2/" + param3).Expect().Status(StatusOK).JSON().Equal(expectedData3)
|
||
|
|
||
|
param4 := "thisimyparam4"
|
||
|
expectedData4 := myData
|
||
|
expectedData4.DynamicPathParameter = param4
|
||
|
e.GET("/custom_handler_2/" + param4).Expect().Status(StatusOK).JSON().Equal(expectedData4)
|
||
|
}
|