From e1d6592b818bc4348078ca6593eea4641caafae5 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Mon, 26 Sep 2022 17:36:12 +0200 Subject: [PATCH] fix(oob): allow encoding in content type header (#1037) Signed-off-by: Timo Glastra --- .../src/utils/__tests__/shortenedUrl.test.ts | 21 ++++++++++++++----- packages/core/src/utils/parseInvitation.ts | 6 +++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/core/src/utils/__tests__/shortenedUrl.test.ts b/packages/core/src/utils/__tests__/shortenedUrl.test.ts index a6e2364f97..5e79621e96 100644 --- a/packages/core/src/utils/__tests__/shortenedUrl.test.ts +++ b/packages/core/src/utils/__tests__/shortenedUrl.test.ts @@ -7,7 +7,7 @@ import { OutOfBandInvitation } from '../../modules/oob' import { convertToNewInvitation } from '../../modules/oob/helpers' import { JsonTransformer } from '../JsonTransformer' import { MessageValidator } from '../MessageValidator' -import { oobInvitationfromShortUrl } from '../parseInvitation' +import { oobInvitationFromShortUrl } from '../parseInvitation' const mockOobInvite = { '@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/invitation', @@ -89,21 +89,32 @@ beforeAll(async () => { describe('shortened urls resolving to oob invitations', () => { test('Resolve a mocked response in the form of a oob invitation as a json object', async () => { - const short = await oobInvitationfromShortUrl(mockedResponseOobJson) + const short = await oobInvitationFromShortUrl(mockedResponseOobJson) expect(short).toEqual(outOfBandInvitationMock) }) test('Resolve a mocked response in the form of a oob invitation encoded in an url', async () => { - const short = await oobInvitationfromShortUrl(mockedResponseOobUrl) + const short = await oobInvitationFromShortUrl(mockedResponseOobUrl) + expect(short).toEqual(outOfBandInvitationMock) + }) + + test("Resolve a mocked response in the form of a oob invitation as a json object with header 'application/json; charset=utf-8'", async () => { + const short = await oobInvitationFromShortUrl({ + ...mockedResponseOobJson, + headers: new Headers({ + 'content-type': 'application/json; charset=utf-8', + }), + } as Response) expect(short).toEqual(outOfBandInvitationMock) }) }) + describe('shortened urls resolving to connection invitations', () => { test('Resolve a mocked response in the form of a connection invitation as a json object', async () => { - const short = await oobInvitationfromShortUrl(mockedResponseConnectionJson) + const short = await oobInvitationFromShortUrl(mockedResponseConnectionJson) expect(short).toEqual(connectionInvitationToNew) }) test('Resolve a mocked Response in the form of a connection invitation encoded in an url', async () => { - const short = await oobInvitationfromShortUrl(mockedResponseConnectionUrl) + const short = await oobInvitationFromShortUrl(mockedResponseConnectionUrl) expect(short).toEqual(connectionInvitationToNew) }) }) diff --git a/packages/core/src/utils/parseInvitation.ts b/packages/core/src/utils/parseInvitation.ts index 713360512e..6f3c9e8f3b 100644 --- a/packages/core/src/utils/parseInvitation.ts +++ b/packages/core/src/utils/parseInvitation.ts @@ -54,9 +54,9 @@ export const parseInvitationUrl = (invitationUrl: string): OutOfBandInvitation = } //This currently does not follow the RFC because of issues with fetch, currently uses a janky work around -export const oobInvitationfromShortUrl = async (response: Response): Promise => { +export const oobInvitationFromShortUrl = async (response: Response): Promise => { if (response) { - if (response.headers.get('Content-Type') === 'application/json' && response.ok) { + if (response.headers.get('Content-Type')?.startsWith('application/json') && response.ok) { const invitationJson = await response.json() const parsedMessageType = parseMessageType(invitationJson['@type']) if (supportsIncomingMessageType(parsedMessageType, OutOfBandInvitation.type)) { @@ -107,7 +107,7 @@ export const parseInvitationShortUrl = async ( return convertToNewInvitation(invitation) } else { try { - return oobInvitationfromShortUrl(await fetchShortUrl(invitationUrl, dependencies)) + return oobInvitationFromShortUrl(await fetchShortUrl(invitationUrl, dependencies)) } catch (error) { throw new AriesFrameworkError( 'InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters: `oob`, `c_i` or `d_m`, or be valid shortened URL'