Skip to content

Commit

Permalink
Merge 237a409 into 842576d
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Apr 15, 2021
2 parents 842576d + 237a409 commit dc97c9a
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
10 changes: 5 additions & 5 deletions mux.go
Expand Up @@ -129,7 +129,7 @@ func (m *Mux) FullPath() string {
if m.parent == nil {
return m.path
}
return mergePattern(mergePattern(m.parent.path, m.mountp), m.path)
return mergePattern(mergePattern(m.parent.FullPath(), m.mountp), m.path)
}

// Register registers the mux to a service.
Expand Down Expand Up @@ -165,7 +165,7 @@ func (m *Mux) callOnRegister() {
}
fp := m.FullPath()
traverse(m.root, make([]string, 0, 32), 0, func(n *node, path []string, mountIdx int) {
if n.hs.OnRegister != nil {
if n.hs != nil && n.hs.OnRegister != nil {
n.hs.OnRegister(s, Pattern(mergePattern(fp, pathSliceToString(n, path, mountIdx))), n.hs.Handler)
}
})
Expand Down Expand Up @@ -550,9 +550,9 @@ func traverse(n *node, path []string, mountIdx int, cb func(*node, []string, int
if n == nil {
return
}
if n.hs != nil {
cb(n, path, mountIdx)
}

cb(n, path, mountIdx)

if n.mounted {
mountIdx = len(path)
}
Expand Down
6 changes: 6 additions & 0 deletions request.go
Expand Up @@ -552,6 +552,9 @@ func (r *Request) executeHandler() {
var h CallHandler
if hs.Call != nil {
h = hs.Call[r.method]
if h == nil {
h = hs.Call["*"]
}
}
if h == nil {
r.reply(responseMethodNotFound)
Expand All @@ -562,6 +565,9 @@ func (r *Request) executeHandler() {
var h AuthHandler
if hs.Auth != nil {
h = hs.Auth[r.method]
if h == nil {
h = hs.Auth["*"]
}
}
if h == nil {
r.reply(responseMethodNotFound)
Expand Down
4 changes: 2 additions & 2 deletions service.go
Expand Up @@ -364,7 +364,7 @@ func GetResource(h GetHandler) Option {
//
// For pre-defined set call methods, the handler Set should be used instead.
func Call(method string, h CallHandler) Option {
if !isValidPart(method) {
if method != "*" && !isValidPart(method) {
panic("res: invalid method name: " + method)
}
return OptionFunc(func(hs *Handler) {
Expand Down Expand Up @@ -400,7 +400,7 @@ func New(h NewHandler) Option {

// Auth sets a handler for resource auth requests.
func Auth(method string, h AuthHandler) Option {
if !isValidPart(method) {
if method != "*" && !isValidPart(method) {
panic("res: invalid method name: " + method)
}
return OptionFunc(func(hs *Handler) {
Expand Down
8 changes: 4 additions & 4 deletions store/badgerstore/store.go
Expand Up @@ -112,17 +112,17 @@ func (st *Store) Get(id string) (interface{}, error) {
// Read makes a read-lock for the resource that lasts until Close is called.
func (st *Store) Read(id string) store.ReadTxn {
st.kl.RLock(id)
return readTxn{st: st, id: id, rname: []byte(st.prefix + id)}
return &readTxn{st: st, id: id, rname: []byte(st.prefix + id)}
}

// Write makes a write-lock for the resource that lasts until Close is called.
func (st *Store) Write(id string) store.WriteTxn {
st.kl.Lock(id)
return writeTxn{readTxn{st: st, id: id, rname: []byte(st.prefix + id)}}
return &writeTxn{readTxn{st: st, id: id, rname: []byte(st.prefix + id)}}
}

// Close closes the read transaction.
func (rt readTxn) Close() error {
func (rt *readTxn) Close() error {
if rt.closed {
return errors.New("already closed")
}
Expand All @@ -132,7 +132,7 @@ func (rt readTxn) Close() error {
}

// Close closes the write transaction.
func (wt writeTxn) Close() error {
func (wt *writeTxn) Close() error {
if wt.closed {
return errors.New("already closed")
}
Expand Down
8 changes: 4 additions & 4 deletions store/mockstore/store.go
Expand Up @@ -82,17 +82,17 @@ func (st *Store) Add(id string, v interface{}) *Store {
// Read makes a read-lock for the resource that lasts until Close is called.
func (st *Store) Read(id string) store.ReadTxn {
st.RLock()
return readTxn{st: st, id: id}
return &readTxn{st: st, id: id}
}

// Write makes a write-lock for the resource that lasts until Close is called.
func (st *Store) Write(id string) store.WriteTxn {
st.Lock()
return writeTxn{readTxn{st: st, id: id}}
return &writeTxn{readTxn{st: st, id: id}}
}

// Close closes the read transaction.
func (rt readTxn) Close() error {
func (rt *readTxn) Close() error {
if rt.closed {
return errors.New("already closed")
}
Expand All @@ -102,7 +102,7 @@ func (rt readTxn) Close() error {
}

// Close closes the write transaction.
func (wt writeTxn) Close() error {
func (wt *writeTxn) Close() error {
if wt.closed {
return errors.New("already closed")
}
Expand Down
35 changes: 35 additions & 0 deletions test/04call_request_test.go
Expand Up @@ -406,6 +406,41 @@ func TestCallRequest_UnknownMethod_ErrorMethodNotFound(t *testing.T) {
})
}

// Test call request with wildcard method returns result
func TestCallRequest_WildcardMethod_CallsHandler(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model",
res.Call("method", func(r res.CallRequest) {
r.OK(mock.Result)
}),
res.Call("*", func(r res.CallRequest) {
r.Error(mock.CustomError)
}),
)
}, func(s *restest.Session) {
s.Call("test.model", "method", nil).
Response().
AssertResult(mock.Result)

s.Call("test.model", "unset", nil).
Response().
AssertError(mock.CustomError)
})
}

// Test call request with wildcard method returns result
func TestCallRequest_NonWildcardMethod_CallsHandler(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model", res.Call("*", func(r res.CallRequest) {
r.OK(mock.Result)
}))
}, func(s *restest.Session) {
s.Call("test.model", "unset", nil).
Response().
AssertResult(mock.Result)
})
}

// Test that multiple responses to call request causes panic
func TestCall_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *res.Service) {
Expand Down
35 changes: 35 additions & 0 deletions test/06auth_request_test.go
Expand Up @@ -425,6 +425,41 @@ func TestAuthRequest_UnknownMethod_ErrorMethodNotFound(t *testing.T) {
})
}

// Test auth request with wildcard method returns result
func TestAuthRequest_WildcardMethod_CallsHandler(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model", res.Auth("*", func(r res.AuthRequest) {
r.OK(mock.Result)
}))
}, func(s *restest.Session) {
s.Auth("test.model", "unset", nil).
Response().
AssertResult(mock.Result)
})
}

// Test auth request with wildcard method returns result
func TestAuthRequest_NonWildcardMethod_CallsHandler(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model",
res.Auth("method", func(r res.AuthRequest) {
r.OK(mock.Result)
}),
res.Auth("*", func(r res.AuthRequest) {
r.Error(mock.CustomError)
}),
)
}, func(s *restest.Session) {
s.Auth("test.model", "method", nil).
Response().
AssertResult(mock.Result)

s.Auth("test.model", "unset", nil).
Response().
AssertError(mock.CustomError)
})
}

// Test that multiple responses to auth request causes panic
func TestAuth_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *res.Service) {
Expand Down

0 comments on commit dc97c9a

Please sign in to comment.