Skip to content

Commit 0744a34

Browse files
committed
Add mocha/chai tests for stdin, files, & HTTP.
1 parent 619786d commit 0744a34

4 files changed

Lines changed: 111 additions & 2 deletions

File tree

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
},
4343
"devDependencies": {
4444
"@digitalbazaar/eslint-config": "^8.0.1",
45-
"eslint": "^9.39.4"
45+
"eslint": "^9.39.4",
46+
"mocha": "^11.7.5",
47+
"chai": "^6.2.2"
4648
},
4749
"engines": {
4850
"node": "^20.19.0 || ^22.13.0 || >=24.0.0"
@@ -57,6 +59,7 @@
5759
"jsonld"
5860
],
5961
"scripts": {
60-
"lint": "eslint ."
62+
"lint": "eslint .",
63+
"test": "mocha \"test/**/*.test.js\""
6164
}
6265
}

test/fixtures/sample.jsonld

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"@context": "https://www.w3.org/2018/credentials/v1",
3+
"id": "urn:uuid:0000-0000-0000-0000",
4+
"type": ["VerifiableCredential"],
5+
"credentialSubject": {
6+
"id": "did:example:123",
7+
"name": "Example"
8+
}
9+
}

test/jsonld-request.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import * as chai from 'chai';
2+
import {fileURLToPath} from 'node:url';
3+
import {promises as fs} from 'node:fs';
4+
import http from 'node:http';
5+
import {jsonldRequest} from '../lib/index.js';
6+
import path from 'node:path';
7+
import {spawn} from 'node:child_process';
8+
9+
const should = chai.should();
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = path.dirname(__filename);
12+
13+
describe('jsonldRequest', function() {
14+
this.timeout(10000);
15+
16+
let fixturePath;
17+
let fixtureData;
18+
19+
before(async () => {
20+
fixturePath = path.join(__dirname, 'fixtures', 'sample.jsonld');
21+
fixtureData = await fs.readFile(fixturePath, 'utf8');
22+
});
23+
24+
it('loads a local file (file://) and parses JSON-LD', async () => {
25+
const fileUrl = `file://${fixturePath}`;
26+
const {data} = await jsonldRequest(fileUrl, {allow: ['file']});
27+
should.exist(data);
28+
data.should.be.an('object');
29+
data.should.have.property('@context');
30+
});
31+
32+
it('loads JSON-LD over HTTP and parses it', async () => {
33+
// start local HTTP server to serve the fixture
34+
const server = http.createServer((req, res) => {
35+
const headers = {
36+
'Content-Type': 'application/ld+json; charset=utf-8'
37+
};
38+
res.writeHead(200, headers);
39+
res.end(fixtureData);
40+
});
41+
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
42+
const addr = server.address();
43+
const url = `http://127.0.0.1:${addr.port}/fixture.json`;
44+
try {
45+
const {data} = await jsonldRequest(url, {allow: ['http', 'https']});
46+
should.exist(data);
47+
data.should.be.an('object');
48+
data.should.have.property('@context');
49+
} finally {
50+
server.close();
51+
}
52+
});
53+
54+
it('reads JSON-LD from stdin (child process runner)', async () => {
55+
const runner = path.join(__dirname, 'runner-stdin.js');
56+
const child = spawn(
57+
process.execPath,
58+
[runner, '-']
59+
);
60+
61+
let stdout = '';
62+
let stderr = '';
63+
child.stdout.on('data', d => {
64+
stdout += d.toString();
65+
});
66+
child.stderr.on('data', d => {
67+
stderr += d.toString();
68+
});
69+
70+
// write fixture to child's stdin
71+
child.stdin.write(fixtureData);
72+
child.stdin.end();
73+
74+
const exitCode = await new Promise(resolve => child.on('close', resolve));
75+
if(exitCode !== 0) {
76+
throw new Error(`Runner exited with ${exitCode}: ${stderr}`);
77+
}
78+
const parsed = JSON.parse(stdout);
79+
should.exist(parsed);
80+
parsed.should.be.an('object');
81+
parsed.should.have.property('@context');
82+
});
83+
});

test/runner-stdin.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
import {jsonldRequest} from '../lib/index.js';
3+
4+
const loc = process.argv[2] || '-';
5+
const options = {allow: ['stdin']};
6+
7+
jsonldRequest(loc, options).then(({data}) => {
8+
// print only the data as JSON to stdout
9+
console.log(JSON.stringify(data));
10+
}).catch(err => {
11+
// print error to stderr
12+
console.error(err && err.stack ? err.stack : String(err));
13+
process.exit(1);
14+
});

0 commit comments

Comments
 (0)