4 * Wrapper interface for controlling fake-servers.
7 const axios
= require('axios');
10 constructor(host
, subscriberPort
, topicPort
) {
11 this.logger
= console
;
13 this.subscriberPort
= subscriberPort
;
14 this.topicPort
= topicPort
;
15 this.axios
= axios
.create({
16 validateStatus: (statusCode
) => (Math
.floor(statusCode
/ 100)) === 2,
18 'User-Agent': 'FakeClient',
24 return `http://${this.host}:${this.topicPort}/topic/${id}`;
27 subscriberUrl(id
, extra
= '') {
28 return `http://${this.host}:${this.subscriberPort}/subscriber/${id}${extra}`;
31 static _axiosRequestConfig(method
, url
, params
= {}, headers
= {}, data
) {
32 const urlObj
= new URL(url
);
35 url: `${urlObj.origin}${urlObj.pathname}`,
36 params: urlObj
.searchParams
,
38 ...(data
&& { data
}),
40 transformResponse: [ (res
) => res
],
42 Object
.entries(params
).map(([k
, v
]) => config
.params
.set(k
, v
));
46 async
subscribe(hubUrl
, subscriberId
, topicId
, postData
= {}) {
47 const topicUrl
= this.topicUrl(topicId
);
48 const subscriberUrl
= this.subscriberUrl(subscriberId
);
50 'hub.callback': subscriberUrl
,
51 'hub.mode': 'subscribe',
52 'hub.topic': topicUrl
,
53 'hub.lease_seconds': 60,
54 'hub.secret': 'sharedSecret',
57 const formData
= new URLSearchParams(data
).toString();
59 'Content-Type': 'application/x-www-form-urlencoded',
63 return this.axios(FakeClient
._axiosRequestConfig('POST', hubUrl
, {}, headers
, formData
));
65 this.logger
.error('subscribe', e
);
71 * Set the behavior for a topic id.
73 * @param {Object} behavior
74 * @param {Number} behavior.statusCode
75 * @param {String} behavior.content
76 * @param {String} behavior.contentType
77 * @param {String} behavior.hubUrl
79 async
topicSet(id
, behavior
= {}) {
80 const defaultBehavior
= {
82 content: 'some content',
83 contentType: 'text/plain',
85 const url
= this.topicUrl(id
);
87 return this.axios(FakeClient
._axiosRequestConfig('PUT', url
, {
92 this.logger
.error('topicSet', e
);
101 async
topicDelete(id
) {
102 const url
=this.topicUrl(id
);
104 return this.axios(FakeClient
._axiosRequestConfig('DELETE', url
));
106 this.logger
.error('topicDelete', e
);
112 * Set the behavior for a subscriber id verify response.
114 * @param {Object} behavior
115 * @param {Number} behavior.statusCode
116 * @param {Boolean} behavior.matchChallenge
118 async
subscriberSetVerify(id
, behavior
= {}) {
119 const defaultBehavior
= {
121 matchChallenge: true,
123 const url
= this.subscriberUrl(id
, '/verify');
125 return this.axios(FakeClient
._axiosRequestConfig('PUT', url
, {
130 this.logger
.error('subscriberSetVerify', e
);
136 * Set the behavior for a subscriber id content-update response.
138 * @param {Object} behavior
139 * @param {Number} behavior.statusCode
141 async
subscriberSetContent(id
, behavior
= {}) {
142 const defaultBehavior
= {
145 const url
= this.subscriberUrl(id
, '/content');
147 return this.axios(FakeClient
._axiosRequestConfig('PUT', url
, {
152 this.logger
.error('subscriberSetContent', e
);
158 * Removes a topic id.
161 async
subscriberDelete(id
) {
162 const url
= this.subscriberUrl(id
);
164 return this.axios(FakeClient
._axiosRequestConfig('DELETE', url
));
166 this.logger
.error('subscriberDelete', e
);
173 module
.exports
= FakeClient
;