Skip to content
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

Parse xml responses files as xml files #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function Osmosis(url, params) {
* @property {bool} timeout - HTTP request timeout
* @property {bool} tries - HTTP request attempts
* @property {bool} user_agent - HTTP user agent
* @property {string} parse_as - Chose between html and xml parsing mode. Default - chose automatically based on content-type
* @memberof Osmosis
* @instance
* @default
Expand All @@ -102,6 +103,7 @@ Osmosis.prototype.opts = {
statsThreshold: 25,
timeout: 30 * 1000,
tries: 3,
parse_as: 'auto',
user_agent: 'Mozilla/5.0 (Windows NT x.y; rv:10.0) ' +
'Gecko/20100101 Firefox/10.0'
};
Expand Down
14 changes: 12 additions & 2 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,18 @@ function Request(method, url, params, opts, tries, callback) {
return;
}

document = libxml.parseHtml(document,
{ baseUrl: location.href, huge: true });
var parse_mode = opts.parse_as;
if (parse_mode === 'auto')
parse_mode = getResponseType(res.headers['content-type']);
try {
if (parse_mode === 'xml')
document = libxml.parseXml(document, {baseUrl: location.href, huge: true});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, was it always throwing on parse errors or is this new with parseXml?

Can you try adding the XML parse option errors: false so we can avoid the try/catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Libxmljs docs says that unrecoverable errors are "thrown and need to be caught". I don't know is there any fatal errors in html mode, though. But anyway using try-catch even for html is safer, just in case. Why don't you like it?

Xml throws even with errors: false. Looks like this option is only about recoverable errors.

else
document = libxml.parseHtml(document, {baseUrl: location.href, huge: true});
} catch (e) {
callback('Response ' + parse_mode + ' parsing error: ' + e);
return;
}

if (document === null) {
callback('Couldn\'t parse response');
Expand Down
49 changes: 49 additions & 0 deletions test/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,44 @@ module.exports.multiple = function (assert) {
}, 5000);
}

module.exports.xml = function (assert) {
osmosis.get(url + '/xml-auto')
.then(function (context, data) {
assert.equal(context.get('link').text(), 'http://example.com');
})
.get(url + '/xml')
.then(function (context, data) {
// In html mode <link> tag treated as singleton tag and can't have any body
assert.notEqual(context.get('link').text(), 'http://example.com');
})
.get(url + '/xml')
.config('parse_as', 'xml')
.then(function (context, data) {
assert.equal(context.get('link').text(), 'http://example.com');
})
.done(function () {
assert.done();
});
};

module.exports.error_xml_parse = function (assert) {
var tries = 4;

osmosis.get(url + '/get')
.config('parse_as', 'xml')
.config('tries', tries)
.error(function (msg) {
// Multiply root elements are not allowed
if (msg.indexOf('parsing error') > -1) {
tries--;
}
})
.done(function () {
assert.strictEqual(tries, 0);
assert.done();
});
};

module.exports.absentQueryString = function (assert) {
var found = false;
osmosis.get(url + '/test-query-string')
Expand Down Expand Up @@ -202,6 +240,17 @@ server('/error-parse', function (url, req, res) {
res.end();
});

server('/xml-auto', function (url, req, res) {
res.setHeader('Content-Type', 'application/xml');
res.write('<rss><link>http://example.com</link></rss>');
res.end();
});

server('/xml', function (url, req, res) {
res.write('<rss><link>http://example.com</link></rss>');
res.end();
});


server('/redirect', function (url, req, res) {
res.write('<div>/redirect</div>');
Expand Down