-
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 #123 from ant0ine/v3-alpha
Implementation branch for the v3
- Loading branch information
Showing
31 changed files
with
977 additions
and
963 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
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
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
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
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,83 @@ | ||
package rest | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Api defines a stack of Middlewares and an App. | ||
type Api struct { | ||
stack []Middleware | ||
app App | ||
} | ||
|
||
// NewApi makes a new Api object. The Middleware stack is empty, and the App is nil. | ||
func NewApi() *Api { | ||
return &Api{ | ||
stack: []Middleware{}, | ||
app: nil, | ||
} | ||
} | ||
|
||
// Use pushes one or multiple middlewares to the stack for middlewares | ||
// maintained in the Api object. | ||
func (api *Api) Use(middlewares ...Middleware) { | ||
api.stack = append(api.stack, middlewares...) | ||
} | ||
|
||
// SetApp sets the App in the Api object. | ||
func (api *Api) SetApp(app App) { | ||
api.app = app | ||
} | ||
|
||
// MakeHandler wraps all the Middlewares of the stack and the App together, and returns an | ||
// http.Handler ready to be used. If the Middleware stack is empty the App is used directly. If the | ||
// App is nil, a HandlerFunc that does nothing is used instead. | ||
func (api *Api) MakeHandler() http.Handler { | ||
var appFunc HandlerFunc | ||
if api.app != nil { | ||
appFunc = api.app.AppFunc() | ||
} else { | ||
appFunc = func(w ResponseWriter, r *Request) {} | ||
} | ||
return http.HandlerFunc( | ||
adapterFunc( | ||
WrapMiddlewares(api.stack, appFunc), | ||
), | ||
) | ||
} | ||
|
||
// Defines a stack of middlewares convenient for development. Among other things: | ||
// console friendly logging, JSON indentation, error stack strace in the response. | ||
var DefaultDevStack = []Middleware{ | ||
&AccessLogApacheMiddleware{}, | ||
&TimerMiddleware{}, | ||
&RecorderMiddleware{}, | ||
&PoweredByMiddleware{}, | ||
&RecoverMiddleware{ | ||
EnableResponseStackTrace: true, | ||
}, | ||
&JsonIndentMiddleware{}, | ||
&ContentTypeCheckerMiddleware{}, | ||
} | ||
|
||
// Defines a stack of middlewares convenient for production. Among other things: | ||
// Apache CombinedLogFormat logging, gzip compression. | ||
var DefaultProdStack = []Middleware{ | ||
&AccessLogApacheMiddleware{ | ||
Format: CombinedLogFormat, | ||
}, | ||
&TimerMiddleware{}, | ||
&RecorderMiddleware{}, | ||
&PoweredByMiddleware{}, | ||
&RecoverMiddleware{}, | ||
&GzipMiddleware{}, | ||
&ContentTypeCheckerMiddleware{}, | ||
} | ||
|
||
// Defines a stack of middlewares that should be common to most of the middleware stacks. | ||
var DefaultCommonStack = []Middleware{ | ||
&TimerMiddleware{}, | ||
&RecorderMiddleware{}, | ||
&PoweredByMiddleware{}, | ||
&RecoverMiddleware{}, | ||
} |
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,97 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/ant0ine/go-json-rest/rest/test" | ||
"testing" | ||
) | ||
|
||
func TestApiNoAppNoMiddleware(t *testing.T) { | ||
|
||
api := NewApi() | ||
if api == nil { | ||
t.Fatal("Api object must be instantiated") | ||
} | ||
|
||
handler := api.MakeHandler() | ||
if handler == nil { | ||
t.Fatal("the http.Handler must be have been create") | ||
} | ||
|
||
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil)) | ||
recorded.CodeIs(200) | ||
} | ||
|
||
func TestApiSimpleAppNoMiddleware(t *testing.T) { | ||
|
||
api := NewApi() | ||
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) { | ||
w.WriteJson(map[string]string{"Id": "123"}) | ||
})) | ||
|
||
handler := api.MakeHandler() | ||
if handler == nil { | ||
t.Fatal("the http.Handler must be have been create") | ||
} | ||
|
||
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil)) | ||
recorded.CodeIs(200) | ||
recorded.ContentTypeIsJson() | ||
recorded.BodyIs(`{"Id":"123"}`) | ||
} | ||
|
||
func TestDevStack(t *testing.T) { | ||
|
||
api := NewApi() | ||
api.Use(DefaultDevStack...) | ||
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) { | ||
w.WriteJson(map[string]string{"Id": "123"}) | ||
})) | ||
|
||
handler := api.MakeHandler() | ||
if handler == nil { | ||
t.Fatal("the http.Handler must be have been create") | ||
} | ||
|
||
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil)) | ||
recorded.CodeIs(200) | ||
recorded.ContentTypeIsJson() | ||
recorded.BodyIs("{\n \"Id\": \"123\"\n}") | ||
} | ||
|
||
func TestProdStack(t *testing.T) { | ||
|
||
api := NewApi() | ||
api.Use(DefaultProdStack...) | ||
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) { | ||
w.WriteJson(map[string]string{"Id": "123"}) | ||
})) | ||
|
||
handler := api.MakeHandler() | ||
if handler == nil { | ||
t.Fatal("the http.Handler must be have been create") | ||
} | ||
|
||
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil)) | ||
recorded.CodeIs(200) | ||
recorded.ContentTypeIsJson() | ||
recorded.ContentEncodingIsGzip() | ||
} | ||
|
||
func TestCommonStack(t *testing.T) { | ||
|
||
api := NewApi() | ||
api.Use(DefaultCommonStack...) | ||
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) { | ||
w.WriteJson(map[string]string{"Id": "123"}) | ||
})) | ||
|
||
handler := api.MakeHandler() | ||
if handler == nil { | ||
t.Fatal("the http.Handler must be have been create") | ||
} | ||
|
||
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/", nil)) | ||
recorded.CodeIs(200) | ||
recorded.ContentTypeIsJson() | ||
recorded.BodyIs(`{"Id":"123"}`) | ||
} |
Oops, something went wrong.