-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into test-summary
- Loading branch information
Showing
5 changed files
with
225 additions
and
58 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const BaseService = require('./base') | ||
const emojic = require('emojic') | ||
const { InvalidResponse } = require('./errors') | ||
const trace = require('./trace') | ||
const yaml = require('js-yaml') | ||
|
||
class BaseYamlService extends BaseService { | ||
async _requestYaml({ | ||
schema, | ||
url, | ||
options = {}, | ||
errorMessages = {}, | ||
encoding = 'utf8', | ||
}) { | ||
const logTrace = (...args) => trace.logTrace('fetch', ...args) | ||
const mergedOptions = { | ||
...{ | ||
headers: { | ||
Accept: | ||
'text/x-yaml, text/yaml, application/x-yaml, application/yaml, text/plain', | ||
}, | ||
}, | ||
...options, | ||
} | ||
const { buffer } = await this._request({ | ||
url, | ||
options: mergedOptions, | ||
errorMessages, | ||
}) | ||
let parsed | ||
try { | ||
parsed = yaml.safeLoad(buffer.toString(), encoding) | ||
} catch (err) { | ||
logTrace(emojic.dart, 'Response YAML (unparseable)', buffer) | ||
throw new InvalidResponse({ | ||
prettyMessage: 'unparseable yaml response', | ||
underlyingError: err, | ||
}) | ||
} | ||
logTrace(emojic.dart, 'Response YAML (before validation)', parsed, { | ||
deep: true, | ||
}) | ||
return this.constructor._validate(parsed, schema) | ||
} | ||
} | ||
|
||
module.exports = BaseYamlService |
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,156 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const BaseYamlService = require('./base-yaml') | ||
|
||
const dummySchema = Joi.object({ | ||
requiredString: Joi.string().required(), | ||
}).required() | ||
|
||
class DummyYamlService extends BaseYamlService { | ||
static get category() { | ||
return 'cat' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'foo', | ||
} | ||
} | ||
|
||
async handle() { | ||
const { requiredString } = await this._requestYaml({ | ||
schema: dummySchema, | ||
url: 'http://example.com/foo.yaml', | ||
}) | ||
return { message: requiredString } | ||
} | ||
} | ||
|
||
const expectedYaml = ` | ||
--- | ||
requiredString: some-string | ||
` | ||
|
||
const unexpectedYaml = ` | ||
--- | ||
unexpectedKey: some-string | ||
` | ||
|
||
const invalidYaml = ` | ||
--- | ||
foo: bar | ||
foo: baz | ||
` | ||
|
||
describe('BaseYamlService', function() { | ||
describe('Making requests', function() { | ||
let sendAndCacheRequest | ||
beforeEach(function() { | ||
sendAndCacheRequest = sinon.stub().returns( | ||
Promise.resolve({ | ||
buffer: expectedYaml, | ||
res: { statusCode: 200 }, | ||
}) | ||
) | ||
}) | ||
|
||
it('invokes _sendAndCacheRequest', async function() { | ||
await DummyYamlService.invoke( | ||
{ sendAndCacheRequest }, | ||
{ handleInternalErrors: false } | ||
) | ||
|
||
expect(sendAndCacheRequest).to.have.been.calledOnceWith( | ||
'http://example.com/foo.yaml', | ||
{ | ||
headers: { | ||
Accept: | ||
'text/x-yaml, text/yaml, application/x-yaml, application/yaml, text/plain', | ||
}, | ||
} | ||
) | ||
}) | ||
|
||
it('forwards options to _sendAndCacheRequest', async function() { | ||
class WithOptions extends DummyYamlService { | ||
async handle() { | ||
const { value } = await this._requestYaml({ | ||
schema: dummySchema, | ||
url: 'http://example.com/foo.yaml', | ||
options: { method: 'POST', qs: { queryParam: 123 } }, | ||
}) | ||
return { message: value } | ||
} | ||
} | ||
|
||
await WithOptions.invoke( | ||
{ sendAndCacheRequest }, | ||
{ handleInternalErrors: false } | ||
) | ||
|
||
expect(sendAndCacheRequest).to.have.been.calledOnceWith( | ||
'http://example.com/foo.yaml', | ||
{ | ||
headers: { | ||
Accept: | ||
'text/x-yaml, text/yaml, application/x-yaml, application/yaml, text/plain', | ||
}, | ||
method: 'POST', | ||
qs: { queryParam: 123 }, | ||
} | ||
) | ||
}) | ||
}) | ||
|
||
describe('Making badges', function() { | ||
it('handles valid yaml responses', async function() { | ||
const sendAndCacheRequest = async () => ({ | ||
buffer: expectedYaml, | ||
res: { statusCode: 200 }, | ||
}) | ||
expect( | ||
await DummyYamlService.invoke( | ||
{ sendAndCacheRequest }, | ||
{ handleInternalErrors: false } | ||
) | ||
).to.deep.equal({ | ||
message: 'some-string', | ||
}) | ||
}) | ||
|
||
it('handles yaml responses which do not match the schema', async function() { | ||
const sendAndCacheRequest = async () => ({ | ||
buffer: unexpectedYaml, | ||
res: { statusCode: 200 }, | ||
}) | ||
expect( | ||
await DummyYamlService.invoke( | ||
{ sendAndCacheRequest }, | ||
{ handleInternalErrors: false } | ||
) | ||
).to.deep.equal({ | ||
color: 'lightgray', | ||
message: 'invalid response data', | ||
}) | ||
}) | ||
|
||
it('handles unparseable yaml responses', async function() { | ||
const sendAndCacheRequest = async () => ({ | ||
buffer: invalidYaml, | ||
res: { statusCode: 200 }, | ||
}) | ||
expect( | ||
await DummyYamlService.invoke( | ||
{ sendAndCacheRequest }, | ||
{ handleInternalErrors: false } | ||
) | ||
).to.deep.equal({ | ||
color: 'lightgray', | ||
message: 'unparseable yaml response', | ||
}) | ||
}) | ||
}) | ||
}) |
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