files.tests.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var path = require("path");
  2. var fs = require("fs");
  3. var assert = require("assert");
  4. var promises = require("../../lib/promises");
  5. var Files = require("../../lib/docx/files").Files;
  6. var uriToPath = require("../../lib/docx/files").uriToPath;
  7. var testing = require("../testing");
  8. var test = require("../test")(module);
  9. var readFile = promises.promisify(fs.readFile.bind(fs));
  10. test("Files", {
  11. "can open files with file URI": function() {
  12. var filePath = path.resolve(testing.testPath("tiny-picture.png"));
  13. var files = new Files(null);
  14. return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) {
  15. return readFile(filePath, "base64").then(function(expectedContents) {
  16. assert.deepEqual(contents, expectedContents);
  17. });
  18. });
  19. },
  20. "can open files with relative URI": function() {
  21. var filePath = path.resolve(testing.testPath("tiny-picture.png"));
  22. var files = new Files(testing.testPath("."));
  23. return files.read("tiny-picture.png", "base64").then(function(contents) {
  24. return readFile(filePath, "base64").then(function(expectedContents) {
  25. assert.deepEqual(contents, expectedContents);
  26. });
  27. });
  28. },
  29. "given base is not set when opening relative uri then error is raised": function() {
  30. var files = new Files(null);
  31. return assertError(files.read("not-a-real-file.png", "base64"), function(err) {
  32. assert.equal(err.message, "could not find external image 'not-a-real-file.png', path of input document is unknown");
  33. });
  34. },
  35. "error if relative uri cannot be opened": function() {
  36. var files = new Files("/tmp");
  37. return assertError(files.read("not-a-real-file.png", "base64"), function(err) {
  38. assertRegex(err.message, /could not open external image: 'not-a-real-file.png' \(document directory: '\/tmp'\)\nENOENT.*\/tmp\/not-a-real-file.png.*/);
  39. });
  40. }
  41. });
  42. function assertError(promise, func) {
  43. return promise.then(function() {
  44. assert(false, "Expected error");
  45. }, func);
  46. }
  47. function assertRegex(actual, expected) {
  48. assert.ok(expected.test(actual), "Expected regex: " + expected + "\nbut was: " + actual);
  49. }
  50. test("uriToPath", {
  51. "leading slash is retained on non-Windows file URIs": function() {
  52. assert.equal(uriToPath("file:///a/b/c", "linux"), "/a/b/c");
  53. assert.equal(uriToPath("file:///a/b/c", "win32"), "/a/b/c");
  54. },
  55. "URI is unquoted": function() {
  56. assert.equal(uriToPath("file:///a%20b"), "/a b");
  57. },
  58. "when host is set to localhost then path can be found": function() {
  59. assert.equal(uriToPath("file://localhost/a/b/c"), "/a/b/c");
  60. },
  61. "when host is set but not localhost then path cannot be found": function() {
  62. assert.throws(function() {
  63. uriToPath("file://example/a/b/c");
  64. }, /Could not convert URI to path: file:\/\/example\/a\/b\/c/);
  65. },
  66. "leading slash is not dropped on Windows file URIs when platform is not Windows": function() {
  67. assert.equal(uriToPath("file:///c:/a", "linux"), "/c:/a");
  68. },
  69. "leading slash is dropped on Windows file URIs when platform is Windows": function() {
  70. assert.equal(uriToPath("file:///c:/a", "win32"), "c:/a");
  71. assert.equal(uriToPath("file:///C:/a", "win32"), "C:/a");
  72. },
  73. "relative URI is unquoted": function() {
  74. assert.equal(uriToPath("a%20b/c"), "a b/c");
  75. }
  76. });