From 4051117ed023c4e59c073091679ffe684be99599 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 11 Dec 2017 18:24:22 +0300 Subject: [PATCH] src: Minor reorganisation, fixes --- doc/usage.md | 14 ++++--- test/telegram.js | 28 +------------- ...t.sendfile.js => test.format-send-data.js} | 37 +++++++++++++++++-- 3 files changed, 44 insertions(+), 35 deletions(-) rename test/{test.sendfile.js => test.format-send-data.js} (74%) diff --git a/doc/usage.md b/doc/usage.md index d6247b1..657846b 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -117,16 +117,16 @@ bot.sendPhoto(chatId, url); If you wish to explicitly specify the filename or [MIME type](http://en.wikipedia.org/wiki/Internet_media_type), -you may pass the an additional argument as file options, like so: +you may pass an additional argument as file options, like so: ```js -const fileOpts = { +const fileOptions = { // Explicitly specify the file name. filename: 'customfilename', // Explicitly specify the MIME type. - contentType: 'audio/mpeg' + contentType: 'audio/mpeg', }; -bot.sendAudio(chatId, data, {}, fileOpts); +bot.sendAudio(chatId, data, {}, fileOptions); ``` @@ -140,6 +140,10 @@ variable `NTBA_FIX_350`.** In order of highest-to-lowest precedence in searching for a value, when resolving the `filename`: +*(`fileOptions` is the Object argument passed to the method. +The "file" argument passed to the method can be a `Stream`, +`Buffer` or `filepath`.)* + 1. Is `fileOptions.filename` explictly defined? 1. Does `Stream#path` exist? 1. Is `filepath` provided? @@ -152,7 +156,7 @@ And the `contentType`: 1. Try detecting file-type from the `Buffer` 1. Is `filepath` provided? 1. Is `fileOptions.filename` explicitly defined? -1. Default to `"application/octet-stream` +1. Default to `"application/octet-stream"` ### Performance Issue diff --git a/test/telegram.js b/test/telegram.js index c0501cf..b69727e 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -1332,33 +1332,7 @@ describe('TelegramBot', function telegramSuite() { }); describe('#_formatSendData', function _formatSendDataSuite() { - it('should handle buffer path from fs.readStream', function test() { - let photo; - try { - photo = fs.createReadStream(Buffer.from(`${__dirname}/data/photo.gif`)); - } catch (ex) { - // Older Node.js versions do not support passing a Buffer - // representation of the path to fs.createReadStream() - if (ex instanceof TypeError) return Promise.resolve(); - } - return bot.sendPhoto(USERID, photo).then(resp => { - assert.ok(is.object(resp)); - assert.ok(is.array(resp.photo)); - }); - }); - it('should not accept file-paths if disallowed with constructor option', function test() { - const tgbot = new TelegramBot(TOKEN, { filepath: false }); - const photo = `${__dirname}/data/photo.gif`; - return tgbot.sendPhoto(USERID, photo).catch(err => { - // TODO: check for error in a better way - assert.ok(err.response.body.description.indexOf('Bad Request') !== -1); - }); - }); - it('should allow stream.path that can not be parsed', function test() { - const stream = fs.createReadStream(`${__dirname}/data/photo.gif`); - stream.path = '/?id=123'; // for example, 'http://example.com/?id=666' - return bot.sendPhoto(USERID, stream); - }); + }); describe('#sendInvoice', function sendInvoiceSuite() { diff --git a/test/test.sendfile.js b/test/test.format-send-data.js similarity index 74% rename from test/test.sendfile.js rename to test/test.format-send-data.js index 7b69db7..9782842 100644 --- a/test/test.sendfile.js +++ b/test/test.format-send-data.js @@ -8,8 +8,9 @@ const paths = { }; -describe('sending files', function sendfileSuite() { - const bot = new TelegramBot('token '); +describe('#_formatSendData', function sendfileSuite() { + const bot = new TelegramBot('token'); + const type = 'file'; before(function beforeSuite() { process.env.NTBA_FIX_350 = 1; @@ -19,7 +20,6 @@ describe('sending files', function sendfileSuite() { }); describe('using fileOptions', function sendfileOptionsSuite() { - const type = 'file'; const stream = fs.createReadStream(paths.audio); const nonPathStream = fs.createReadStream(paths.audio); const buffer = fs.readFileSync(paths.audio); @@ -105,4 +105,35 @@ describe('sending files', function sendfileSuite() { }); }); }); + + it('should handle buffer path from fs.readStream', function test() { + let file; + try { + file = fs.createReadStream(Buffer.from(paths.audio)); + } catch (ex) { + // Older Node.js versions do not support passing a Buffer + // representation of the path to fs.createReadStream() + if (ex instanceof TypeError) { + Promise.resolve(); + return; + } + } + const [{ [type]: data }] = bot._formatSendData('file', file); + assert.equal(data.options.filename, path.basename(paths.audio)); + }); + + it('should not accept file-paths if disallowed with constructor option', function test() { + const tgbot = new TelegramBot('token', { filepath: false }); + const [formData, fileId] = tgbot._formatSendData('file', paths.audio); + assert.ok(fileId); + assert.ok(!formData); + }); + + it('should allow stream.path that can not be parsed', function test() { + const stream = fs.createReadStream(paths.audio); + stream.path = '/?id=123'; // for example, 'http://example.com/?id=666' + assert.doesNotThrow(function assertDoesNotThrow() { + bot._formatSendData('file', stream); + }); + }); });