Skip to content

Commit

Permalink
Merge pull request #136 from ant0ine/ifMiddleware
Browse files Browse the repository at this point in the history
implementation of the IfMiddleware
  • Loading branch information
ant0ine committed Mar 28, 2015
2 parents 36d1f85 + 8408240 commit 6f94f6f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
53 changes: 53 additions & 0 deletions rest/if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package rest

import (
"log"
)

// IfMiddleware evaluates at runtime a condition based on the current request, and decides to
// execute one of the other Middleware based on this boolean.
type IfMiddleware struct {

// Runtime condition that decides of the execution of IfTrue of IfFalse.
Condition func(r *Request) bool

// Middleware to run when the condition is true. Note that the middleware is initialized
// weather if will be used or not. (Optional, pass-through if not set)
IfTrue Middleware

// Middleware to run when the condition is false. Note that the middleware is initialized
// weather if will be used or not. (Optional, pass-through if not set)
IfFalse Middleware
}

// MiddlewareFunc makes TimerMiddleware implement the Middleware interface.
func (mw *IfMiddleware) MiddlewareFunc(h HandlerFunc) HandlerFunc {

if mw.Condition == nil {
log.Fatal("IfMiddleware Condition is required")
}

var ifTrueHandler HandlerFunc
if mw.IfTrue != nil {
ifTrueHandler = mw.IfTrue.MiddlewareFunc(h)
} else {
ifTrueHandler = h
}

var ifFalseHandler HandlerFunc
if mw.IfFalse != nil {
ifFalseHandler = mw.IfFalse.MiddlewareFunc(h)
} else {
ifFalseHandler = h
}

return func(w ResponseWriter, r *Request) {

if mw.Condition(r) {
ifTrueHandler(w, r)
} else {
ifFalseHandler(w, r)
}

}
}
51 changes: 51 additions & 0 deletions rest/if_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rest

import (
"github.com/ant0ine/go-json-rest/rest/test"
"testing"
)

func TestIfMiddleware(t *testing.T) {

api := NewApi()

// the middleware to test
api.Use(&IfMiddleware{
Condition: func(r *Request) bool {
if r.URL.Path == "/true" {
return true
}
return false
},
IfTrue: MiddlewareSimple(func(handler HandlerFunc) HandlerFunc {
return func(w ResponseWriter, r *Request) {
r.Env["TRUE_MIDDLEWARE"] = true
handler(w, r)
}
}),
IfFalse: MiddlewareSimple(func(handler HandlerFunc) HandlerFunc {
return func(w ResponseWriter, r *Request) {
r.Env["FALSE_MIDDLEWARE"] = true
handler(w, r)
}
}),
})

// a simple app
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(r.Env)
}))

// wrap all
handler := api.MakeHandler()

recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.BodyIs("{\"FALSE_MIDDLEWARE\":true}")

recorded = test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/true", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.BodyIs("{\"TRUE_MIDDLEWARE\":true}")
}

0 comments on commit 6f94f6f

Please sign in to comment.