I have created this simple class:
import https from 'https';
class Request {
constructor(opts) {
this.hostname = opts.hostname;
}
init(method, path, data = {}) {
const optsReq = {
hostname: this.hostname,
path: path,
method: method
};
return new Promise((resolve, reject) => {
const req = https.request(optsReq, (response) => resolve(response));
req.on('error', (error) => {
console.log(error);
reject(error);
});
req.end(JSON.stringify(data));
});
}
get(path) {
return this.init('GET', path);
}
post(path, data) {
return this.init('POST', path, data);
}
put(path) {
return this.init('PUT', path);
}
delete(path) {
return this.init('DELETE', path);
}
patch(path) {
return this.init('PATCH', path);
}
}
export default Request;
and then I'm trying to test it:
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import Request from '../src/request';
chai.use(chaiAsPromised);
describe('Request', () => {
const hostname = 'fake-server.com';
let server;
before(() => {
server = sinon.fakeServer.create();
server.respondWith(
'GET',
`https://${hostname}/api/entities`,
[ 200, { 'Content-type': 'application/json' }, JSON.stringify({ entities: 1 })]
);
});
it('Receive HTTP 200 status code from a GET request', () => {
const opts = { hostname };
const req = new Request(opts).get('/api/entities');
return expect(req).to.eventually.have.property('entities');
});
after(() => {
server.restore();
});
});
unfortunately whatever hostname I use I always get the following error:
[Error: getaddrinfo ENOTFOUND fake-server.com fake-server.com:443]
Any idea?
Aucun commentaire:
Enregistrer un commentaire