-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Delete BaseHTTPService and implement new BaseXmlService (affects [eclipse-marketplace f-droid]; also testing on [uptimerobot circleci]) #2037
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
06dac0e
Deleted BaseHTTPService and implemented new BaseXmlService
PyvesB b9b6a84
Fixed lint errors
PyvesB 5ec6bfe
Switched to dynamic date for badge example
PyvesB ea91b5f
Merge remote-tracking branch 'upstream/master' into refactor-base-xml
PyvesB 5a0737a
Fixed tests
PyvesB 78cede5
Switched to fast-xml-parser
PyvesB 7bc9c0b
Based check on interval
PyvesB 5de279e
Strengthened tests
PyvesB 4dd47ec
Made phrasing clearer
PyvesB 3dbbce2
Removed extension
PyvesB 9bf7c33
Merge branch 'master' into refactor-base-xml
PyvesB 5dc426d
Updated package-lock.json with latest version of npm
PyvesB 45f9e8b
Merge branch 'master' into refactor-base-xml
paulmelnikow a0de9f9
Inlined JSON and XML parsers
PyvesB 372a382
Merge branch 'master' into refactor-base-xml
PyvesB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
const { InvalidResponse } = require('../services/errors') | ||
const fastXmlParser = require('fast-xml-parser') | ||
|
||
async function asJson({ buffer, res }) { | ||
try { | ||
return JSON.parse(buffer) | ||
} catch (err) { | ||
throw new InvalidResponse({ | ||
prettyMessage: 'unparseable json response', | ||
underlyingError: err, | ||
}) | ||
} | ||
} | ||
|
||
async function asXml({ buffer, res }) { | ||
if (fastXmlParser.validate(buffer) === true) { | ||
return fastXmlParser.parse(buffer) | ||
} | ||
throw new InvalidResponse({ | ||
prettyMessage: 'unparseable xml response', | ||
}) | ||
} | ||
|
||
module.exports = { | ||
asJson, | ||
asXml, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const { asJson, asXml } = require('./response-parsers') | ||
const { InvalidResponse } = require('../services/errors') | ||
|
||
describe('Json parser', function() { | ||
it('parses json', async function() { | ||
const json = await asJson({ buffer: '{"foo": "bar"}' }) | ||
|
||
expect(json).to.deep.equal({ foo: 'bar' }) | ||
}) | ||
|
||
it('thows invalid response if json parsing fails', async function() { | ||
try { | ||
await asJson({ buffer: 'not json' }) | ||
expect.fail('Expected to throw') | ||
} catch (e) { | ||
expect(e).to.be.an.instanceof(InvalidResponse) | ||
expect(e.message).to.equal( | ||
'Invalid Response: Unexpected token o in JSON at position 1' | ||
) | ||
expect(e.prettyMessage).to.equal('unparseable json response') | ||
} | ||
}) | ||
}) | ||
|
||
describe('Xml parser', function() { | ||
it('parses xml', async function() { | ||
const xml = await asXml({ buffer: '<foo>bar</foo>' }) | ||
|
||
expect(xml).to.deep.include({ foo: 'bar' }) | ||
}) | ||
|
||
it('thows invalid response if xml parsing fails', async function() { | ||
try { | ||
await asXml({ buffer: 'not xml' }) | ||
expect.fail('Expected to throw') | ||
} catch (e) { | ||
expect(e).to.be.an.instanceof(InvalidResponse) | ||
expect(e.prettyMessage).to.equal('unparseable xml response') | ||
} | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding these tests! Well worth keeping and adjusting if this functionality is moved inline, which would widen the bracket a bit.
We aren't using
nock
directly anywhere right now, though it's probably a good idea to start. I've written service tests for things that ought to be unit tests, and smaller-bracket unit tests for things, e.g. the one in #2036, which probably should mock an HTTP response.