|
| 1 | +import { expect } from 'chai' |
| 2 | +import get from '../../lib/webid/lib/get.mjs' |
| 3 | + |
| 4 | +describe('webid get()', () => { |
| 5 | + const originalFetch = global.fetch |
| 6 | + const originalTlsSetting = process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 7 | + |
| 8 | + function callGet (webid) { |
| 9 | + return new Promise((resolve, reject) => { |
| 10 | + get(webid, (err, body, contentType) => { |
| 11 | + if (err) { |
| 12 | + reject(err) |
| 13 | + return |
| 14 | + } |
| 15 | + resolve({ body, contentType }) |
| 16 | + }) |
| 17 | + }) |
| 18 | + } |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + global.fetch = originalFetch |
| 22 | + if (originalTlsSetting === undefined) { |
| 23 | + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 24 | + } else { |
| 25 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalTlsSetting |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + it('uses an insecure dispatcher for https fetches when TLS verification is disabled', async () => { |
| 30 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
| 31 | + |
| 32 | + global.fetch = async (url, options) => { |
| 33 | + expect(url).to.equal('https://example.com/profile/card#me') |
| 34 | + expect(options.method).to.equal('GET') |
| 35 | + expect(options.headers.Accept).to.equal('text/turtle, application/ld+json') |
| 36 | + expect(options.dispatcher).to.exist |
| 37 | + |
| 38 | + return { |
| 39 | + ok: true, |
| 40 | + headers: { |
| 41 | + get: () => 'text/turtle' |
| 42 | + }, |
| 43 | + text: async () => '@prefix ex: <http://example.com/> .' |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const { body, contentType } = await callGet('https://example.com/profile/card#me') |
| 48 | + expect(contentType).to.equal('text/turtle') |
| 49 | + expect(body).to.include('@prefix ex:') |
| 50 | + }) |
| 51 | + |
| 52 | + it('does not use an insecure dispatcher when TLS verification is enabled', async () => { |
| 53 | + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 54 | + |
| 55 | + global.fetch = async (url, options) => { |
| 56 | + expect(options.dispatcher).to.equal(undefined) |
| 57 | + |
| 58 | + return { |
| 59 | + ok: true, |
| 60 | + headers: { |
| 61 | + get: () => 'text/turtle' |
| 62 | + }, |
| 63 | + text: async () => 'ok' |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const { body } = await callGet('https://example.com/profile/card#me') |
| 68 | + expect(body).to.equal('ok') |
| 69 | + }) |
| 70 | +}) |
0 commit comments