diff --git a/test/cloudcmd.js b/test/cloudcmd.js index 4cbc1e7c..a2da439e 100644 --- a/test/cloudcmd.js +++ b/test/cloudcmd.js @@ -1,4 +1,6 @@ +const assert = require('assert'); const http = require('http'); +const fs = require('fs'); const test = require('tape'); const express = require('express'); @@ -10,6 +12,8 @@ const success = (fn) => (...args) => fn(null, ...args); const freeport = promisify(require('freeport')); const getBody = promisify(pipe.getBody); +const getBuffer = promisify(_getBuffer); + const get = promisify((url, fn) => { http.get(url, success(fn)); }); @@ -57,3 +61,50 @@ test('cloudcmd: rest: fs: path', (t) => { }); }); +test('cloudcmd: rest: pack', (t) => { + before((port, after) => { + console.log(port); + get(`http://${host}:${port}/api/v1/pack/fixture/pack`) + .then(wrap(getBuffer)) + .then((pack) => { + const fixture = fs.readFileSync(__dirname + '/fixture/pack.tar.gz'); + t.ok(fixture.compare(pack), 'should pack data'); + t.end(); + after(); + }) + .catch((error) => { + console.log(error); + }); + }); +}); + +/** + * get body of readStream + * + * @param readStream + * @param callback + */ +function _getBuffer(readStream, callback) { + var error, + body = []; + + assert(readStream, 'could not be empty!'); + assert(callback, 'could not be empty!'); + + readStream.on('data', onData); + readStream.on('error', onEnd); + readStream.on('end', onEnd); + + function onData(chunk) { + body.push(chunk); + } + + function onEnd(error) { + readStream.removeListener('data', onData); + readStream.removeListener('error', onEnd); + readStream.removeListener('end', onEnd); + + callback(error, Buffer.from(body)); + } +} + diff --git a/test/fixture/pack b/test/fixture/pack new file mode 100644 index 00000000..3b18e512 --- /dev/null +++ b/test/fixture/pack @@ -0,0 +1 @@ +hello world diff --git a/test/fixture/pack.tar.gz b/test/fixture/pack.tar.gz new file mode 100644 index 00000000..9cf8925a Binary files /dev/null and b/test/fixture/pack.tar.gz differ