Skip to content

Commit

Permalink
add axios testing
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Jan 13, 2024
1 parent 2c2da6f commit d9256b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
20 changes: 6 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const axios = require('axios')

function streamToString (stream) {
const chunks = [];
Expand Down Expand Up @@ -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)

Check warning on line 68 in index.js

View workflow job for this annotation

GitHub Actions / lint / lint

'response' is assigned a value but never used
}
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');
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
16 changes: 15 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
})
})

Expand Down

0 comments on commit d9256b5

Please sign in to comment.