Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

routes.FilterPrefixPath() #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (m *RouteMux) AddRoute(method string, pattern string, handler http.HandlerF
if strings.HasPrefix(part, ":") {
expr := "([^/]+)"
//a user may choose to override the defult expression
// similar to expressjs: ‘/user/:id([0-9]+)’
// similar to expressjs: ‘/user/:id([0-9]+)’
if index := strings.Index(part, "("); index != -1 {
expr = part[index:]
part = part[:index]
Expand Down Expand Up @@ -138,13 +138,24 @@ func (m *RouteMux) Filter(filter http.HandlerFunc) {

// FilterParam adds the middleware filter iff the REST URL parameter exists.
func (m *RouteMux) FilterParam(param string, filter http.HandlerFunc) {
if !strings.HasPrefix(param,":") {
param = ":"+param
if !strings.HasPrefix(param, ":") {
param = ":" + param
}

m.Filter(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Query().Get(param)
if len(p) > 0 { filter(w, r) }
if len(p) > 0 {
filter(w, r)
}
})
}

// FilterParam adds the middleware filter if the prefix path exists.
func (m *RouteMux) FilterPrefixPath(path string, filter http.HandlerFunc) {
m.Filter(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, path) {
filter(w, r)
}
})
}

Expand Down
67 changes: 67 additions & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var FilterId = func(w http.ResponseWriter, r *http.Request) {
}
}

var FilterPrefixPath = func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Token")
secret := r.Header.Get("Secret")

if token == "mytoken" && secret == "mysecret" {
w.WriteHeader(http.StatusOK)

} else {
http.Error(w, "", http.StatusUnauthorized)
}
}

// TestAuthOk tests that an Auth handler will append the
// username and password to to the request URL, and will
// continue processing the request by invoking the handler.
Expand Down Expand Up @@ -148,6 +160,61 @@ func TestFilterParam(t *testing.T) {

}

// TestFilterParam tests the ability to apply middleware
// function to filter all routes with specified parameter
// in the REST url
func TestFilterPrefixPath(t *testing.T) {

r, _ := http.NewRequest("GET", "/someotherprefix", nil)
w := httptest.NewRecorder()

// first test that the prefix path filter does not trigger
handler := new(RouteMux)
handler.Get("/prefix", HandlerOk)
handler.Get("/prefix/mypath", HandlerOk) //prefix is the base bath
handler.Get("/someotherprefix", HandlerErr)
handler.FilterPrefixPath("/prefix", FilterPrefixPath)
handler.ServeHTTP(w, r)

if w.Code != http.StatusBadRequest {
t.Errorf("Code set to [%v]; want [%v]", w.Code, http.StatusBadRequest)
}

// now test the prefix path filter does trigger
r, _ = http.NewRequest("GET", "/prefix", nil)
r.Header.Set("Token", "mytoken")
r.Header.Set("Secret", "mysecret")
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)

if w.Code != http.StatusOK {
t.Errorf("Did not apply Prefix path Filter. Code set to [%v]; want [%v]", w.Code, http.StatusOK)
}

// test ok for /prefix as base path
r, _ = http.NewRequest("GET", "/prefix/mypath", nil)
r.Header.Set("Token", "mytoken")
r.Header.Set("Secret", "mysecret")
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)

if w.Code != http.StatusOK {
t.Errorf("Did not apply Prefix base path Filter. Code set to [%v]; want [%v]", w.Code, http.StatusOK)
}

//test when secret is incorrect
r, _ = http.NewRequest("GET", "/prefix", nil)
r.Header.Set("Token", "mytoken")
r.Header.Set("Secret", "incorrectsecret")
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)

if w.Code != http.StatusUnauthorized {
t.Errorf("Did not apply Prefix path Filter. Code set to [%v]; want [%v]", w.Code, http.StatusUnauthorized)
}

}

// Benchmark_RoutedHandler runs a benchmark against
// the RouteMux using the default settings.
func Benchmark_RoutedHandler(b *testing.B) {
Expand Down