request.post(
test('should send 400 in case of invalid JSON', async t => { const url = await getUrl() const params = { resolveWithFullResponse: true } try { await request.post(`${url}/`, params) } catch (e) { t.is(e.statusCode, 400) } })
reply(msg) { // https://core.telegram.org/bots/api#sendmessage request.post(this._telegramApiUrl + '/sendMessage', { json: msg }, function (error, response, body) { if (error) { console.error('Error while /sendMessage', error); return; } if (response.statusCode != 200) { console.error('Error status code while /sendMessage', body); return; } console.log('Method /sendMessage succeeded'); }); }
test('should send an empty response in case of succes when storing log and should invoke broadcast.setState() once', async t => { const feed = utils.getMockedFeed() const broadcast = utils.getMockedBroadcast() const url = await getUrl(feed, broadcast) const payload = { event: 'hello' } const params = { resolveWithFullResponse: true, body: payload, json: true } const res = await request.post(`${url}/`, params) t.is(td.explain(broadcast.setState).callCount, 1) t.is(res.statusCode, 204) t.falsy(res.body) })
test('should send 500 in case of failure when storing log', async t => { const feed = utils.getMockedFeedThatFailsOnAppend() const url = await getUrl(feed) const params = { resolveWithFullResponse: true, body: { event: 'hello' }, json: true } try { await request.post(`${url}/`, params) } catch (e) { t.is(e.statusCode, 500) } })
reply(replyToken, messages) { return new Promise((resolve, reject) => { request.post("https://api.line.me/v2/bot/message/reply", { forever: true, headers: { 'Authorization': `Bearer ${this.botConfig.channelAccessToken}` }, json: { replyToken: replyToken, messages: messages } }, (error, response, body) => { if (error) { this.logError('Error while sending message', error); reject(error); return; } if (response.statusCode !== 200) { this.logError('Error status code while sending message', body); reject(error); return; } this.log('Send message succeeded'); resolve(body); }); }); }
function sendPostToSlack(media) { let caption; if (media.caption === null) { caption = 'Unknown'; } else { caption = media.caption.text; } request.post( slackURL, { json: { attachments: [ { pretext: caption, image_url: media.images.standard_resolution.url, footer: `Instagram ${media.type} liked`, footer_icon: 'https://lh3.googleusercontent.com/aYbdIM1abwyVSUZLDKoE0CDZGRhlkpsaPOg9tNnBktUQYsXflwknnOn2Ge1Yr7rImGk=w300', ts: Math.floor(Date.now() / 1000), }, ], }, }, (error, response) => response, ); }
test('should send 403 when POSTING from a not allowed origin and origin is not specified in headers', async t => { const url = await getUrl( utils.getMockedFeed(), utils.getMockedBroadcast(), utils.getMockedSse(), utils.getMockedStatsDb(), 'another origin' ) try { await request.post(`${url}/`, { resolveWithFullResponse: true }) } catch (e) { t.is(e.statusCode, 403) } })
replyWithData(roomId, messageData) { let msg = messageData; msg.roomId = roomId; return new Promise((resolve, reject) => { request.post("https://api.ciscospark.com/v1/messages", { auth: { bearer: this._botConfig.sparkToken }, forever: true, json: msg }, (err, resp, body) => { if (err) { console.error('Error while reply:', err); reject('Error while reply: ' + err.message); } else if (resp.statusCode != 200) { console.log('Error while reply:', resp.statusCode, body); reject('Error while reply: ' + body); } else { console.log("reply answer body", body); resolve(body); } }); }); }
request.post(
request.post(
request.post(
test('should send 500 in case of missing "event" param', async t => { const feed = utils.getMockedFeedThatFailsOnAppend() const url = await getUrl(feed) const params = { resolveWithFullResponse: true, body: { aaaa: 'hello' }, json: true } try { await request.post(`${url}/`, params) } catch (e) { t.is(e.statusCode, 400) } })
test('should send 403 when POSTING from a not allowed origin', async t => { const url = await getUrl( utils.getMockedFeed(), utils.getMockedBroadcast(), utils.getMockedSse(), utils.getMockedStatsDb(), 'another origin' ) try { await request.post(`${url}/`, { resolveWithFullResponse: true, headers: { origin: 'another origin' } }) } catch (e) { t.is(e.statusCode, 403) } })
reply(replyToken, messages) { return new Promise((resolve, reject) => { request.post("https://api.line.me/v2/bot/message/reply", { forever: true, headers: { 'Authorization': `Bearer ${this.botConfig.channelAccessToken}` }, json: { replyToken: replyToken, messages: messages } }, (error, response, body) => { if (error) { this.logError('Error while sending message', error); reject(error); return; } if (response.statusCode !== 200) { this.logError('Error status code while sending message', body); reject(error); return; } this.log('Send message succeeded'); resolve(body); }); }); }