X-Git-Url: http://git.squeep.com/?p=squeep-indieauth-helper;a=blobdiff_plain;f=test%2Flib%2Fcommunication.js;fp=test%2Flib%2Fcommunication.js;h=46ef89e2642ff206207fce8783052d1ea9fee2a5;hp=7ecad8e817b396f5681953ec72e822bf65afe1e1;hb=cc52f66ba8522b6bc7002dfba79c1162a51aef0e;hpb=03cdf23c601ed191fb3d23f5d5bd7b3c7198e578 diff --git a/test/lib/communication.js b/test/lib/communication.js index 7ecad8e..46ef89e 100644 --- a/test/lib/communication.js +++ b/test/lib/communication.js @@ -16,11 +16,12 @@ const testData = require('../test-data/communication'); describe('Communication', function () { let communication, options; - beforeEach(function () { + beforeEach(async function () { options = {}; communication = new Communication(stubLogger, options); + await communication._init(); stubLogger._reset(); - sinon.stub(communication, 'axios'); + sinon.stub(communication, 'got'); }); afterEach(function () { sinon.restore(); @@ -34,20 +35,19 @@ describe('Communication', function () { communication = new Communication(stubLogger); }); - describe('Axios timing coverage', function () { - const request = {}; - const response = { - config: request, - }; - it('tags request', function () { - communication.axios.interceptors.request.handlers[0].fulfilled(request); - assert(request.startTimestampMs); + describe('_init', function () { + it('covers first use', async function () { + await communication._init({}); + assert(communication.got.called); }); - it('tags response', function () { - communication.axios.interceptors.response.handlers[0].fulfilled(response); - assert(response.elapsedTimeMs); + }); // _init + + describe('_onRetry', function () { + it('covers', function () { + communication._onRetry(new Error('oh no'), 1); + assert(communication.logger.debug.called); }); - }); // Axios timing coverage + }); // _onRetry describe('_challengeFromVerifier', function () { it('covers', function () { @@ -127,93 +127,6 @@ describe('Communication', function () { }); }); // userAgentString - describe('Axios Configurations', function () { - let requestUrl, expectedUrl; - beforeEach(function () { - requestUrl = 'https://example.com/client_id'; - expectedUrl = 'https://example.com/client_id'; - }); - it('_axiosConfig', function () { - const method = 'GET'; - const contentType = 'text/plain'; - const body = undefined; - const params = { - 'extra_parameter': 'foobar', - }; - const urlObj = new URL(requestUrl); - const expectedUrlObj = new URL(`${requestUrl}?extra_parameter=foobar`); - const expected = { - method, - url: 'https://example.com/client_id', - headers: { - 'Content-Type': 'text/plain', - }, - params: expectedUrlObj.searchParams, - responseType: 'text', - validateStatus: Communication._validateStatus, - }; - const result = Communication._axiosConfig(method, urlObj, body, params, { - 'Content-Type': contentType, - }); - delete result.transformResponse; - assert.deepStrictEqual(result, expected); - }); - it('_axiosConfig covers defaults', function () { - const method = 'OPTIONS'; - const urlObj = new URL(requestUrl); - const expectedUrlObj = new URL(requestUrl); - const expected = { - method, - url: expectedUrl, - headers: {}, - params: expectedUrlObj.searchParams, - responseType: 'text', - validateStatus: Communication._validateStatus, - }; - const result = Communication._axiosConfig(method, urlObj); - delete result.transformResponse; - assert.deepStrictEqual(result, expected); - }); - it('covers data', function () { - const method = 'POST'; - const body = Buffer.from('some data'); - const params = {}; - const urlObj = new URL(requestUrl); - const expected = { - method, - url: 'https://example.com/client_id', - data: body, - headers: {}, - params: urlObj.searchParams, - responseType: 'text', - validateStatus: Communication._validateStatus, - }; - const result = Communication._axiosConfig(method, urlObj, body, params, {}); - delete result.transformResponse; - assert.deepStrictEqual(result, expected); - }); - it('covers null response transform', function () { - const urlObj = new URL(requestUrl); - const result = Communication._axiosConfig('GET', urlObj, undefined, {}, {}); - result.transformResponse[0](); - }); - - describe('_validateStatus', function () { - it('allows normal valid', function () { - const result = Communication._validateStatus(200); - assert.strictEqual(result, true); - }); - it('allows unauthorized', function () { - const result = Communication._validateStatus(401); - assert.strictEqual(result, true); - }); - it('rejects invalid', function () { - const result = Communication._validateStatus(400); - assert.strictEqual(result, false); - }); - }); // _validateStatus - }); // Axios Configurations - describe('_baseUrlString', function () { it('covers no path', function () { const urlObj = new URL('https://example.com'); @@ -271,7 +184,7 @@ describe('Communication', function () { headers: { link: '; rel="self", ;rel="hub"', }, - data: {}, + body: {}, } }); it('covers', function () { @@ -358,12 +271,12 @@ describe('Communication', function () { urlObj = new URL('https://thuza.ratfeathers.com/'); response = { headers: Object.assign({}, testData.linkHeaders), - data: testData.hCardHtml, + body: Buffer.from(testData.hCardHtml), }; }); it('covers', async function () { - response.data = testData.hCardHtml; - communication.axios.resolves(response); + response.body = testData.hCardHtml; + communication.got.resolves(response); expected = { rels: { 'authorization_endpoint': ['https://ia.squeep.com/auth'], @@ -410,8 +323,8 @@ describe('Communication', function () { result = await communication.fetchMicroformat(urlObj); assert.deepStrictEqual(result, expected); }); - it('covers axios error', async function () { - communication.axios.rejects(new Error('blah')); + it('covers got error', async function () { + communication.got.rejects(new Error('blah')); expected = undefined; result = await communication.fetchMicroformat(urlObj); @@ -419,9 +332,9 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers non-parsable content', async function () { - response.data = 'some bare text'; + response.body = 'some bare text'; response.headers = {}; - communication.axios.resolves(response); + communication.got.resolves(response); expected = { items: [], rels: {}, @@ -434,7 +347,7 @@ describe('Communication', function () { }); it('covers non-utf8 content', async function () { response.headers['content-type'] = 'text/html; charset=ASCII'; - communication.axios.resolves(response); + communication.got.resolves(response); expected = { rels: { 'authorization_endpoint': ['https://ia.squeep.com/auth'], @@ -492,19 +405,19 @@ describe('Communication', function () { urlObj = new URL('https://thuza.ratfeathers.com/'); response = { headers: Object.assign({}, testData.linkHeaders), - data: testData.hCardHtml, + body: testData.hCardHtml, }; }); it('covers', async function () { - communication.axios.resolves(response); + communication.got.resolves(response); expected = { foo: 'bar', baz: 123 }; - response.data = JSON.stringify(expected); + response.body = expected; result = await communication.fetchJSON(urlObj); assert.deepStrictEqual(result, expected); }); - it('covers axios error', async function () { - communication.axios.rejects(new Error('blah')); + it('covers got error', async function () { + communication.got.rejects(new Error('blah')); expected = undefined; result = await communication.fetchJSON(urlObj); @@ -512,9 +425,11 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers non-parsable content', async function () { - response.data = 'some bare text'; + response.body = 'some bare text'; response.headers = {}; - communication.axios.resolves(response); + const error = new Error('oh no'); + response.request = { options: { url: new URL('https://example.com/') } }; + communication.got.rejects(new communication.Got.ParseError(error, response)); expected = undefined; result = await communication.fetchJSON(urlObj); @@ -640,11 +555,11 @@ describe('Communication', function () { urlObj = new URL('https://thuza.ratfeathers.com/'); response = { headers: {}, - data: testData.multiMF2Html, + body: testData.multiMF2Html, }; }); it('covers', async function () { - communication.axios.resolves(response); + communication.got.resolves(response); expected = { items: [{ properties: { @@ -665,14 +580,14 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers failed fetch', async function () { - communication.axios.rejects(); + communication.got.rejects(); expected = undefined; result = await communication.fetchClientIdentifier(urlObj); assert.deepStrictEqual(result, expected); }); it('covers no h-app data', async function () { - response.data = testData.noneMF2Html; - communication.axios.resolves(response); + response.body = testData.noneMF2Html; + communication.got.resolves(response); expected = { items: [], rels: {}, @@ -729,13 +644,13 @@ describe('Communication', function () { urlObj = new URL('https://thuza.ratfeathers.com/'); response = { headers: {}, - data: testData.hCardHtml, + body: testData.hCardHtml, }; sinon.stub(communication, 'fetchJSON'); }); describe('legacy without indieauth-metadata', function () { it('covers', async function () { - communication.axios.resolves(response); + communication.got.resolves(response); expected = { name: 'Thuza', photo: 'https://thuza.ratfeathers.com/image.png', @@ -752,8 +667,8 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers multiple hCards', async function () { - response.data = testData.multiMF2Html; - communication.axios.resolves(response); + response.body = testData.multiMF2Html; + communication.got.resolves(response); expected = { email: undefined, name: 'Thuza', @@ -770,7 +685,7 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers failed fetch', async function () { - communication.axios.rejects(); + communication.got.rejects(); expected = { email: undefined, name: undefined, @@ -783,8 +698,8 @@ describe('Communication', function () { }); }); it('covers', async function () { - response.data = testData.hCardMetadataHtml; - communication.axios.resolves(response); + response.body = testData.hCardMetadataHtml; + communication.got.resolves(response); communication.fetchJSON.resolves({ 'issuer': 'https://ia.squeep.com/', 'authorization_endpoint': 'https://ia.squeep.com/auth', @@ -828,8 +743,8 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers metadata missing fields', async function () { - response.data = testData.hCardMetadataHtml; - communication.axios.resolves(response); + response.body = testData.hCardMetadataHtml; + communication.got.resolves(response); communication.fetchJSON.resolves({ 'issuer': 'https://ia.squeep.com/', }); @@ -850,8 +765,8 @@ describe('Communication', function () { }); it('covers metadata response failure', async function () { const jsonError = new Error('oh no'); - response.data = testData.hCardMetadataHtml; - communication.axios + response.body = testData.hCardMetadataHtml; + communication.got .onCall(0).resolves(response) .onCall(1).rejects(jsonError); communication.fetchJSON.restore(); @@ -874,14 +789,16 @@ describe('Communication', function () { let expected, urlObj, code, codeVerifier, clientId, redirectURI; beforeEach(function () { urlObj = new URL('https://example.com/auth'); - code = Buffer.allocUnsafe(42).toString('base64').replace('/', '_').replace('+', '-'); - codeVerifier = Buffer.allocUnsafe(42).toString('base64').replace('/', '_').replace('+', '-'); + code = Buffer.allocUnsafe(42).toString('base64url'); + codeVerifier = Buffer.allocUnsafe(42).toString('base64url'); clientId = 'https://example.com/'; redirectURI = 'https://example.com/_ia'; }); it('covers', async function () { - communication.axios.resolves({ - data: '{"me":"https://profile.example.com/"}', + communication.got.resolves({ + body: { + me: 'https://profile.example.com/', + }, }); expected = { me: 'https://profile.example.com/', @@ -892,8 +809,10 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers deprecated method name', async function () { - communication.axios.resolves({ - data: '{"me":"https://profile.example.com/"}', + communication.got.resolves({ + body: { + me: 'https://profile.example.com/', + }, }); expected = { me: 'https://profile.example.com/', @@ -904,7 +823,16 @@ describe('Communication', function () { assert.deepStrictEqual(result, expected); }); it('covers failure', async function () { - communication.axios.resolves('Not a JSON payload.'); + const error = new Error('oh no'); + const response = { + request: { + options: { + url: new URL('https://example.com'), + }, + }, + }; + const parseError = new communication.Got.ParseError(error, response); + communication.got.rejects(parseError); const result = await communication.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); @@ -921,30 +849,30 @@ describe('Communication', function () { }); it('covers success active', async function () { const nowEpoch = Math.ceil(Date.now() / 1000); - communication.axios.resolves({ - data: JSON.stringify({ + communication.got.resolves({ + body: { active: true, me: 'https://profile.example.com/', 'client_id': 'https://app.example.com/', scope: 'create profile email', exp: nowEpoch + 86400, iat: nowEpoch, - }), + }, }); const result = await communication.introspectToken(introspectionUrlObj, authenticationHeader, token); assert.strictEqual(result.active, true); }); it('covers success inactive', async function () { - communication.axios.resolves({ - data: JSON.stringify({ + communication.got.resolves({ + body: { active: false, - }), + }, }); const result = await communication.introspectToken(introspectionUrlObj, authenticationHeader, token); assert.strictEqual(result.active, false); }); it('covers failure', async function () { - communication.axios.resolves('what kind of response is this?'); + communication.got.resolves({ body: 'what kind of response is this?' }); await assert.rejects(() => communication.introspectToken(introspectionUrlObj, authenticationHeader, token)); }); }); // introspectToken @@ -958,14 +886,14 @@ describe('Communication', function () { ticket = 'XXXThisIsATicketXXX'; }); it('covers success', async function () { - const expected = { data: 'blah', statusCode: 200 }; - communication.axios.resolves(expected); + const expected = { body: 'blah', statusCode: 200 }; + communication.got.resolves(expected); const result = await communication.deliverTicket(ticketEndpointUrlObj, resourceUrlObj, subjectUrlObj, ticket); assert.deepStrictEqual(result, expected); }); it('covers failure', async function () { const expectedException = new Error('oh no'); - communication.axios.rejects(expectedException); + communication.got.rejects(expectedException); await assert.rejects(() => communication.deliverTicket(ticketEndpointUrlObj, resourceUrlObj, subjectUrlObj, ticket), expectedException); }); }); // deliverTicket