Skip to content

Commit

Permalink
GH-77: Ensured empty request payload is properly handled.
Browse files Browse the repository at this point in the history
Added tests with empty request payload.
  • Loading branch information
jirenius committed May 29, 2020
1 parent 07b0042 commit 8f7a686
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
15 changes: 9 additions & 6 deletions queryevent.go
Expand Up @@ -149,15 +149,18 @@ func (qe *queryEvent) handleQueryRequest(m *nats.Msg) {
}

var rqr resQueryRequest
err := json.Unmarshal(m.Data, &rqr)
if err != nil {
s.errorf("Error unmarshaling incoming query request: %s", err)
qr.error(ToError(err))
return
var err error
if len(m.Data) > 0 {
err = json.Unmarshal(m.Data, &rqr)
if err != nil {
s.errorf("Error unmarshaling incoming query request: %s", err)
qr.error(ToError(err))
return
}
}

if rqr.Query == "" {
s.errorf("Missing query on incoming query request: %s", err)
s.errorf("Missing query on incoming query request: %s", m.Data)
qr.reply(responseMissingQuery)
return
}
Expand Down
22 changes: 10 additions & 12 deletions request.go
Expand Up @@ -401,12 +401,11 @@ func (r *Request) New(rid Ref) {
//
// Only valid for call and auth requests.
func (r *Request) ParseParams(p interface{}) {
if len(r.params) == 0 {
return
}
err := json.Unmarshal(r.params, p)
if err != nil {
panic(&Error{Code: CodeInvalidParams, Message: err.Error()})
if len(r.params) > 0 {
err := json.Unmarshal(r.params, p)
if err != nil {
panic(&Error{Code: CodeInvalidParams, Message: err.Error()})
}
}
}

Expand All @@ -416,12 +415,11 @@ func (r *Request) ParseParams(p interface{}) {
//
// Not valid for get requests.
func (r *Request) ParseToken(t interface{}) {
if len(r.token) == 0 {
return
}
err := json.Unmarshal(r.token, t)
if err != nil {
panic(InternalError(err))
if len(r.token) > 0 {
err := json.Unmarshal(r.token, t)
if err != nil {
panic(InternalError(err))
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/03get_request_test.go
Expand Up @@ -592,3 +592,16 @@ func TestGet_WithMultipleResponses_CausesPanic(t *testing.T) {
AssertModel(mock.Model)
})
}

func TestGetResource_WithEmptyRequestPayload_ReturnsModel(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model.foo", res.GetResource(func(r res.GetRequest) {
r.Model(mock.Model)
}))
}, func(s *restest.Session) {
inb := s.RequestRaw("get.test.model.foo", nil)
s.GetMsg().
AssertSubject(inb).
AssertModel(mock.Model)
})
}
13 changes: 13 additions & 0 deletions test/04call_request_test.go
Expand Up @@ -434,3 +434,16 @@ func TestCallRequest_InvalidJSON_RespondsWithInternalError(t *testing.T) {
AssertErrorCode(res.CodeInternalError)
})
}

func TestCallRequest_WithEmptyRequestPayload_ReturnsResult(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model.foo",
res.Call("method", func(r res.CallRequest) { r.OK(nil) }),
)
}, func(s *restest.Session) {
inb := s.RequestRaw("call.test.model.foo.method", nil)
s.GetMsg().
AssertSubject(inb).
AssertResult(nil)
})
}
13 changes: 13 additions & 0 deletions test/06auth_request_test.go
Expand Up @@ -453,3 +453,16 @@ func TestAuthRequest_InvalidJSON_RespondsWithInternalError(t *testing.T) {
AssertErrorCode(res.CodeInternalError)
})
}

func TestAuthRequest_WithEmptyRequestPayload_ReturnsResult(t *testing.T) {
runTest(t, func(s *res.Service) {
s.Handle("model.foo",
res.Auth("method", func(r res.AuthRequest) { r.OK(nil) }),
)
}, func(s *restest.Session) {
inb := s.RequestRaw("auth.test.model.foo.method", nil)
s.GetMsg().
AssertSubject(inb).
AssertResult(nil)
})
}

0 comments on commit 8f7a686

Please sign in to comment.