Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Merge branch '1.0.6-development'
Browse files Browse the repository at this point in the history
* 1.0.6-development:
  ✅ Add middleware tests
  🐛 Fix middleware bug, close #6
  📝 Update CONTRIBUTING.md
  Update badges in README.md
  • Loading branch information
bahlo committed Feb 18, 2016
2 parents e385dfd + 79f9d04 commit 3613b1f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
35 changes: 29 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# Contributing

So you want to contribute to Goat? Great!
So you want to contribute to Goat? Great! :tada:

Here's a small guide to make this easy for everyone:
Here are a few guidelines when it comes to Pull Requests.

1. Fork this repository
2. Create a branch named after your addition (e.g. `my-awesome-feature`)
3. Write many tests (the coverage shouldn't fall)
4. Create pull request
## Go
* Use [gofmt](https://golang.org/cmd/gofmt) to format your code
* Write tests (the coverage should not fall)

## Git Commit Messages
(Original: [Atom](https://github.com/atom/atom/blob/97d697f1952eaa4bb0d5d71f4895877183ad53ba/CONTRIBUTING.md#git-commit-messages))

* Use the present tense ("Add feature" not "Added feature")
* Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
* Limit the first line to 72 characters or less
* Reference issues and pull requests liberally
* Consider starting the commit message with an applicable emoji:
* :lipstick: `:lipstick:` when improving the format/structure of the code
* :racehorse: `:racehorse:` when improving performance
* :non-potable_water: `:non-potable_water:` when plugging memory leaks
* :memo: `:memo:` when writing docs
* :penguin: `:penguin:` when fixing something on Linux
* :apple: `:apple:` when fixing something on Mac OS
* :checkered_flag: `:checkered_flag:` when fixing something on Windows
* :bug: `:bug:` when fixing a bug
* :fire: `:fire:` when removing code or files
* :green_heart: `:green_heart:` when fixing the CI build
* :white_check_mark: `:white_check_mark:` when adding tests
* :lock: `:lock:` when dealing with security
* :arrow_up: `:arrow_up:` when upgrading dependencies
* :arrow_down: `:arrow_down:` when downgrading dependencies
* :shirt: `:shirt:` when removing linter warnings

Thank you! :heart:
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Goat [![GoDoc](https://godoc.org/github.com/bahlo/goat?status.png)](https://godoc.org/github.com/bahlo/goat) [![Build Status](https://secure.travis-ci.org/bahlo/goat.png?branch=master)](https://travis-ci.org/bahlo/goat) [![Coverage Status](https://coveralls.io/repos/bahlo/goat/badge.png?branch=master)](https://coveralls.io/r/bahlo/goat?branch=master)
# Goat [![GoDoc](https://godoc.org/github.com/bahlo/goat?status.svg)](https://godoc.org/github.com/bahlo/goat) [![Build Status](https://secure.travis-ci.org/bahlo/goat.svg?branch=master)](https://travis-ci.org/bahlo/goat) [![Coverage Status](https://coveralls.io/repos/bahlo/goat/badge.svg?branch=master)](https://coveralls.io/r/bahlo/goat?branch=master)

Goat is a minimalistic REST API server in Go. You can pronounce it like the
_goat_, or _go-at_. Depends on how you like goats.
Expand Down
4 changes: 4 additions & 0 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ func TestIndex(t *testing.T) {
r.Get("/bar", "bar_url", emptyHandler)
r.Post("/foo", "foo_url", emptyHandler)

sr := r.Subrouter("sub")
sr.Get("/", "sub_url", emptyHandler)

out := r.Index()
expected := map[string]string{
"bar_url": "/bar",
"foo_bar_url": "/foo/bar",
"sub_url": "/sub/",
}
if !reflect.DeepEqual(out, expected) {
t.Errorf("Index should return %v, but did return %v", expected, out)
Expand Down
18 changes: 15 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ type Middleware func(http.Handler) http.Handler
// chain calls all middlewares and returns the final handler
func (r *Router) chain() http.Handler {
var final http.Handler
final = r.router

for i := len(r.middleware) - 1; i >= 0; i-- {
final = r.middleware[i](final)
final = r.router
mw := r.allMiddleware()
for i := len(mw) - 1; i >= 0; i-- {
final = mw[i](final)
}

return final
}

// allMiddleware returns the middleware from this router and all parents
func (r *Router) allMiddleware() []Middleware {
mw := r.middleware

if r.parent != nil {
mw = append(mw, r.parent.allMiddleware()...)
}

return mw
}

// Use adds middleware to the router
func (r *Router) Use(middleware ...Middleware) {
if r.parent != nil {
Expand Down
44 changes: 44 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,47 @@ func TestUse(t *testing.T) {

// TODO: Check function equality
}

func TestUseSubrouter(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic when using middleware on subrouter")
}
}()

mw := func(h http.Handler) http.Handler { return nil }

r := New()
sr := r.Subrouter("test")
sr.Use(mw)
}

func TestAllMiddleware(t *testing.T) {
mw := func(h http.Handler) http.Handler { return nil }

r := New()
r.Use(mw)

sr := r.Subrouter("/test")
ssr := sr.Subrouter("/test")

if len(ssr.allMiddleware()) != 1 {
t.Errorf("Expected one middleware from parents in ssr")
}
}

func TestChain(t *testing.T) {
c := 0
mw := func(h http.Handler) http.Handler {
c++
return h
}

r := New()
r.Use(mw, mw, mw)
r.chain()

if c != 3 {
t.Errorf("Middleware not correctly chained")
}
}

0 comments on commit 3613b1f

Please sign in to comment.