Skip to content

Commit

Permalink
Added support for steps
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-ed-raffalli committed Aug 20, 2018
1 parent 3b479a8 commit a36bce5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,38 @@ class Request {
}

static process(app, definition, config) {
if (definition.steps) {
return definition.steps.reduce((previousRequest, stepDef, index) => {
return previousRequest.then(response => {
debug(`Processing step ${index}`);
stepDef = typeof stepDef === 'function' ? stepDef(response) : stepDef;

return Request.processRequestDefinition(app, {
...definition,
...stepDef
}, config);
});


}, Promise.resolve());
}

return Request.processRequestDefinition(app, definition, config);
}

static processRequestDefinition(app, definition, config) {
const auth = Request.getAuth(definition.auth);
definition = {...definition, auth};

if (!auth) {
if (!definition.auth) {
debug('No auth defined');
return Request.processRequest(app, definition, config);
}

if (Array.isArray(auth)) {
debug(`Auth defined with ${auth.length} users`);
if (Array.isArray(definition.auth)) {
debug(`Auth defined with ${definition.auth.length} users`);
return Promise.all(
auth.map(auth =>
definition.auth.map(auth =>
Request.processAuthenticatedRequest(app, {
...definition,
auth
Expand Down
72 changes: 72 additions & 0 deletions src/Request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,78 @@ describe('Request', () => {

describe('process', () => {

beforeEach(() => {
def = {
url: 'http://127.0.0.1/some/url'
};
sinon.stub(Request, 'processRequestDefinition').resolves();
});

afterEach(() => {
Request.processRequestDefinition.restore();
});

it('calls processRequestDefinition with object definition', () => {
return Request.process('app', def, config)
.then(() => {
expect(Request.processRequestDefinition.calledOnce).to.be.true;
expect(Request.processRequestDefinition.calledWithMatch('app', def, config)).to.be.true;
});
});

it('calls processRequestDefinition with merged definition for each step', () => {
const defSteps = {...def, steps: [{order: 1}, {order: 2}]};

return Request.process('app', defSteps, config)
.then(() => {

expect(Request.processRequestDefinition.calledTwice).to.be.true;

expect(Request.processRequestDefinition.firstCall.calledWithMatch('app', {
...def,
order: 1
}, config)).to.be.true;
expect(Request.processRequestDefinition.secondCall.calledWithMatch('app', {
...def,
order: 2
}, config)).to.be.true;
});
});

it('calls processRequestDefinition with definition as callback', () => {
Request.processRequestDefinition
.onFirstCall().resolves('resp-1')
.onSecondCall().resolves('resp-2');

const step1 = {order: 1},
step2 = {order: 2},
step1Stub = sinon.stub().returns(step1),
step2Stub = sinon.stub().returns(step2),
defSteps = {...def, steps: [step1Stub, step2Stub]};

return Request.process('app', defSteps, config)
.then(() => {

expect(Request.processRequestDefinition.calledTwice).to.be.true;

expect(Request.processRequestDefinition.firstCall.calledWithMatch('app', {
...def,
order: 1
}, config)).to.be.true;
expect(Request.processRequestDefinition.secondCall.calledWithMatch('app', {
...def,
order: 2
}, config)).to.be.true;

expect(step1Stub.calledWithExactly(undefined)).to.be.true;
expect(step2Stub.calledWithExactly('resp-1')).to.be.true;
});
});

});

describe('processRequestDefinition', () => {

const authArr = [
'tokenStub',
{foo: 'bar'}
Expand Down

0 comments on commit a36bce5

Please sign in to comment.