Skip to content

Commit

Permalink
Change and extend web body helpers (#16)
Browse files Browse the repository at this point in the history
* Change and extend web body helpers

* Fix linter errors

* Fix linter errors
  • Loading branch information
themue authored Dec 15, 2021
1 parent c3144c0 commit 0bbd5bd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module tideland.dev/go/audit

go 1.16
go 1.17
26 changes: 21 additions & 5 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,34 @@ func (w *ResponseWriter) finalize(r *http.Request) {
}

//--------------------
// RESPONSE HELPER
// BODY HELPER
//--------------------

// BodyAsString reads the whole body and simply interprets it as string.
func BodyAsString(r *http.Response) (string, error) {
// StringToBody sets the request body to a given string.
func StringToBody(s string, r *http.Request) {
r.Body = ioutil.NopCloser(bytes.NewBufferString(s))
}

// JSONToBody sets the request body to the JSON representation of
// the given object.
func JSONToBody(obj interface{}, r *http.Request) error {
b := bytes.NewBuffer(nil)
if err := json.NewEncoder(b).Encode(obj); err != nil {
return err
}
r.Body = ioutil.NopCloser(b)
return nil
}

// BodyToString reads the whole body and simply interprets it as string.
func BodyToString(r *http.Response) (string, error) {
bs, err := ioutil.ReadAll(r.Body)
return string(bs), err
}

// BodyAsJSON reads the whole body and decodes the JSON content into the
// BodyToJSON reads the whole body and decodes the JSON content into the
// given object.
func BodyAsJSON(r *http.Response, obj interface{}) error {
func BodyToJSON(r *http.Response, obj interface{}) error {
return json.NewDecoder(r.Body).Decode(obj)
}

Expand Down
53 changes: 51 additions & 2 deletions web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,54 @@ func TestSimpleRequests(t *testing.T) {
resp, err := s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, http.StatusOK)
body, err := web.BodyAsString(resp)
body, err := web.BodyToString(resp)
assert.NoError(err)
assert.Equal(body, test.expected)
}
}

// TestJSONBody verifies the reading and writing of JSON bodies.
func TestJSONBody(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)

// Correctly marshalling data.
s := web.NewFuncSimulator(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
assert.NoError(err)
_, err = w.Write(b)
assert.NoError(err)
})
req, err := http.NewRequest(http.MethodGet, "https://localhost:8080/", nil)
assert.NoError(err)
err = web.JSONToBody(data{"correct", 12345, true}, req)
assert.NoError(err)
resp, err := s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, http.StatusOK)
var obj data
err = web.BodyToJSON(resp, &obj)
assert.NoError(err)
assert.Equal(obj.A, "correct")
assert.Equal(obj.B, 12345)
assert.Equal(obj.C, true)

// Failing marshalling data.
s = web.NewFuncSimulator(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("{xyz)[]"))
assert.NoError(err)
})
req, err = http.NewRequest(http.MethodGet, "https://localhost:8080/", nil)
assert.NoError(err)
err = web.JSONToBody(data{"correct", 12345, true}, req)
assert.NoError(err)
resp, err = s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, http.StatusOK)
err = web.BodyToJSON(resp, &obj)
assert.ErrorContains(err, "invalid character")

}

// TestResponseCode verifies that the status code cannot be changed after
// writing to the response body.
func TestResponseCode(t *testing.T) {
Expand Down Expand Up @@ -120,7 +162,7 @@ func TestPreprocessors(t *testing.T) {
resp, err := s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, http.StatusOK)
body, err := web.BodyAsString(resp)
body, err := web.BodyToString(resp)
assert.NoError(err)
assert.Equal(body, test.expected)
}
Expand Down Expand Up @@ -156,4 +198,11 @@ func (h *echoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "m(%s) p(%s) ct(%s) a(%s) b(%s)", m, p, ct, a, string(bs))
}

// data is used when testing the JSON marshalling.
type data struct {
A string `json:"a"`
B int `json:"b"`
C bool `json:"c"`
}

// EOF

0 comments on commit 0bbd5bd

Please sign in to comment.