Skip to content
This repository was archived by the owner on May 24, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6210890

Browse files
committedAug 5, 2013
Provide a helper to decode the JSON response payload
1 parent ceb1556 commit 6210890

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
 

‎test/util.go

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package test
99
import (
1010
"encoding/json"
1111
"fmt"
12+
"io/ioutil"
1213
"net/http"
1314
"net/http/httptest"
1415
"strings"
@@ -72,6 +73,18 @@ func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string) {
7273
}
7374
}
7475

76+
func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error {
77+
content, err := ioutil.ReadAll(r.Body)
78+
if err != nil {
79+
return err
80+
}
81+
err = json.Unmarshal(content, v)
82+
if err != nil {
83+
return err
84+
}
85+
return nil
86+
}
87+
7588
type Recorded struct {
7689
T *testing.T
7790
Recorder *httptest.ResponseRecorder
@@ -102,3 +115,7 @@ func (self *Recorded) ContentEncodingIsGzip() {
102115
func (self *Recorded) BodyIs(expectedBody string) {
103116
BodyIs(self.T, self.Recorder, expectedBody)
104117
}
118+
119+
func (self *Recorded) DecodeJsonPayload(v interface{}) error {
120+
return DecodeJsonPayload(self.Recorder, v)
121+
}

0 commit comments

Comments
 (0)
This repository has been archived.