Skip to content

Commit

Permalink
Create helper functions for PATCH, HEAD and OPTIONS methods in route …
Browse files Browse the repository at this point in the history
…helper with auth capability (#44)
  • Loading branch information
georgegg authored and Sotirios Mantziaris committed May 14, 2019
1 parent 4a1b7ee commit 9d0f2a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
27 changes: 21 additions & 6 deletions sync/http/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewOptionsRoute(p string, pr sync.ProcessorFunc, trace bool) Route {
return NewRoute(p, http.MethodOptions, pr, trace, nil)
}

// NewRoute creates a new route from a generic handler.
// NewRoute creates a new route from a generic handler with auth capability.
func NewRoute(p string, m string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return Route{Pattern: p, Method: m, Handler: handler(pr), Trace: trace, Auth: auth}
}
Expand All @@ -61,27 +61,42 @@ func NewRouteRaw(p string, m string, h http.HandlerFunc, trace bool) Route {
return Route{Pattern: p, Method: m, Handler: h, Trace: trace}
}

// NewAuthGetRoute creates a new GET route from a generic handler.
// NewAuthGetRoute creates a new GET route from a generic handler with auth capability.
func NewAuthGetRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodGet, pr, trace, auth)
}

// NewAuthPostRoute creates a new POST route from a generic handler.
// NewAuthPostRoute creates a new POST route from a generic handler with auth capability.
func NewAuthPostRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodPost, pr, trace, auth)
}

// NewAuthPutRoute creates a new PUT route from a generic handler.
// NewAuthPutRoute creates a new PUT route from a generic handler with auth capability.
func NewAuthPutRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodPut, pr, trace, auth)
}

// NewAuthDeleteRoute creates a new DELETE route from a generic handler.
// NewAuthDeleteRoute creates a new DELETE route from a generic handler with auth capability.
func NewAuthDeleteRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodDelete, pr, trace, auth)
}

// NewAuthRouteRaw creates a new route from a HTTP handler.
// NewAuthPatchRoute creates a new PATCH route from a generic handler with auth capability.
func NewAuthPatchRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodPatch, pr, trace, auth)
}

// NewAuthHeadRoute creates a new HEAD route from a generic handler with auth capability.
func NewAuthHeadRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodHead, pr, trace, auth)
}

// NewAuthOptionsRoute creates a new OPTIONS route from a generic handler with auth capability.
func NewAuthOptionsRoute(p string, pr sync.ProcessorFunc, trace bool, auth auth.Authenticator) Route {
return NewRoute(p, http.MethodOptions, pr, trace, auth)
}

// NewAuthRouteRaw creates a new route from a HTTP handler with auth capability.
func NewAuthRouteRaw(p string, m string, h http.HandlerFunc, trace bool, auth auth.Authenticator) Route {
return Route{Pattern: p, Method: m, Handler: h, Trace: trace, Auth: auth}
}
25 changes: 25 additions & 0 deletions sync/http/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ func TestNewAuthDeleteRoute(t *testing.T) {
assert.True(t, r.Trace)
assert.NotNil(t, r.Auth)
}

func TestNewAuthPatchRoute(t *testing.T) {
r := NewAuthPatchRoute("/index", nil, true, &MockAuthenticator{})
assert.Equal(t, "/index", r.Pattern)
assert.Equal(t, http.MethodPatch, r.Method)
assert.True(t, r.Trace)
assert.NotNil(t, r.Auth)
}

func TestNewAuthHeadRoute(t *testing.T) {
r := NewAuthHeadRoute("/index", nil, true, &MockAuthenticator{})
assert.Equal(t, "/index", r.Pattern)
assert.Equal(t, http.MethodHead, r.Method)
assert.True(t, r.Trace)
assert.NotNil(t, r.Auth)
}

func TestNewAuthOptionsRoute(t *testing.T) {
r := NewAuthOptionsRoute("/index", nil, true, &MockAuthenticator{})
assert.Equal(t, "/index", r.Pattern)
assert.Equal(t, http.MethodOptions, r.Method)
assert.True(t, r.Trace)
assert.NotNil(t, r.Auth)
}

func TestNewAuthRouteRaw(t *testing.T) {
r := NewAuthRouteRaw("/index", http.MethodGet, nil, false, &MockAuthenticator{})
assert.Equal(t, "/index", r.Pattern)
Expand Down

0 comments on commit 9d0f2a4

Please sign in to comment.