mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
fix #2158 and more
This commit is contained in:
@@ -6,9 +6,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -722,7 +720,7 @@ func (api *APIBuilder) createRoutes(errorCode int, methods []string, relativePat
|
||||
}
|
||||
}
|
||||
|
||||
filename, line := getCaller()
|
||||
filename, line := hero.GetCaller()
|
||||
|
||||
fullpath := api.relativePath + relativePath // for now, keep the last "/" if any, "/xyz/"
|
||||
if len(handlers) == 0 {
|
||||
@@ -1706,50 +1704,3 @@ func (api *APIBuilder) Layout(tmplLayoutFile string) Party {
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
// https://golang.org/doc/go1.9#callersframes
|
||||
func getCaller() (string, int) {
|
||||
var pcs [32]uintptr
|
||||
n := runtime.Callers(1, pcs[:])
|
||||
frames := runtime.CallersFrames(pcs[:n])
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
var (
|
||||
frame runtime.Frame
|
||||
more = true
|
||||
)
|
||||
|
||||
for {
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
|
||||
frame, more = frames.Next()
|
||||
file := filepath.ToSlash(frame.File)
|
||||
// fmt.Printf("%s:%d | %s\n", file, frame.Line, frame.Function)
|
||||
|
||||
if strings.Contains(file, "go/src/runtime/") {
|
||||
continue
|
||||
}
|
||||
|
||||
if !strings.Contains(file, "_test.go") {
|
||||
if strings.Contains(file, "/kataras/iris") &&
|
||||
!strings.Contains(file, "kataras/iris/_examples") &&
|
||||
!strings.Contains(file, "kataras/iris/middleware") &&
|
||||
!strings.Contains(file, "iris-contrib/examples") {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if relFile, err := filepath.Rel(wd, file); err == nil {
|
||||
if !strings.HasPrefix(relFile, "..") {
|
||||
// Only if it's relative to this path, not parent.
|
||||
file = "./" + filepath.ToSlash(relFile)
|
||||
}
|
||||
}
|
||||
|
||||
return file, frame.Line
|
||||
}
|
||||
|
||||
return "???", 0
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func genPaths(routesLength, minCharLength, maxCharLength int) []string {
|
||||
//
|
||||
// GOCACHE=off && go test -run=XXX -bench=BenchmarkAPIBuilder$ -benchtime=10s
|
||||
func BenchmarkAPIBuilder(b *testing.B) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
rand.New(rand.NewSource(time.Now().Unix()))
|
||||
|
||||
noOpHandler := func(ctx *context.Context) {}
|
||||
handlersPerRoute := make(context.Handlers, 12)
|
||||
|
||||
@@ -14,7 +14,7 @@ var (
|
||||
|
||||
testExecutionResponse = func(t *testing.T, app *iris.Application, path string) {
|
||||
e := httptest.New(t, app)
|
||||
e.GET(path).Expect().Status(httptest.StatusOK).Body().Equal(finalExecutionRulesResponse)
|
||||
e.GET(path).Expect().Status(httptest.StatusOK).Body().IsEqual(finalExecutionRulesResponse)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -87,5 +87,5 @@ func TestRouterExecutionRulesShouldNotModifyTheCallersHandlerAndChildrenCanReset
|
||||
testExecutionResponse(t, app, "/")
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/c").Expect().Status(httptest.StatusOK).Body().Equal("4") // the "should not" should not be written.
|
||||
e.GET("/c").Expect().Status(httptest.StatusOK).Body().IsEqual("4") // the "should not" should not be written.
|
||||
}
|
||||
|
||||
@@ -57,9 +57,9 @@ func testRegisterRule(e *httptest.Expect, expectedGetBody string) {
|
||||
for _, method := range router.AllMethods {
|
||||
tt := e.Request(method, "/v1").Expect().Status(httptest.StatusOK).Body()
|
||||
if method == iris.MethodGet {
|
||||
tt.Equal(expectedGetBody)
|
||||
tt.IsEqual(expectedGetBody)
|
||||
} else {
|
||||
tt.Equal("[any] " + method)
|
||||
tt.IsEqual("[any] " + method)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,8 +111,8 @@ func TestRegisterRuleOverlap(t *testing.T) {
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/users").Expect().Status(httptest.StatusOK).Body().Equal("data")
|
||||
e.GET("/users/p1").Expect().Status(httptest.StatusUnauthorized).Body().Equal("Unauthorized")
|
||||
e.GET("/users/p2").Expect().Status(httptest.StatusUnauthorized).Body().Equal("no access")
|
||||
e.GET("/users/p3").Expect().Status(httptest.StatusOK).Body().Equal("p3 data")
|
||||
e.GET("/users").Expect().Status(httptest.StatusOK).Body().IsEqual("data")
|
||||
e.GET("/users/p1").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual("Unauthorized")
|
||||
e.GET("/users/p2").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual("no access")
|
||||
e.GET("/users/p3").Expect().Status(httptest.StatusOK).Body().IsEqual("p3 data")
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ var (
|
||||
t.Helper()
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET(path).Expect().Status(httptest.StatusOK).Body().Equal(finalResponse)
|
||||
e.GET(path).Expect().Status(httptest.StatusOK).Body().IsEqual(finalResponse)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -169,7 +169,7 @@ func TestUseRouterStopExecution(t *testing.T) {
|
||||
app.Get("/", writeHandler("index"))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal("stop")
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().IsEqual("stop")
|
||||
|
||||
app = iris.New()
|
||||
app.OnErrorCode(iris.StatusForbidden, func(ctx iris.Context) {
|
||||
@@ -183,7 +183,7 @@ func TestUseRouterStopExecution(t *testing.T) {
|
||||
app.Get("/", writeHandler("index"))
|
||||
|
||||
e = httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusForbidden).Body().Equal("err: custom error")
|
||||
e.GET("/").Expect().Status(iris.StatusForbidden).Body().IsEqual("err: custom error")
|
||||
}
|
||||
|
||||
func TestUseRouterParentDisallow(t *testing.T) {
|
||||
@@ -226,9 +226,9 @@ func TestUseRouterParentDisallow(t *testing.T) {
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/index").Expect().Status(iris.StatusOK).Body().Equal(expectedResponse)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedResponse)
|
||||
e.GET("/user").Expect().Status(iris.StatusOK).Body().Equal(expectedResponse)
|
||||
e.GET("/index").Expect().Status(iris.StatusOK).Body().IsEqual(expectedResponse)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().IsEqual(expectedResponse)
|
||||
e.GET("/user").Expect().Status(iris.StatusOK).Body().IsEqual(expectedResponse)
|
||||
}
|
||||
|
||||
func TestUseRouterSubdomains(t *testing.T) {
|
||||
@@ -270,21 +270,21 @@ func TestUseRouterSubdomains(t *testing.T) {
|
||||
})
|
||||
|
||||
e := httptest.New(t, app, httptest.URL("http://example.com"))
|
||||
e.GET("/notfound").Expect().Status(iris.StatusOK).Body().Equal("always_")
|
||||
e.GET("/notfound").Expect().Status(iris.StatusOK).Body().IsEqual("always_")
|
||||
|
||||
e.GET("/").WithURL("http://admin.example.com").Expect().Status(iris.StatusOK).Body().
|
||||
Equal("always_admin always_admin")
|
||||
IsEqual("always_admin always_admin")
|
||||
|
||||
e.GET("/").WithURL("http://control.admin.example.com").Expect().Status(iris.StatusOK).Body().
|
||||
Equal("always_admin always_control admin always_control admin")
|
||||
IsEqual("always_admin always_control admin always_control admin")
|
||||
|
||||
// It has a route, and use router just proceeds to the router.
|
||||
e.GET("/").WithURL("http://old.example.com").Expect().Status(iris.StatusOK).Body().
|
||||
Equal("chat")
|
||||
IsEqual("chat")
|
||||
// this is not a registered path, should fire 404, the UseRouter does not write
|
||||
// anything to the response writer, so the router has control over it.
|
||||
e.GET("/notfound").WithURL("http://old.example.com").Expect().Status(iris.StatusNotFound).Body().
|
||||
Equal("Not Found")
|
||||
IsEqual("Not Found")
|
||||
}
|
||||
|
||||
func TestUseWrapOrder(t *testing.T) {
|
||||
@@ -359,8 +359,8 @@ func TestUseWrapOrder(t *testing.T) {
|
||||
app.Get("/", handler)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/NotFound").Expect().Status(iris.StatusNotFound).Body().Equal(expectedNotFoundBody)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
|
||||
e.GET("/NotFound").Expect().Status(iris.StatusNotFound).Body().IsEqual(expectedNotFoundBody)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().IsEqual(expectedBody)
|
||||
}
|
||||
|
||||
func TestResumeExecution(t *testing.T) {
|
||||
@@ -414,5 +414,5 @@ func TestResumeExecution(t *testing.T) {
|
||||
app.Get("/", before, handler, after)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().IsEqual(expectedBody)
|
||||
}
|
||||
|
||||
@@ -69,9 +69,9 @@ func TestLowercaseRouting(t *testing.T) {
|
||||
e := httptest.New(t, app)
|
||||
for _, tt := range tests {
|
||||
s := strings.ToLower(tt)
|
||||
e.GET(tt).Expect().Status(httptest.StatusOK).Body().Equal(s)
|
||||
e.GET(s).Expect().Status(httptest.StatusOK).Body().Equal(s)
|
||||
e.GET(strings.ToUpper(tt)).Expect().Status(httptest.StatusOK).Body().Equal(s)
|
||||
e.GET(tt).Expect().Status(httptest.StatusOK).Body().IsEqual(s)
|
||||
e.GET(s).Expect().Status(httptest.StatusOK).Body().IsEqual(s)
|
||||
e.GET(strings.ToUpper(tt)).Expect().Status(httptest.StatusOK).Body().IsEqual(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func TestRouterWrapperOrder(t *testing.T) {
|
||||
app.Get("/", func(ctx iris.Context) {}) // to not append the not found one.
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedOrderStr)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().IsEqual(expectedOrderStr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,9 +190,9 @@ func TestNewSubdomainPartyRedirectHandler(t *testing.T) {
|
||||
}
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").WithURL("http://mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("root index")
|
||||
e.GET("/").WithURL("http://test.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("test index")
|
||||
e.GET("/").WithURL("http://testold.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("test index")
|
||||
e.GET("/").WithURL("http://testold.mydomain.com/notfound").Expect().Status(iris.StatusNotFound).Body().Equal("test 404")
|
||||
e.GET("/").WithURL("http://leveled.testold.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("leveled.testold this can be fired")
|
||||
e.GET("/").WithURL("http://mydomain.com").Expect().Status(iris.StatusOK).Body().IsEqual("root index")
|
||||
e.GET("/").WithURL("http://test.mydomain.com").Expect().Status(iris.StatusOK).Body().IsEqual("test index")
|
||||
e.GET("/").WithURL("http://testold.mydomain.com").Expect().Status(iris.StatusOK).Body().IsEqual("test index")
|
||||
e.GET("/").WithURL("http://testold.mydomain.com/notfound").Expect().Status(iris.StatusNotFound).Body().IsEqual("test 404")
|
||||
e.GET("/").WithURL("http://leveled.testold.mydomain.com").Expect().Status(iris.StatusOK).Body().IsEqual("leveled.testold this can be fired")
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ func testTheRoutes(t *testing.T, tests []testRoute, debug bool) {
|
||||
expectedBody = staticPathPrefixBody + req.path
|
||||
}
|
||||
|
||||
ex.Expect().Status(req.expectedStatusCode).Body().Equal(expectedBody)
|
||||
ex.Expect().Status(req.expectedStatusCode).Body().IsEqual(expectedBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,20 +45,20 @@ func TestOnAnyErrorCode(t *testing.T) {
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/found").Expect().Status(iris.StatusOK).
|
||||
Body().Equal(expectedFoundResponse)
|
||||
Body().IsEqual(expectedFoundResponse)
|
||||
|
||||
e.GET("/notfound").Expect().Status(iris.StatusNotFound).
|
||||
Body().Equal(http.StatusText(iris.StatusNotFound))
|
||||
Body().IsEqual(http.StatusText(iris.StatusNotFound))
|
||||
|
||||
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
|
||||
|
||||
e.POST("/found").Expect().Status(iris.StatusMethodNotAllowed).
|
||||
Body().Equal(http.StatusText(iris.StatusMethodNotAllowed))
|
||||
Body().IsEqual(http.StatusText(iris.StatusMethodNotAllowed))
|
||||
|
||||
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
|
||||
|
||||
e.GET("/407").Expect().Status(iris.StatusProxyAuthRequired).
|
||||
Body().Equal(expected407)
|
||||
Body().IsEqual(expected407)
|
||||
|
||||
// Test Configuration.ResetOnFireErrorCode.
|
||||
app2 := iris.New()
|
||||
@@ -77,7 +77,7 @@ func TestOnAnyErrorCode(t *testing.T) {
|
||||
})
|
||||
|
||||
httptest.New(t, app2).GET("/406").Expect().Status(iris.StatusNotAcceptable).
|
||||
Body().Equal(http.StatusText(iris.StatusNotAcceptable))
|
||||
Body().IsEqual(http.StatusText(iris.StatusNotAcceptable))
|
||||
|
||||
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
|
||||
}
|
||||
@@ -135,26 +135,26 @@ func TestPartyOnErrorCode(t *testing.T) {
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().Equal(globalNotFoundResponse)
|
||||
e.POST("/path").Expect().Status(iris.StatusMethodNotAllowed).Body().Equal(globalMethodNotAllowedResponse)
|
||||
e.GET("/path").Expect().Status(iris.StatusOK).Body().Equal("/path")
|
||||
e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().IsEqual(globalNotFoundResponse)
|
||||
e.POST("/path").Expect().Status(iris.StatusMethodNotAllowed).Body().IsEqual(globalMethodNotAllowedResponse)
|
||||
e.GET("/path").Expect().Status(iris.StatusOK).Body().IsEqual("/path")
|
||||
|
||||
e.POST("/users").Expect().Status(iris.StatusMethodNotAllowed).
|
||||
Body().Equal(usersResponse)
|
||||
Body().IsEqual(usersResponse)
|
||||
|
||||
e.POST("/users/42").Expect().Status(iris.StatusMethodNotAllowed).
|
||||
Body().Equal(usersuserResponse)
|
||||
Body().IsEqual(usersuserResponse)
|
||||
|
||||
e.GET("/users/42").Expect().Status(iris.StatusOK).
|
||||
Body().Equal("/users/42")
|
||||
e.GET("/users/ab").Expect().Status(iris.StatusNotFound).Body().Equal(usersuserNotFoundResponse)
|
||||
Body().IsEqual("/users/42")
|
||||
e.GET("/users/ab").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse)
|
||||
// inherit the parent.
|
||||
e.GET("/users/42/friends/dsa").Expect().Status(iris.StatusNotFound).Body().Equal(usersuserNotFoundResponse)
|
||||
e.GET("/users/42/friends/dsa").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse)
|
||||
|
||||
// if not registered to the party, then the root is taking action.
|
||||
e.GET("/users/42/ab/badrequest").Expect().Status(iris.StatusBadRequest).Body().Equal(http.StatusText(iris.StatusBadRequest))
|
||||
e.GET("/users/42/ab/badrequest").Expect().Status(iris.StatusBadRequest).Body().IsEqual(http.StatusText(iris.StatusBadRequest))
|
||||
|
||||
// if not registered to the party, and not in root, then just write the status text (fallback behavior)
|
||||
e.GET("/users/badrequest").Expect().Status(iris.StatusBadRequest).
|
||||
Body().Equal(http.StatusText(iris.StatusBadRequest))
|
||||
Body().IsEqual(http.StatusText(iris.StatusBadRequest))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user