mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
Merge pull request #37 from siriustnt/nilresponse
NewDocumentFromResponse crashes when nil pointer is passed
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/html"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"code.google.com/p/go.net/html"
|
||||
)
|
||||
|
||||
// Document represents an HTML document to be manipulated. Unlike jQuery, which
|
||||
@@ -54,6 +54,10 @@ func NewDocumentFromReader(r io.Reader) (*Document, error) {
|
||||
// It loads the specified response's document, parses it, and stores the root Document
|
||||
// node, ready to be manipulated. The response's body is closed on return.
|
||||
func NewDocumentFromResponse(res *http.Response) (*Document, error) {
|
||||
if res == nil {
|
||||
return nil, errors.New("Response is nil pointer")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
// Parse the HTML into nodes
|
||||
|
||||
@@ -151,3 +151,10 @@ func TestNewDocumentFromReader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDocumentFromResponseNil(t *testing.T) {
|
||||
_, e := NewDocumentFromResponse(nil)
|
||||
if e == nil {
|
||||
t.Error("Expected error, got none")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user