server-tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. var fs = Npm.require('fs');
  2. var temp = Npm.require('temp');
  3. // Automatically track and cleanup files at exit
  4. temp.track();
  5. // Set up HTTP method URL used by client tests
  6. HTTP.methods({
  7. 'test': {
  8. get: function () {
  9. var buf = new Buffer('Hello World');
  10. this.setContentType('text/plain');
  11. return buf;
  12. },
  13. head: function () {
  14. var buf = new Buffer('Hello World');
  15. this.setContentType('text/plain');
  16. this.addHeader('Content-Length', buf.length);
  17. buf = null;
  18. }
  19. }
  20. });
  21. // Save temp file for testing with
  22. function openTempFile(name, callback) {
  23. return temp.open(name, callback);
  24. }
  25. var openTempFileSync = Meteor.wrapAsync(openTempFile);
  26. var info = openTempFileSync(null);
  27. var tempFilePath = info.path;
  28. fs.writeSync(info.fd, 'Hello World');
  29. fs.closeSync(info.fd);
  30. var bufferData;
  31. var arrayBufferData;
  32. var binaryData;
  33. var dataUriData;
  34. var urlData;
  35. var filePathData;
  36. var streamData;
  37. // Init with Buffer
  38. Tinytest.addAsync('cfs-data - server - Init with Buffer', function(test, onComplete) {
  39. bufferData = new DataMan(new Buffer('Hello World'), "text/plain");
  40. test.instanceOf(bufferData.source, DataMan.Buffer);
  41. test.equal(bufferData.type(), "text/plain");
  42. onComplete();
  43. });
  44. // Init with ArrayBuffer
  45. Tinytest.addAsync('cfs-data - server - Init with ArrayBuffer', function(test, onComplete) {
  46. arrayBufferData = new DataMan(str2ab('Hello World'), "text/plain");
  47. // Should be converted upon init to a Buffer
  48. test.instanceOf(arrayBufferData.source, DataMan.Buffer);
  49. test.equal(arrayBufferData.type(), "text/plain");
  50. onComplete();
  51. });
  52. // Init with Binary
  53. Tinytest.addAsync('cfs-data - server - Init with Binary', function(test, onComplete) {
  54. binaryData = new DataMan(new Uint8Array(str2ab('Hello World')), "text/plain");
  55. // Should be converted upon init to a Buffer
  56. test.instanceOf(arrayBufferData.source, DataMan.Buffer);
  57. test.equal(binaryData.type(), "text/plain");
  58. onComplete();
  59. });
  60. // Init with data URI string
  61. Tinytest.addAsync('cfs-data - server - Init with data URI string', function(test, onComplete) {
  62. var dataUri = 'data:text/plain;base64,SGVsbG8gV29ybGQ='; //'Hello World'
  63. dataUriData = new DataMan(dataUri);
  64. // Data URIs are not converted to Buffers upon init
  65. test.instanceOf(dataUriData.source, DataMan.DataURI);
  66. test.equal(dataUriData.type(), "text/plain"); //should be extracted from data URI
  67. onComplete();
  68. });
  69. // Init with URL string
  70. Tinytest.addAsync('cfs-data - server - Init with URL string', function(test, onComplete) {
  71. var url = Meteor.absoluteUrl('test');
  72. urlData = new DataMan(url, "text/plain"); //'Hello World'
  73. // URLs are not converted to Buffers upon init
  74. test.instanceOf(urlData.source, DataMan.URL);
  75. test.equal(urlData.type(), "text/plain");
  76. onComplete();
  77. });
  78. // Init with filepath string
  79. Tinytest.addAsync('cfs-data - server - Init with filepath string', function(test, onComplete) {
  80. filePathData = new DataMan(tempFilePath, "text/plain");
  81. // filepaths are not converted to Buffers upon init
  82. test.instanceOf(filePathData.source, DataMan.FilePath);
  83. test.equal(filePathData.type(), "text/plain");
  84. onComplete();
  85. });
  86. // Init with readable stream
  87. Tinytest.addAsync('cfs-data - server - Init with readable stream', function(test, onComplete) {
  88. streamData = new DataMan(fs.createReadStream(tempFilePath), "text/plain");
  89. // filepaths are not converted to Buffers upon init
  90. test.instanceOf(streamData.source, DataMan.ReadStream);
  91. test.equal(streamData.type(), "text/plain");
  92. onComplete();
  93. });
  94. // getBuffer
  95. Tinytest.addAsync('cfs-data - server - getBuffer', function(test, onComplete) {
  96. var total = 12, done = 0;
  97. function testBuffer(error, buffer, testType) {
  98. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  99. test.instanceOf(buffer, Buffer);
  100. if (buffer instanceof Buffer) {
  101. test.equal(buffer.toString(), 'Hello World', testType + ' got back buffer with incorrect data');
  102. }
  103. done++;
  104. if (total === done) {
  105. onComplete();
  106. }
  107. }
  108. // from Buffer (async)
  109. bufferData.getBuffer(function (error, buffer) {
  110. testBuffer(error, buffer, 'getBuffer from Buffer async');
  111. });
  112. // from Buffer (sync)
  113. testBuffer(null, bufferData.getBuffer(), 'getBuffer from Buffer sync');
  114. // from ArrayBuffer (async)
  115. arrayBufferData.getBuffer(function (error, buffer) {
  116. testBuffer(error, buffer, 'getBuffer from ArrayBuffer async');
  117. });
  118. // from ArrayBuffer (sync)
  119. testBuffer(null, arrayBufferData.getBuffer(), 'getBuffer from ArrayBuffer sync');
  120. // from binary (async)
  121. binaryData.getBuffer(function (error, buffer) {
  122. testBuffer(error, buffer, 'getBuffer from binary async');
  123. });
  124. // from binary (sync)
  125. testBuffer(null, binaryData.getBuffer(), 'getBuffer from binary sync');
  126. // from data URI (async)
  127. dataUriData.getBuffer(function (error, buffer) {
  128. testBuffer(error, buffer, 'getBuffer from data URI async');
  129. });
  130. // from data URI (sync)
  131. testBuffer(null, dataUriData.getBuffer(), 'getBuffer from data URI sync');
  132. // from URL (async)
  133. urlData.getBuffer(function (error, buffer) {
  134. testBuffer(error, buffer, 'getBuffer from URL async');
  135. });
  136. // from URL (sync)
  137. testBuffer(null, urlData.getBuffer(), 'getBuffer from URL sync');
  138. // from filepath (async)
  139. filePathData.getBuffer(function (error, buffer) {
  140. testBuffer(error, buffer, 'getBuffer filepath async');
  141. });
  142. // from filepath (sync)
  143. testBuffer(null, filePathData.getBuffer(), 'getBuffer filepath sync');
  144. });
  145. // getDataUri
  146. Tinytest.addAsync('cfs-data - server - getDataUri', function(test, onComplete) {
  147. var total = 12, done = 0;
  148. function testURI(error, uri, testType) {
  149. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  150. test.equal(typeof uri, "string", testType + ' got no URI string');
  151. test.equal(uri, 'data:text/plain;base64,SGVsbG8gV29ybGQ=', testType + ' got invalid URI');
  152. done++;
  153. if (total === done) {
  154. onComplete();
  155. }
  156. }
  157. // from Buffer (async)
  158. bufferData.getDataUri(function (error, uri) {
  159. testURI(error, uri, 'getDataUri from Buffer async');
  160. });
  161. // from Buffer (sync)
  162. testURI(null, bufferData.getDataUri(), 'getDataUri from Buffer sync');
  163. // from ArrayBuffer (async)
  164. arrayBufferData.getDataUri(function (error, uri) {
  165. testURI(error, uri, 'getDataUri from ArrayBuffer async');
  166. });
  167. // from ArrayBuffer (sync)
  168. testURI(null, arrayBufferData.getDataUri(), 'getDataUri from ArrayBuffer sync');
  169. // from binary (async)
  170. binaryData.getDataUri(function (error, uri) {
  171. testURI(error, uri, 'getDataUri from binary async');
  172. });
  173. // from binary (sync)
  174. testURI(null, binaryData.getDataUri(), 'getDataUri from binary sync');
  175. // from data URI (async)
  176. dataUriData.getDataUri(function (error, uri) {
  177. testURI(error, uri, 'getDataUri from data URI async');
  178. });
  179. // from data URI (sync)
  180. testURI(null, dataUriData.getDataUri(), 'getDataUri from data URI sync');
  181. // from URL (async)
  182. urlData.getDataUri(function (error, uri) {
  183. testURI(error, uri, 'getDataUri from URL async');
  184. });
  185. // from URL (sync)
  186. testURI(null, urlData.getDataUri(), 'getDataUri from URL sync');
  187. // from filepath (async)
  188. filePathData.getDataUri(function (error, uri) {
  189. testURI(error, uri, 'getDataUri filepath async');
  190. });
  191. // from filepath (sync)
  192. testURI(null, filePathData.getDataUri(), 'getDataUri filepath sync');
  193. });
  194. // size
  195. Tinytest.addAsync('cfs-data - server - size', function(test, onComplete) {
  196. var total = 6, done = 0;
  197. function testSize(size, testType) {
  198. test.equal(size, 11, testType + ' got wrong size');
  199. done++;
  200. if (total === done) {
  201. onComplete();
  202. }
  203. }
  204. // from Buffer
  205. testSize(bufferData.size(), 'size from Buffer');
  206. // from ArrayBuffer
  207. testSize(arrayBufferData.size(), 'size from ArrayBuffer');
  208. // from binary
  209. testSize(binaryData.size(), 'size from binary');
  210. // from data URI
  211. testSize(dataUriData.size(), 'size from data URI');
  212. // from URL
  213. testSize(urlData.size(), 'size from URL');
  214. // from filepath
  215. testSize(filePathData.size(), 'size from filepath');
  216. });
  217. // saveToFile
  218. // Since saveToFile uses createReadStream, this tests that function also
  219. Tinytest.addAsync('cfs-data - server - saveToFile', function(test, onComplete) {
  220. var total = 12, done = 0;
  221. function testSave(dataInstance) {
  222. var tempName = temp.path({suffix: '.txt'});
  223. dataInstance.saveToFile(tempName, function (error) {
  224. test.isFalse(!!error);
  225. test.equal(fs.readFileSync(tempName, {encoding: 'utf8'}), 'Hello World', 'file was not saved with correct data');
  226. done++;
  227. if (total === done) {
  228. onComplete();
  229. }
  230. });
  231. }
  232. function testSaveSync(dataInstance) {
  233. var tempName = temp.path({suffix: '.txt'});
  234. dataInstance.saveToFile(tempName);
  235. test.equal(fs.readFileSync(tempName, {encoding: 'utf8'}), 'Hello World', 'file was not saved with correct data');
  236. done++;
  237. if (total === done) {
  238. onComplete();
  239. }
  240. }
  241. // from Buffer
  242. testSave(bufferData);
  243. testSaveSync(bufferData);
  244. // from ArrayBuffer
  245. testSave(arrayBufferData);
  246. testSaveSync(arrayBufferData);
  247. // from binary
  248. testSave(binaryData);
  249. testSaveSync(binaryData);
  250. // from data URI
  251. testSave(dataUriData);
  252. testSaveSync(dataUriData);
  253. // from URL
  254. testSave(urlData);
  255. testSaveSync(urlData);
  256. // from filepath
  257. testSave(filePathData);
  258. testSaveSync(filePathData);
  259. });
  260. // Ensure that URL createReadStream can be piped after delay
  261. // https://github.com/mikeal/request/issues/887
  262. Tinytest.addAsync('cfs-data - server - createReadStream delay', function(test, onComplete) {
  263. var readStream = urlData.createReadStream();
  264. // wait for 5 seconds, then pipe
  265. Meteor.setTimeout(function() {
  266. var tempName = temp.path({suffix: '.txt'});
  267. try {
  268. var writeStream = readStream.pipe(fs.createWriteStream(tempName));
  269. writeStream.on('finish', Meteor.bindEnvironment(function() {
  270. test.equal(fs.readFileSync(tempName, {encoding: 'utf8'}), 'Hello World', 'file was not saved with correct data');
  271. onComplete();
  272. }));
  273. writeStream.on('error', Meteor.bindEnvironment(function(err) {
  274. test.isFalse(!!err);
  275. }));
  276. } catch (err) {
  277. test.isFalse(!!err);
  278. onComplete();
  279. }
  280. }, 5000);
  281. });
  282. //Test API:
  283. //test.isFalse(v, msg)
  284. //test.isTrue(v, msg)
  285. //test.equalactual, expected, message, not
  286. //test.length(obj, len)
  287. //test.include(s, v)
  288. //test.isNaN(v, msg)
  289. //test.isUndefined(v, msg)
  290. //test.isNotNull
  291. //test.isNull
  292. //test.throws(func)
  293. //test.instanceOf(obj, klass)
  294. //test.notEqual(actual, expected, message)
  295. //test.runId()
  296. //test.exception(exception)
  297. //test.expect_fail()
  298. //test.ok(doc)
  299. //test.fail(doc)
  300. //test.equal(a, b, msg)