1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const assert = require('assert');
const { Before } = require('@cucumber/cucumber');
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
// This sets the mock adapter on the default instance
const mockAxios = new MockAdapter(axios);
const { fixtures } = require('../../fixtures/server');
const mockValidConfig = fixtures.get('config-valid');
const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = fixtures.get('schema-swagger');
const { GenericProtocol } = require('../../../dist/protocol');
const { Adapter } = require('../../../dist/adapter');
/**
* Use assert.CallTracker to track internal method calls
* Instead of adding trackers to each method, create a
* single tracked stub function and then use the parameters
* to record what is being tracked.
*/
const tracker = new assert.CallTracker();
const trackedFunction = function(method, params) {
// do nothing;
};
const recorder = tracker.calls(trackedFunction);
class TrackedAdapter extends Adapter {
setupCallTracking(recorder, tracker) {
this.recorder = recorder;
this.tracker = tracker;
}
getAuthorizationHeader() {
this.recorder('getAuthorizationHeader');
return super.getAuthorizationHeader();
}
poll(is_retry) {
this.recorder('poll', { is_retry });
return super.poll(is_retry);
}
publish(topic, body, is_retry) {
this.recorder('publish', { topic, body, is_retry });
return super.publish(topic, body, is_retry);
}
broadcast(body, is_retry) {
this.recorder('broadcast', { body, is_retry });
return super.broadcast(body, is_retry);
}
getTrackedCalls(method) {
let calls = this.tracker.getCalls(this.recorder);
let methodCalls = calls.filter(call => call.arguments[0] === method);
return methodCalls;
}
resetTracker() {
this.tracker.reset();
}
}
class TrackedGenericProtocol extends GenericProtocol {
setupCallTracking(recorder, tracker) {
this.recorder = recorder;
this.tracker = tracker;
}
encode(type, message) {
this.recorder('encode', {type, message});
return super.encode(type, message)
}
decode(type, message) {
this.recorder('decode', {type, message});
return super.decode(type, message)
}
validate(message) {
this.recorder('validate', {message});
return super.validate(message);
}
getTrackedCalls(method) {
let calls = this.tracker.getCalls(this.recorder);
let methodCalls = calls.filter(call => call.arguments[0] === method);
return methodCalls;
}
resetTracker() {
this.tracker.reset();
}
}
Before(function() {
this.api = mockValidConfig.api;
this.schema = mockSchema;
this.tracker = tracker;
this.recorder = recorder;
let services = {
recorder,
tracker
};
this.classes = {
TrackedAdapter,
TrackedGenericProtocol,
};
this.callCounts = {};
this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services);
this.protocol.setupCallTracking(this.recorder, this.tracker);
this.protocol.resetTracker();
this.mockAxios = mockAxios;
this.mockAxios.reset();
this.mockAxios.resetHistory();
this.mockAxios.onGet(
`${mockValidConfig.api}/token`,
{ params: { client_id: mockValidConfig.client_id, secret: mockValidConfig.secret } }
).reply(200, fixtures.get('response-valid-token'));
this.mockAxios.onGet(
`${mockInvalidConfig.api}/token`,
{ params: { client_id: mockInvalidConfig.client_id, secret: mockInvalidConfig.secret } }
).reply(403, fixtures.get('response-denied-token'));
});