mirror of
https://github.com/documize/community.git
synced 2025-07-22 22:59:43 +02:00
initial commit
This commit is contained in:
commit
18933c6767
1841 changed files with 810642 additions and 0 deletions
75
vendor/github.com/codegangsta/negroni/negroni_test.go
generated
vendored
Normal file
75
vendor/github.com/codegangsta/negroni/negroni_test.go
generated
vendored
Normal file
|
@ -0,0 +1,75 @@
|
|||
package negroni
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Test Helpers */
|
||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if a != b {
|
||||
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
||||
|
||||
func refute(t *testing.T, a interface{}, b interface{}) {
|
||||
if a == b {
|
||||
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNegroniRun(t *testing.T) {
|
||||
// just test that Run doesn't bomb
|
||||
go New().Run(":3000")
|
||||
}
|
||||
|
||||
func TestNegroniServeHTTP(t *testing.T) {
|
||||
result := ""
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
n := New()
|
||||
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
result += "foo"
|
||||
next(rw, r)
|
||||
result += "ban"
|
||||
}))
|
||||
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
result += "bar"
|
||||
next(rw, r)
|
||||
result += "baz"
|
||||
}))
|
||||
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
result += "bat"
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
}))
|
||||
|
||||
n.ServeHTTP(response, (*http.Request)(nil))
|
||||
|
||||
expect(t, result, "foobarbatbazban")
|
||||
expect(t, response.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// Ensures that a Negroni middleware chain
|
||||
// can correctly return all of its handlers.
|
||||
func TestHandlers(t *testing.T) {
|
||||
response := httptest.NewRecorder()
|
||||
n := New()
|
||||
handlers := n.Handlers()
|
||||
expect(t, 0, len(handlers))
|
||||
|
||||
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
// Expects the length of handlers to be exactly 1
|
||||
// after adding exactly one handler to the middleware chain
|
||||
handlers = n.Handlers()
|
||||
expect(t, 1, len(handlers))
|
||||
|
||||
// Ensures that the first handler that is in sequence behaves
|
||||
// exactly the same as the one that was registered earlier
|
||||
handlers[0].ServeHTTP(response, (*http.Request)(nil), nil)
|
||||
expect(t, response.Code, http.StatusOK)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue