Working gateway
Change-Id: I8ca690fe9d1b7f8e20b438df1ddd48d6b2f99326
diff --git a/spec/core_proxy.spec.js b/spec/core_proxy.spec.js
new file mode 100644
index 0000000..c5bb9ad
--- /dev/null
+++ b/spec/core_proxy.spec.js
@@ -0,0 +1,183 @@
+(function () {
+ 'use strict';
+
+ const chai = require('chai');
+ const expect = chai.expect;
+ const sinonChai = require('sinon-chai');
+ const supertest = require('supertest');
+ const mockery = require('mockery');
+ chai.use(sinonChai);
+
+ const request = require('superagent');
+ const stub = require('./spec_helper.js');
+
+ const configMock = {
+ xos: {
+ host: 'http://test-xos-url',
+ port: '80'
+ }
+ };
+
+ let app;
+
+ describe('The core proxy routes', () => {
+
+ // stub for GET method
+ stub.makeStub('getSuccess', request, 'get', cb => {
+ cb(null, {
+ status: 200,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for POST method
+ stub.makeStub('postSuccess', request, 'post', cb => {
+ cb(null, {
+ status: 201,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for PUT method
+ stub.makeStub('putSuccess', request, 'put', cb => {
+ cb(null, {
+ status: 200,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for DELETE method
+ stub.makeStub('deleteSuccess', request, 'delete', cb => {
+ cb(null, {
+ status: 204
+ });
+ });
+
+ // mocking the config.rest module
+ before(() => {
+ mockery.enable({
+ warnOnReplace: true,
+ warnOnUnregistered: false
+ });
+ mockery.registerMock('../config/config.js', configMock);
+
+ app = require('../src/server.js').app;
+ });
+
+ after(() => {
+ mockery.deregisterMock('../config/config.js');
+ mockery.disable();
+ });
+
+ it('should read XOS address from config.rest module', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/')
+ .end((err) => {
+ if (err) return done(err);
+ expect(myStub.get.called).to.be.true;
+ expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/');
+ done();
+ });
+ });
+
+ it('should pass token and cookies along with the request', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err) {
+ if (err) return done(err);
+ expect(myStub.set.getCall(0)).to.have.been.calledWith('x-csrftoken', 'testToken');
+ expect(myStub.set.getCall(1)).to.have.been.calledWith('cookie', 'testCookie');
+ done();
+ });
+ });
+
+ it('should pass query paramenters along with the request', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/instances/?no_hyperlink=1&node=1')
+ .end((err) => {
+ if (err) return done(err);
+ expect(myStub.get.called).to.be.true;
+ expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/instances/?no_hyperlink=1&node=1');
+ done();
+ });
+ });
+
+ it('should proxy GET request to XOS', (done) => {
+ supertest(app)
+ .get('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(res.status).to.equal(200);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy POST request to XOS', (done) => {
+
+ const myStub = stub.getStub('postSuccess');
+
+ supertest(app)
+ .post('/api/core/')
+ .send({foo: 'bar'})
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+ expect(res.status).to.equal(201);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy PUT request to XOS', (done) => {
+
+ const myStub = stub.getStub('putSuccess');
+
+ supertest(app)
+ .put('/api/core/')
+ .send({foo: 'bar'})
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+ expect(res.status).to.equal(200);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy DELETE request to XOS', (done) => {
+
+ const myStub = stub.getStub('deleteSuccess');
+
+ supertest(app)
+ .delete('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send).not.to.have.been.called;
+ expect(res.status).to.equal(204);
+ done();
+ });
+ });
+ });
+})();
\ No newline at end of file