test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Module dependencies.
  3. */
  4. var assert = require('assert');
  5. var dataUriToBuffer = require('../');
  6. describe('data-uri-to-buffer', function () {
  7. it('should decode bare-bones Data URIs', function () {
  8. var uri = 'data:,Hello%2C%20World!';
  9. var buf = dataUriToBuffer(uri);
  10. assert.equal('text/plain', buf.type);
  11. assert.equal('text/plain;charset=US-ASCII', buf.typeFull);
  12. assert.equal('US-ASCII', buf.charset);
  13. assert.equal('Hello, World!', buf.toString());
  14. });
  15. it('should decode bare-bones "base64" Data URIs', function () {
  16. var uri = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D';
  17. var buf = dataUriToBuffer(uri);
  18. assert.equal('text/plain', buf.type);
  19. assert.equal('Hello, World!', buf.toString());
  20. });
  21. it('should decode plain-text Data URIs', function () {
  22. var html = '<!DOCTYPE html>'+
  23. '<html lang="en">'+
  24. '<head><title>Embedded Window</title></head>'+
  25. '<body><h1>42</h1></body>'+
  26. '</html>';
  27. // Escape the HTML for URL formatting
  28. var uri = 'data:text/html;charset=utf-8,' + encodeURIComponent(html);
  29. var buf = dataUriToBuffer(uri);
  30. assert.equal('text/html', buf.type);
  31. assert.equal('utf-8', buf.charset);
  32. assert.equal(html, buf.toString());
  33. });
  34. // the next 4 tests are from:
  35. // https://bug161965.bugzilla.mozilla.org/attachment.cgi?id=94670&action=view
  36. it('should decode "ISO-8859-8 in Base64" URIs', function () {
  37. var uri = 'data:text/plain;charset=iso-8859-8-i;base64,+ezl7Q==';
  38. var buf = dataUriToBuffer(uri);
  39. assert.equal('text/plain', buf.type);
  40. assert.equal('iso-8859-8-i', buf.charset);
  41. assert.equal(4, buf.length);
  42. assert.equal(0xf9, buf[0]);
  43. assert.equal(0xec, buf[1]);
  44. assert.equal(0xe5, buf[2]);
  45. assert.equal(0xed, buf[3]);
  46. });
  47. it('should decode "ISO-8859-8 in URL-encoding" URIs', function () {
  48. var uri = 'data:text/plain;charset=iso-8859-8-i,%f9%ec%e5%ed';
  49. var buf = dataUriToBuffer(uri);
  50. assert.equal('text/plain', buf.type);
  51. assert.equal('iso-8859-8-i', buf.charset);
  52. assert.equal(4, buf.length);
  53. assert.equal(0xf9, buf[0]);
  54. assert.equal(0xec, buf[1]);
  55. assert.equal(0xe5, buf[2]);
  56. assert.equal(0xed, buf[3]);
  57. });
  58. it('should decode "UTF-8 in Base64" URIs', function () {
  59. var uri = 'data:text/plain;charset=UTF-8;base64,16nXnNeV150=';
  60. var buf = dataUriToBuffer(uri);
  61. assert.equal('text/plain', buf.type);
  62. assert.equal('UTF-8', buf.charset);
  63. assert.equal(8, buf.length);
  64. assert.equal('שלום', buf.toString('utf8'));
  65. });
  66. it('should decode "UTF-8 in URL-encoding" URIs', function () {
  67. var uri = 'data:text/plain;charset=UTF-8,%d7%a9%d7%9c%d7%95%d7%9d';
  68. var buf = dataUriToBuffer(uri);
  69. assert.equal('text/plain', buf.type);
  70. assert.equal('UTF-8', buf.charset);
  71. assert.equal(8, buf.length);
  72. assert.equal('שלום', buf.toString('utf8'));
  73. });
  74. // this next one is from Wikipedia IIRC
  75. it('should decode "base64" Data URIs with newlines', function () {
  76. var uri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA\n' +
  77. 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO\n' +
  78. '9TXL0Y4OHwAAAABJRU5ErkJggg==';
  79. var buf = dataUriToBuffer(uri);
  80. assert.equal('image/png', buf.type);
  81. assert.equal('iVBORw0KGgoAAAANSUhEUgAAAAUA' +
  82. 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
  83. '9TXL0Y4OHwAAAABJRU5ErkJggg==', buf.toString('base64'));
  84. });
  85. it('should decode a plain-text URI with a space character in it', function () {
  86. var uri = 'data:,foo bar';
  87. var buf = dataUriToBuffer(uri);
  88. assert.equal('text/plain', buf.type);
  89. assert.equal('foo bar', buf.toString());
  90. });
  91. it('should decode data with a "," comma char', function () {
  92. var uri = 'data:,a,b';
  93. var buf = dataUriToBuffer(uri);
  94. assert.equal('text/plain', buf.type);
  95. assert.equal('a,b', buf.toString());
  96. });
  97. it('should decode data with traditionally reserved characters like ";"', function () {
  98. var uri = 'data:,;test';
  99. var buf = dataUriToBuffer(uri);
  100. assert.equal('text/plain', buf.type);
  101. assert.equal(';test', buf.toString());
  102. });
  103. it('should not default to US-ASCII if main type is provided', function () {
  104. var uri = 'data:text/plain,abc';
  105. var buf = dataUriToBuffer(uri);
  106. assert.equal('text/plain', buf.type);
  107. assert.equal('text/plain', buf.typeFull);
  108. assert.equal('', buf.charset);
  109. assert.equal('abc', buf.toString());
  110. });
  111. it('should default to text/plain if main type is not provided', function () {
  112. var uri = 'data:;charset=UTF-8,abc';
  113. var buf = dataUriToBuffer(uri);
  114. assert.equal('text/plain', buf.type);
  115. assert.equal('text/plain;charset=UTF-8', buf.typeFull);
  116. assert.equal('UTF-8', buf.charset);
  117. assert.equal('abc', buf.toString());
  118. });
  119. it('should not allow charset without a leading ;', function () {
  120. var uri = 'data:charset=UTF-8,abc';
  121. var buf = dataUriToBuffer(uri);
  122. assert.equal('charset=UTF-8', buf.type);
  123. assert.equal('charset=UTF-8', buf.typeFull);
  124. assert.equal('', buf.charset);
  125. assert.equal('abc', buf.toString());
  126. });
  127. it('should allow custom media type parameters', function () {
  128. var uri = 'data:application/javascript;version=1.8;charset=UTF-8,abc';
  129. var buf = dataUriToBuffer(uri);
  130. assert.equal('application/javascript', buf.type);
  131. assert.equal('application/javascript;version=1.8;charset=UTF-8', buf.typeFull);
  132. assert.equal('UTF-8', buf.charset);
  133. assert.equal('abc', buf.toString());
  134. });
  135. it('should allow base64 annotation anywhere', function () {
  136. var uri = 'data:text/plain;base64;charset=UTF-8,YWJj';
  137. var buf = dataUriToBuffer(uri);
  138. assert.equal('text/plain', buf.type);
  139. assert.equal('text/plain;charset=UTF-8', buf.typeFull);
  140. assert.equal('UTF-8', buf.charset);
  141. assert.equal('abc', buf.toString());
  142. });
  143. });