-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from ant0ine/ifMiddleware
implementation of the IfMiddleware
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} |