Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Dec 2, 2021
1 parent 57dc963 commit b6efc25
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/assets/CollectiblesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const DEPRESSIONIST_CID_V1 =
const DEPRESSIONIST_CLOUDFLARE_IPFS_SUBDOMAIN_PATH = getFormattedIpfsUrl(
CLOUDFARE_PATH,
`ipfs://${DEPRESSIONIST_CID_V1}`,
true
true,
);

describe('CollectiblesController', () => {
Expand Down
34 changes: 19 additions & 15 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,14 +1075,22 @@ describe('util', () => {

describe('getFormattedIpfsUrl', () => {
it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway without protocol prefix, no path and subdomainSupported argument set to true', () => {
expect(util.getFormattedIpfsUrl(IFPS_GATEWAY, `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`, true)).toStrictEqual(
`https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`,
);
expect(
util.getFormattedIpfsUrl(
IFPS_GATEWAY,
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`,
true,
),
).toStrictEqual(`https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`);
});

it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway with protocol prefix, a cidv0 no path and subdomainSupported argument set to true', () => {
it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway with protocol prefix, a cidv0 no path and subdomainSupported argument set to true', () => {
expect(
util.getFormattedIpfsUrl(`https://${IFPS_GATEWAY}`, `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`, true),
util.getFormattedIpfsUrl(
`https://${IFPS_GATEWAY}`,
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`,
true,
),
).toStrictEqual(`https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`);
});

Expand Down Expand Up @@ -1124,37 +1132,35 @@ describe('util', () => {
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}/test`,
),
).toStrictEqual(`${IPFS_CID_V0}/test`);
})
});

it('should return content identifier string from default ipfs url format if no path preset', () => {
expect(
util.removeIpfsProtocolPrefix(
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`,
),
).toStrictEqual(IPFS_CID_V0);
})
});

it('should return content identifier string from alternate ipfs url format', () => {
expect(
util.removeIpfsProtocolPrefix(
`${ALTERNATIVE_IPFS_URL_FORMAT}${IPFS_CID_V0}`,
),
).toStrictEqual(IPFS_CID_V0);
})
});

it('should throw error if passed a non ipfs url', () => {
expect(() => util.removeIpfsProtocolPrefix(SOME_API)).toThrow(
'this method should not be used with non ipfs urls',
);
});
})
});

describe('getIpfsCIDv1AndPath', () => {
it('should return content identifier from default ipfs url format', () => {
expect(
util.getIpfsCIDv1AndPath(
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`,
),
util.getIpfsCIDv1AndPath(`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`),
).toStrictEqual({ cid: IPFS_CID_V1, path: '' });
});

Expand All @@ -1168,9 +1174,7 @@ describe('util', () => {

it('should return unchanged content identifier if already v1', () => {
expect(
util.getIpfsCIDv1AndPath(
`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`,
),
util.getIpfsCIDv1AndPath(`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`),
).toStrictEqual({ cid: IPFS_CID_V1, path: '' });
});

Expand Down
20 changes: 9 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,21 +770,20 @@ export function validateMinimumIncrease(proposed: string, min: string) {
}

/**
* removes ipfs protocol prefix from ipfs-url
* Removes ipfs protocol prefix from ipfs-url.
*
* @param ipfsUrl - ipfs url
* @returns Ipfs content identifier and (possibly) path in a string
* @throws Will throw if the url passed is not ipfs.
* @throws will throw if the url passed is not ipfs.
*/
export function removeIpfsProtocolPrefix(ipfsUrl: string) {
if (ipfsUrl.startsWith('ipfs://ipfs/')) {
return ipfsUrl.replace('ipfs://ipfs/', '');
} else if (ipfsUrl.startsWith('ipfs://')) {
return ipfsUrl.replace('ipfs://', '');
} else {
// this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true)
throw new Error('this method should not be used with non ipfs urls');
}
// this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true)
throw new Error('this method should not be used with non ipfs urls');
}

/**
Expand Down Expand Up @@ -842,11 +841,10 @@ export function getFormattedIpfsUrl(
const gatewayHost = new URL(addUrlProtocolPrefix(ipfsGateway))?.host;
const { cid, path } = getIpfsCIDv1AndPath(ipfsUrl);
return `https://${cid}.ipfs.${gatewayHost}${path ?? ''}`;
} else {
const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl);
const gateway = ipfsGateway.endsWith('/ipfs/')
? ipfsGateway
: `${ipfsGateway}/ipfs/`;
return `${gateway}${cidAndPath}`;
}
const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl);
const gateway = ipfsGateway.endsWith('/ipfs/')
? ipfsGateway
: `${ipfsGateway}/ipfs/`;
return `${gateway}${cidAndPath}`;
}

0 comments on commit b6efc25

Please sign in to comment.