1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-22 03:15:58 +00:00

Update to 8.0.4 | New: transfer a message to the request logger

Former-commit-id: 2bab3c9f28f7e9edd5d85e579349f70388af871d
This commit is contained in:
kataras
2017-07-17 17:42:51 +03:00
parent 56aa3de645
commit 093d087a68
13 changed files with 394 additions and 157 deletions

View File

@@ -101,7 +101,7 @@ func StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error
// StaticHandler returns a new Handler which is ready
// to serve all kind of static files.
//
// Developers can wrap this handler using the `iris.StripPrefix`
// Developers can wrap this handler using the `router.StripPrefix`
// for a fixed static path when the result handler is being, finally, registered to a route.
//
//
@@ -109,7 +109,7 @@ func StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error
// app := iris.New()
// ...
// fileserver := iris.StaticHandler("./static_files", false, false)
// h := iris.StripPrefix("/static", fileserver)
// h := router.StripPrefix("/static", fileserver)
// /* http://mydomain.com/static/css/style.css */
// app.Get("/static", h)
// ...
@@ -288,7 +288,7 @@ func (w *fsHandler) Build() context.Handler {
//
// Usage:
// fileserver := iris.StaticHandler("./static_files", false, false)
// h := iris.StripPrefix("/static", fileserver)
// h := router.StripPrefix("/static", fileserver)
// app.Get("/static", h)
//
func StripPrefix(prefix string, h context.Handler) context.Handler {

View File

@@ -218,11 +218,15 @@ func (h *routerHandler) HandleRequest(ctx context.Context) {
continue
}
}
// RCF rfc2616 https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
// The response MUST include an Allow header containing a list of valid methods for the requested resource.
ctx.Header("Allow", methodAllowed)
ctx.StatusCode(http.StatusMethodNotAllowed)
return
if ctx.Method() != methodAllowed {
// RCF rfc2616 https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
// The response MUST include an Allow header containing a list of valid methods for the requested resource.
ctx.Header("Allow", methodAllowed)
ctx.StatusCode(http.StatusMethodNotAllowed)
return
}
}
ctx.StatusCode(http.StatusNotFound)
}

View File

@@ -21,8 +21,12 @@ type ErrorCodeHandler struct {
func (ch *ErrorCodeHandler) Fire(ctx context.Context) {
// if we can reset the body
if w, ok := ctx.IsRecording(); ok {
// reset if previous content and it's recorder
w.Reset()
if w.StatusCode() < 400 { // if not an error status code
w.WriteHeader(ch.StatusCode) // then set it manually here, otherwise it should be setted via ctx.StatusCode(...)
}
// reset if previous content and it's recorder, keep the status code.
w.ClearHeaders()
w.ResetBody()
} else if w, ok := ctx.ResponseWriter().(*context.GzipResponseWriter); ok {
// reset and disable the gzip in order to be an expected form of http error result
w.ResetBody()
@@ -41,6 +45,18 @@ func (ch *ErrorCodeHandler) Fire(ctx context.Context) {
// i.e
// users := app.Party("/users")
// users.Done(func(ctx context.Context){ if ctx.StatusCode() == 400 { /* custom error code for /users */ }})
// use .HandlerIndex
// that sets the current handler index to zero
// in order to:
// ignore previous runs that may changed the handler index,
// via ctx.Next or ctx.StopExecution, if any.
//
// use .Do
// that overrides the existing handlers and sets and runs these error handlers.
// in order to:
// ignore the route's after-handlers, if any.
ctx.HandlerIndex(0)
ctx.Do(ch.Handlers)
}
@@ -76,10 +92,7 @@ func defaultErrorCodeHandlers() *ErrorCodeHandlers {
func statusText(statusCode int) context.Handler {
return func(ctx context.Context) {
if _, err := ctx.WriteString(http.StatusText(statusCode)); err != nil {
// ctx.Application().Logger().Infof("(status code: %d) %s",
// err.Error(), statusCode)
}
ctx.WriteString(http.StatusText(statusCode))
}
}
@@ -113,10 +126,12 @@ func (s *ErrorCodeHandlers) Register(statusCode int, handlers ...context.Handler
StatusCode: statusCode,
Handlers: handlers,
}
s.handlers = append(s.handlers, ch)
return ch
}
// otherwise update the handlers
h.updateHandlers(handlers)
return h
@@ -136,6 +151,5 @@ func (s *ErrorCodeHandlers) Fire(ctx context.Context) {
if ch == nil {
ch = s.Register(statusCode, statusText(statusCode))
}
ch.Fire(ctx)
}

View File

@@ -0,0 +1,73 @@
// black-box testing
package router_test
import (
"bytes"
"net/http"
"testing"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/httptest"
)
var defaultErrHandler = func(ctx context.Context) {
text := http.StatusText(ctx.GetStatusCode())
ctx.WriteString(text)
}
func TestOnAnyErrorCode(t *testing.T) {
app := iris.New()
app.Configure(iris.WithFireMethodNotAllowed)
buff := &bytes.Buffer{}
expectedPrintBeforeExecuteErr := "printed before error"
// with a middleware
app.OnAnyErrorCode(func(ctx context.Context) {
buff.WriteString(expectedPrintBeforeExecuteErr)
ctx.Next()
}, defaultErrHandler)
expectedFoundResponse := "found"
app.Get("/found", func(ctx context.Context) {
ctx.WriteString(expectedFoundResponse)
})
app.Get("/406", func(ctx context.Context) {
ctx.Record()
ctx.WriteString("this should not be sent, only status text will be sent")
ctx.WriteString("the handler can handle 'rollback' of the text when error code fired because of the recorder")
ctx.StatusCode(iris.StatusNotAcceptable)
})
e := httptest.New(t, app)
e.GET("/found").Expect().Status(iris.StatusOK).
Body().Equal(expectedFoundResponse)
e.GET("/notfound").Expect().Status(iris.StatusNotFound).
Body().Equal(http.StatusText(iris.StatusNotFound))
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
e.POST("/found").Expect().Status(iris.StatusMethodNotAllowed).
Body().Equal(http.StatusText(iris.StatusMethodNotAllowed))
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
e.GET("/406").Expect().Status(iris.StatusNotAcceptable).
Body().Equal(http.StatusText(iris.StatusNotAcceptable))
checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
}
func checkAndClearBuf(t *testing.T, buff *bytes.Buffer, expected string) {
if got, expected := buff.String(), expected; got != expected {
t.Fatalf("expected middleware to run before the error handler, expected %s but got %s", expected, got)
}
buff.Reset()
}