Skip to content

[FIX] Parsing XML from a URL #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"

"github.com/antchfx/xpath"
Expand All @@ -19,12 +20,12 @@ func LoadURL(url string) (*Node, error) {
return nil, err
}
defer resp.Body.Close()
// Checking the HTTP Content-Type value from the response headers.(#39)
v := strings.ToLower(resp.Header.Get("Content-Type"))
if v == "text/xml" || v == "application/xml" {
// Make sure the Content-Type has a valid XML MIME type
regex := regexp.MustCompile(`(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`)
if regex.MatchString(resp.Header.Get("Content-Type")) {
return Parse(resp.Body)
}
return nil, fmt.Errorf("invalid XML document(%s)", v)
return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type"))
}

// Parse returns the parse tree for the XML from the given Reader.
Expand Down
91 changes: 78 additions & 13 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,91 @@
package xmlquery

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestLoadURL(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := `<?xml version="1.0"?>
<rss>
<title></title>
</rss>`
w.Header().Set("Content-Type", "text/xml")
w.Write([]byte(s))
}))
defer server.Close()
_, err := LoadURL(server.URL)
if err != nil {
t.Fatal(err)
func TestLoadURLSuccess(t *testing.T) {
contentTypes := []string{
"application/vnd.paos.xml",
"application/vnd.otps.ct-kip+xml",
"application/vnd.openxmlformats-package.core-properties+xml",
"application/CDFX+XML",
"application/ATXML",
"application/3gpdash-qoe-report+xml",
"application/vnd.nokia.pcd+wbxml",
"image/svg+xml",
"message/imdn+xml",
"model/vnd.collada+xml",
"text/xml-external-parsed-entity",
"text/xml",
"aPPLIcaTioN/xMl; charset=UTF-8",
"application/xhtml+xml",
"application/xml",
"text/xmL; charset=UTF-8",
"application/aTOM+xmL; charset=UTF-8",
"application/RsS+xmL; charset=UTF-8",
"application/maTHml+xmL; charset=UTF-8",
"application/xslt+xmL; charset=UTF-8",
}

for _, contentType := range contentTypes {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := `<?xml version="1.0"?>
<parent>
<child></child>
</parent>
`
w.Header().Set("Content-Type", contentType)
w.Write([]byte(s))
}))
defer server.Close()
_, err := LoadURL(server.URL)
if err != nil {
t.Fatal(err)
}
}
}

func TestLoadURLFailure(t *testing.T) {
contentTypes := []string{
"application/pdf",
"application/json",
"application/tlsrpt+gzip",
"application/vnd.3gpp.pic-bw-small",
"application/vnd.collabio.xodocuments.document-template",
"application/vnd.ctc-posml",
"application/vnd.gov.sk.e-form+zip",
"audio/mp4",
"audio/vnd.sealedmedia.softseal.mpeg",
"image/png",
"image/vnd.adobe.photoshop",
"message/example",
"message/vnd.wfa.wsc",
"model/vnd.usdz+zip",
"model/vnd.valve.source.compiled-map",
"multipart/signed",
"text/css",
"text/html",
"video/quicktime",
"video/JPEG",
}

for _, contentType := range contentTypes {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType)
}))
defer server.Close()
_, err := LoadURL(server.URL)
if err != nil && err.Error() == fmt.Sprintf("invalid XML document(%s)", contentType) {
return
}

t.Fatalf("Want invalid XML document(%s), got %v", contentType, err)
}
}

Expand Down