From fe79fdea4436724acf7bdd40b5f1f6ca5e8ce327 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 22 Dec 2016 19:07:03 +0200 Subject: [PATCH] Fix https://github.com/kataras/iris/issues/566 --- context.go | 11 +++++++++-- context_test.go | 25 +++++++++++++++++++++---- http.go | 2 +- iris.go | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/context.go b/context.go index 90f7d59b..78a8e849 100644 --- a/context.go +++ b/context.go @@ -97,7 +97,7 @@ type ( Middleware Middleware // exported because is useful for debugging session sessions.Session // Pos is the position number of the Context, look .Next to understand - Pos uint8 // exported because is useful for debugging + Pos int // exported because is useful for debugging } ) @@ -116,7 +116,7 @@ func (ctx *Context) Do() { func (ctx *Context) Next() { //set position to the next ctx.Pos++ - midLen := uint8(len(ctx.Middleware)) + midLen := len(ctx.Middleware) //run the next if ctx.Pos < midLen { ctx.Middleware[ctx.Pos].Serve(ctx) @@ -1365,6 +1365,13 @@ func (ctx *Context) BeginTransaction(pipe func(scope *TransactionScope)) { // and replaces the context's response with an error. // if the transaction completed successfully then we need to pass the temp's context's response to this context. // so we must copy back its context at all cases, no matter the result of the transaction. + + // TODO: GREEK, THESE COMMENTS WILL BE REMOVED AFTER I FIX A RECENT BUG REPORTED BY COMMUNITY + // auto to xreiazomaste gia na pianei ta errors kiolas, ara prepei na vrw allous tropous + // na manteuw an 9elei o xristis na dixnei errors sto transaction h apla na to kanei skip + // alla tautoxrona ena failed transaction sto telos na mh kanei reset ta proigoumena, gt twra auto kanei, + // kai auto xreiazete omws se arketes periptwseis alla prepei kiolas na diaxwrizw to ka9e transaction na einai aneksartita + // pisteuw to Response.Reset dn 9a mas xreiastei ka9olou, prepei na ktlvenw mono pote xreiazete na kanw copy to scope's context kai pote oxi. *ctx = *scope.Context // if the scope had lifetime of the whole request and it completed with an error(failure) diff --git a/context_test.go b/context_test.go index 4d642901..57c4eab7 100644 --- a/context_test.go +++ b/context_test.go @@ -5,15 +5,16 @@ import ( "encoding/json" "encoding/xml" "fmt" - "github.com/gavv/httpexpect" - "github.com/kataras/iris" - "github.com/kataras/iris/httptest" - "github.com/valyala/fasthttp" "net/url" "strconv" "strings" "testing" "time" + + "github.com/gavv/httpexpect" + "github.com/kataras/iris" + "github.com/kataras/iris/httptest" + "github.com/valyala/fasthttp" ) // White-box testing * @@ -849,6 +850,14 @@ func TestTransactions(t *testing.T) { ctx.BeginTransaction(successTransaction) }) + /*TODO: MAKE THIS TO WORK + iris.Get("/failFirsAndThirdTransactionsButSuccessSecond", func(ctx *iris.Context) { + ctx.BeginTransaction(maybeFailureTransaction(true, false)) + ctx.BeginTransaction(successTransaction) + ctx.BeginTransaction(maybeFailureTransaction(true, false)) + }) + */ + iris.Get("/failAllBecauseOfRequestScopeAndFailure", func(ctx *iris.Context) { ctx.BeginTransaction(maybeFailureTransaction(true, true)) ctx.BeginTransaction(successTransaction) @@ -869,6 +878,14 @@ func TestTransactions(t *testing.T) { ContentType("text/html", iris.Config.Charset). Body(). Equal(firstTransactionFailureMessage + secondTransactionSuccessHTMLMessage) + /* + e.GET("/failFirsAndThirdTransactionsButSuccessSecond"). + Expect(). + Status(iris.StatusOK). + ContentType("text/html", iris.Config.Charset). + Body(). + Equal(firstTransactionFailureMessage + secondTransactionSuccessHTMLMessage) + */ e.GET("/failAllBecauseOfRequestScopeAndFailure"). Expect(). diff --git a/http.go b/http.go index a2d68e22..fd9cf4a5 100644 --- a/http.go +++ b/http.go @@ -948,7 +948,7 @@ func (mux *serveMux) fireError(statusCode int, ctx *Context) { errHandler := mux.errorHandlers[statusCode] if errHandler == nil { errHandler = HandlerFunc(func(ctx *Context) { - ctx.ResetBody() + ctx.Response.Reset() ctx.SetStatusCode(statusCode) ctx.SetBodyString(statusText[statusCode]) }) diff --git a/iris.go b/iris.go index 21ebbc01..d24f9d20 100644 --- a/iris.go +++ b/iris.go @@ -1772,7 +1772,7 @@ func (api *muxAPI) StaticHandler(systemPath string, stripSlashes int, compress b if errCode == StatusNotFound || errCode == StatusBadRequest || errCode == StatusInternalServerError { api.mux.fireError(errCode, ctx) } - if ctx.Pos < uint8(len(ctx.Middleware))-1 { + if ctx.Pos < len(ctx.Middleware)-1 { ctx.Next() // for any case }