From d9256b535420359a4d6cf54efbe6d0e5b834225c Mon Sep 17 00:00:00 2001 From: noogen Date: Sat, 13 Jan 2024 02:13:57 -0600 Subject: [PATCH] add axios testing --- index.js | 20 ++++++-------------- package.json | 7 ++++++- test/index.js | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 3f2353f..b1dcdc8 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict' +const axios = require('axios') function streamToString (stream) { const chunks = []; @@ -60,25 +61,16 @@ exports.hook_queue = async function (next, connection) { } const options = { - method: "POST", - headers: customHeaders, - body: postData + headers: customHeaders } try { - const response = await fetch(url, options) - const text = await response.text(); - - // network error in the 4xx–5xx range - // ok is in the range of 200-299 - if (! response.ok) { - plugin.logdebug(text); - throw new Error(`${response.status} ${response.statusText}`); - } - + const response = await axios.post(url, data, options) } catch (err) { - plugin.logdebug(err); + if (err.response) { + plugin.logdebug(JSON.encode(err.response)); + } // blackhole this message as deny return next(DENYSOFT, '458 – Unable to queue messages for node resque'); diff --git a/package.json b/package.json index 11da94e..31e6224 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,14 @@ }, "homepage": "https://github.com/niiknow/haraka-plugin-resque#readme", "devDependencies": { + "chai": "^5.0.0", "eslint": "^8.55.0", "eslint-plugin-haraka": "*", "haraka-test-fixtures": "*", - "mocha": "^10.2.0" + "mocha": "^10.2.0", + "nock": "^13.4.0" + }, + "dependencies": { + "axios": "^1.6.5" } } diff --git a/test/index.js b/test/index.js index fe24f94..cdcbb28 100644 --- a/test/index.js +++ b/test/index.js @@ -4,10 +4,15 @@ const assert = require('assert') // npm modules const fixtures = require('haraka-test-fixtures') +const nock = require('nock') +const axios = require('axios') // start of tests // assert: https://nodejs.org/api/assert.html // mocha: http://mochajs.org +const host = 'http://localhost'; +axios.defaults.host = host; +axios.defaults.baseURL = host; beforeEach(function (done) { this.plugin = new fixtures.plugin('resque') @@ -17,7 +22,16 @@ beforeEach(function (done) { describe('resque', function () { it('loads', function (done) { assert.ok(this.plugin) - done() + nock(host) + .get('/test') + .reply(200, 'test data'); + + axios.get('/test').then(response => { + assert.ok(response.data == 'test data'); + // console.log(response.data); + done(); + }); + // done() }) })