From 5becf2c98cd29d0c4308e8f9d40db7eaa782e073 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 27 Oct 2016 09:58:01 +0300 Subject: [PATCH] test(cloudcmd) rest: pack --- test/cloudcmd.js | 51 +++++++++++++++++++++++++++++++++++++++ test/fixture/pack | 1 + test/fixture/pack.tar.gz | Bin 0 -> 116 bytes 3 files changed, 52 insertions(+) create mode 100644 test/fixture/pack create mode 100644 test/fixture/pack.tar.gz 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 0000000000000000000000000000000000000000..9cf8925a04562d5ff2d54c7d0793e60caaa7f2f4 GIT binary patch literal 116 zcmb2|=3oE=W^E6jbJw(2Y9uaDGyno)<3NT>ZO@jmoiGuRn57{hB`q;SLTdKx+0vR4 z5>hjyXEQ#{e5oO!p{wVq!(`PORIFeZ*l^jIC1uI8Cr`AeJQshuq=0uzwA(=q3D(64 RA{Y1>7}VH8`51uK0RZ-MBmMvY literal 0 HcmV?d00001