Skip to content

Commit

Permalink
better coverage for missing protocolPath, fix regex replace
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed May 14, 2024
1 parent 572d79b commit a678d06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ export class HttpApi {

// Returns the data for the most recently published record under a given protocol path collection, if one is present
this.#api.get('/:did/read/protocols/:protocol/*', async (req, res) => {
if (!req.params[0]) {
return res.status(400).send('protocol path is required');
}

const protocolPath = req.params[0].replace(leadTailSlashRegex, '');
const protocol = req.params.protocol;

const query = await RecordsQuery.create({
filter: {
protocol: req.params.protocol,
protocolPath: (req.params[0] || '').replace(leadTailSlashRegex)
protocol,
protocolPath,
},
pagination: { limit: 1 },
dateSort: DateSort.PublishedDescending
Expand Down
22 changes: 22 additions & 0 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,19 @@ describe('http api', function () {
expect(record.recordId).to.equal(recordsWrite.message.recordId);
});

it('removes the trailing slash from the protocol path', async function () {
const recordsQueryCreateSpy = sinon.spy(RecordsQuery, 'create');

const urlEncodedProtocol = encodeURIComponent('http://example.com/protocol');
const protocolUrl = `http://localhost:3000/${alice.did}/read/protocols/${urlEncodedProtocol}/foo/`; // trailing slash
const recordReadResponse = await fetch(protocolUrl);
expect(recordReadResponse.status).to.equal(404);

expect(recordsQueryCreateSpy.calledOnce).to.be.true;
const recordsQueryFilter = recordsQueryCreateSpy.getCall(0).args[0].filter;
expect(recordsQueryFilter.protocolPath).to.equal('foo');
});

it('returns a 404 if record for a given protocol and protocolPath is not published', async function () {
// Create and publish a protocol
const protocolConfigure = await ProtocolsConfigure.create({
Expand Down Expand Up @@ -956,6 +969,15 @@ describe('http api', function () {
const recordReadResponse = await fetch(protocolUrl);
expect(recordReadResponse.status).to.equal(404);
});

it('returns a 400 if protocol path is not provided', async function () {
// Fetch a protocol record without providing a protocol path
const urlEncodedProtocol = encodeURIComponent('http://example.com/protocol');
const protocolUrl = `http://localhost:3000/${alice.did}/read/protocols/${urlEncodedProtocol}/`; // missing protocol path
const recordReadResponse = await fetch(protocolUrl);
expect(recordReadResponse.status).to.equal(400);
expect(await recordReadResponse.text()).to.equal('protocol path is required');
});
});

describe('/:did/query', function () {
Expand Down

0 comments on commit a678d06

Please sign in to comment.