File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -48,8 +48,7 @@ func ParseMediaType(v string) MediaType {
48
48
}
49
49
}
50
50
51
- // ParseHTML parses an HTTP response as HTML document.
52
- func ParseHTML (resp * http.Response ) (* html.Node , error ) {
51
+ func readResponseBody (resp * http.Response ) (io.Reader , error ) {
53
52
var (
54
53
ce encoding.Encoding
55
54
r io.Reader = resp .Body
@@ -83,5 +82,14 @@ func ParseHTML(resp *http.Response) (*html.Node, error) {
83
82
if ce != encoding .Nop {
84
83
r = transform .NewReader (r , ce .NewDecoder ())
85
84
}
85
+ return r , nil
86
+ }
87
+
88
+ // ParseHTML parses an HTTP response as HTML document.
89
+ func ParseHTML (resp * http.Response ) (* html.Node , error ) {
90
+ r , err := readResponseBody (resp )
91
+ if err != nil {
92
+ return nil , err
93
+ }
86
94
return htmlquery .Parse (r )
87
95
}
Original file line number Diff line number Diff line change
1
+ package antch
2
+
3
+ import (
4
+ "net/http"
5
+
6
+ "github.com/antchfx/jsonquery"
7
+ )
8
+
9
+ // ParseJSON parses an HTTP response as JSON document.
10
+ func ParseJSON (resp * http.Response ) (* jsonquery.Node , error ) {
11
+ r , err := readResponseBody (resp )
12
+ if err != nil {
13
+ return nil , err
14
+ }
15
+ return jsonquery .Parse (r )
16
+ }
Original file line number Diff line number Diff line change
1
+ package antch
2
+
3
+ import (
4
+ "io/ioutil"
5
+ "net/http"
6
+ "strings"
7
+ "testing"
8
+ )
9
+
10
+ var testJSON = `{"name":"John","age":31,"city":"New York"}`
11
+
12
+ func TestParseJSON (t * testing.T ) {
13
+ res := & http.Response {
14
+ Body : ioutil .NopCloser (strings .NewReader (testJSON )),
15
+ }
16
+ _ , err := ParseJSON (res )
17
+ if err != nil {
18
+ t .Fatal (err )
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments