|
| 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 | +}); |
0 commit comments