Merge lp:~pedronis/ubuntu-push/check-data into lp:ubuntu-push

Proposed by Samuele Pedroni
Status: Merged
Approved by: Samuele Pedroni
Approved revision: 30
Merged at revision: 29
Proposed branch: lp:~pedronis/ubuntu-push/check-data
Merge into: lp:ubuntu-push
Diff against target: 77 lines (+34/-1)
2 files modified
server/api/handlers.go (+16/-0)
server/api/handlers_test.go (+18/-1)
To merge this branch: bzr merge lp:~pedronis/ubuntu-push/check-data
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+203696@code.launchpad.net

Commit message

check that the data field is not missing

Description of the change

check that the data field is not missing (later bits will be very sad otherwise)

To post a comment you must log in.
Revision history for this message
John Lenton (chipaca) wrote :

Thank you.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'server/api/handlers.go'
--- server/api/handlers.go 2014-01-16 16:22:19 +0000
+++ server/api/handlers.go 2014-01-29 10:12:43 +0000
@@ -91,6 +91,11 @@
91 ioError,91 ioError,
92 "Could not read request body",92 "Could not read request body",
93 }93 }
94 ErrMissingData = &APIError{
95 http.StatusBadRequest,
96 invalidRequest,
97 "Missing data field",
98 }
94 ErrUnknownChannel = &APIError{99 ErrUnknownChannel = &APIError{
95 http.StatusBadRequest,100 http.StatusBadRequest,
96 unknownChannel,101 unknownChannel,
@@ -172,6 +177,13 @@
172 return body, nil177 return body, nil
173}178}
174179
180func checkBroadcast(bcast *Broadcast) *APIError {
181 if len(bcast.Data) == 0 {
182 return ErrMissingData
183 }
184 return nil
185}
186
175// state holds the interfaces to delegate to serving requests187// state holds the interfaces to delegate to serving requests
176type state struct {188type state struct {
177 store store.PendingStore189 store store.PendingStore
@@ -182,6 +194,10 @@
182type BroadcastHandler state194type BroadcastHandler state
183195
184func (h *BroadcastHandler) doBroadcast(bcast *Broadcast) *APIError {196func (h *BroadcastHandler) doBroadcast(bcast *Broadcast) *APIError {
197 apiErr := checkBroadcast(bcast)
198 if apiErr != nil {
199 return apiErr
200 }
185 chanId, err := h.store.GetInternalChannelId(bcast.Channel)201 chanId, err := h.store.GetInternalChannelId(bcast.Channel)
186 if err != nil {202 if err != nil {
187 switch err {203 switch err {
188204
=== modified file 'server/api/handlers_test.go'
--- server/api/handlers_test.go 2014-01-14 15:35:20 +0000
+++ server/api/handlers_test.go 2014-01-29 10:12:43 +0000
@@ -232,7 +232,7 @@
232 payload := json.RawMessage(`{"foo":"bar"}`)232 payload := json.RawMessage(`{"foo":"bar"}`)
233233
234 request := newPostRequest("/broadcast", &Broadcast{234 request := newPostRequest("/broadcast", &Broadcast{
235 Channel: "unkown",235 Channel: "unknown",
236 ExpireAfter: 60,236 ExpireAfter: 60,
237 Data: payload,237 Data: payload,
238 }, testServer)238 }, testServer)
@@ -242,6 +242,23 @@
242 checkError(c, response, ErrUnknownChannel)242 checkError(c, response, ErrUnknownChannel)
243}243}
244244
245func (s *handlersSuite) TestMissingData(c *C) {
246 testServer := httptest.NewServer(&BroadcastHandler{})
247 defer testServer.Close()
248
249 packedMessage := []byte(`{"channel": "system"}`)
250 reader := bytes.NewReader(packedMessage)
251
252 request, err := http.NewRequest("POST", testServer.URL, reader)
253 c.Assert(err, IsNil)
254 request.ContentLength = int64(len(packedMessage))
255 request.Header.Set("Content-Type", "application/json")
256
257 response, err := s.client.Do(request)
258 c.Assert(err, IsNil)
259 checkError(c, response, ErrMissingData)
260}
261
245func (s *handlersSuite) TestCannotBroadcastMalformedData(c *C) {262func (s *handlersSuite) TestCannotBroadcastMalformedData(c *C) {
246 testServer := httptest.NewServer(&BroadcastHandler{})263 testServer := httptest.NewServer(&BroadcastHandler{})
247 defer testServer.Close()264 defer testServer.Close()

Subscribers

People subscribed via source and target branches