|
| 1 | +var { expect } = require("chai"); |
| 2 | +const { defaultImportReader: reader } = require("../src/reader"); |
| 3 | + |
| 4 | +const helloworldContent = "吾有一言。曰「「問天地好在。」」。書之。"; |
| 5 | + |
| 6 | +describe("reader", () => { |
| 7 | + describe("local", () => { |
| 8 | + it("import local files", async () => { |
| 9 | + const content = await reader("helloworld", "./examples"); |
| 10 | + |
| 11 | + expect(content).eq(helloworldContent); |
| 12 | + }); |
| 13 | + |
| 14 | + it("search for files", async () => { |
| 15 | + const content = await reader("helloworld", [ |
| 16 | + "./some/invalid/dir", |
| 17 | + "./lib", |
| 18 | + "./examples" |
| 19 | + ]); |
| 20 | + |
| 21 | + expect(content).eq(helloworldContent); |
| 22 | + }); |
| 23 | + |
| 24 | + it("not found", async () => { |
| 25 | + try { |
| 26 | + await reader("not_exists", "./examples"); |
| 27 | + } catch (e) { |
| 28 | + expect(e).to.be.an.instanceof(ReferenceError); |
| 29 | + } |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("http", () => { |
| 34 | + it("block http imports by default", async () => { |
| 35 | + try { |
| 36 | + await reader( |
| 37 | + "helloworld", |
| 38 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/" |
| 39 | + ); |
| 40 | + } catch (e) { |
| 41 | + expect(e).to.be.an.instanceof(URIError); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it("load http contents", async () => { |
| 46 | + const content = await reader( |
| 47 | + "helloworld", |
| 48 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/", |
| 49 | + { |
| 50 | + allowHttp: true |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + expect(content).eq(helloworldContent); |
| 55 | + }); |
| 56 | + |
| 57 | + it("load http contents in trusted hosts", async () => { |
| 58 | + const content = await reader( |
| 59 | + "helloworld", |
| 60 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/", |
| 61 | + { |
| 62 | + trustedHosts: [ |
| 63 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples" |
| 64 | + ] |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + expect(content).eq(helloworldContent); |
| 69 | + }); |
| 70 | + |
| 71 | + it("search for http contents", async () => { |
| 72 | + const content = await reader( |
| 73 | + "helloworld", |
| 74 | + [ |
| 75 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/lib/", |
| 76 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/" |
| 77 | + ], |
| 78 | + { |
| 79 | + trustedHosts: [ |
| 80 | + "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master" |
| 81 | + ] |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + expect(content).eq(helloworldContent); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments