Merge lp:~pedronis/ubuntu-push/expose-api-helpers into lp:ubuntu-push

Proposed by Samuele Pedroni
Status: Merged
Approved by: Samuele Pedroni
Approved revision: 73
Merged at revision: 71
Proposed branch: lp:~pedronis/ubuntu-push/expose-api-helpers
Merge into: lp:ubuntu-push
Diff against target: 108 lines (+25/-13)
2 files modified
server/api/handlers.go (+12/-10)
server/api/handlers_test.go (+13/-3)
To merge this branch: bzr merge lp:~pedronis/ubuntu-push/expose-api-helpers
Reviewer Review Type Date Requested Status
Nicola Larosa (community) Approve
Review via email: mp+207502@code.launchpad.net

Commit message

expose a couple of helpers for reuse

Description of the change

expose a couple of helpers for reuse

To post a comment you must log in.
Revision history for this message
Nicola Larosa (teknico) wrote :

LGTM. Only, "isn't event read" in TestReadBodyTooBig looks like a question, maybe "event is not read".

review: Approve
73. By Samuele Pedroni

fix comment

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'server/api/handlers.go'
2--- server/api/handlers.go 2014-02-18 14:15:55 +0000
3+++ server/api/handlers.go 2014-02-21 11:31:14 +0000
4@@ -47,7 +47,7 @@
5 const (
6 ioError = "io-error"
7 invalidRequest = "invalid-request"
8- unknownChannel = "unknown channel"
9+ unknownChannel = "unknown-channel"
10 unavailable = "unavailable"
11 internalError = "internal"
12 )
13@@ -143,7 +143,8 @@
14 Data json.RawMessage `json:"data"`
15 }
16
17-func respondError(writer http.ResponseWriter, apiErr *APIError) {
18+// RespondError writes back a JSON error response for a APIError.
19+func RespondError(writer http.ResponseWriter, apiErr *APIError) {
20 wireError, err := json.Marshal(apiErr)
21 if err != nil {
22 panic(fmt.Errorf("couldn't marshal our own errors: %v", err))
23@@ -153,21 +154,21 @@
24 writer.Write(wireError)
25 }
26
27-func checkContentLength(request *http.Request) *APIError {
28+func checkContentLength(request *http.Request, maxBodySize int64) *APIError {
29 if request.ContentLength == -1 {
30 return ErrNoContentLengthProvided
31 }
32 if request.ContentLength == 0 {
33 return ErrRequestBodyEmpty
34 }
35- if request.ContentLength > MaxRequestBodyBytes {
36+ if request.ContentLength > maxBodySize {
37 return ErrRequestBodyTooLarge
38 }
39 return nil
40 }
41
42-func checkRequestAsPost(request *http.Request) *APIError {
43- if err := checkContentLength(request); err != nil {
44+func checkRequestAsPost(request *http.Request, maxBodySize int64) *APIError {
45+ if err := checkContentLength(request, maxBodySize); err != nil {
46 return err
47 }
48 if request.Header.Get("Content-Type") != JSONMediaType {
49@@ -179,8 +180,9 @@
50 return nil
51 }
52
53-func readBody(request *http.Request) ([]byte, *APIError) {
54- if err := checkRequestAsPost(request); err != nil {
55+// ReadBody checks that a POST request is well-formed and reads its body.
56+func ReadBody(request *http.Request, maxBodySize int64) ([]byte, *APIError) {
57+ if err := checkRequestAsPost(request, maxBodySize); err != nil {
58 return nil, err
59 }
60
61@@ -264,11 +266,11 @@
62 var apiErr *APIError
63 defer func() {
64 if apiErr != nil {
65- respondError(writer, apiErr)
66+ RespondError(writer, apiErr)
67 }
68 }()
69
70- body, apiErr := readBody(request)
71+ body, apiErr := ReadBody(request, MaxRequestBodyBytes)
72 if apiErr != nil {
73 return
74 }
75
76=== modified file 'server/api/handlers_test.go'
77--- server/api/handlers_test.go 2014-02-18 14:15:55 +0000
78+++ server/api/handlers_test.go 2014-02-21 11:31:14 +0000
79@@ -59,16 +59,26 @@
80 c.Check(string(wire), Equals, `{"error":"invalid-request","message":"Message"}`)
81 }
82
83-func (s *handlersSuite) TestReadyBodyReadError(c *C) {
84+func (s *handlersSuite) TestReadBodyReadError(c *C) {
85 r := bytes.NewReader([]byte{}) // eof too early
86 req, err := http.NewRequest("POST", "", r)
87+ c.Assert(err, IsNil)
88 req.Header.Set("Content-Type", "application/json")
89 req.ContentLength = 1000
90- c.Assert(err, IsNil)
91- _, err = readBody(req)
92+ _, err = ReadBody(req, 2000)
93 c.Check(err, Equals, ErrCouldNotReadBody)
94 }
95
96+func (s *handlersSuite) TestReadBodyTooBig(c *C) {
97+ r := bytes.NewReader([]byte{}) // not read
98+ req, err := http.NewRequest("POST", "", r)
99+ c.Assert(err, IsNil)
100+ req.Header.Set("Content-Type", "application/json")
101+ req.ContentLength = 3000
102+ _, err = ReadBody(req, 2000)
103+ c.Check(err, Equals, ErrRequestBodyTooLarge)
104+}
105+
106 func (s *handlersSuite) TestGetStore(c *C) {
107 ctx := &context{storeForRequest: func(w http.ResponseWriter, r *http.Request) (store.PendingStore, error) {
108 return nil, ErrStoreUnavailable

Subscribers

People subscribed via source and target branches