Merge pull request #37 from siriustnt/nilresponse

NewDocumentFromResponse crashes when nil pointer is passed
This commit is contained in:
Martin Angers
2014-04-22 14:39:56 -07:00
2 changed files with 13 additions and 2 deletions
+6 -2
View File
@@ -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
+7
View File
@@ -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")
}
}