4 * Wrapper interface for controlling fake-servers.
8 constructor(host
, subscriberPort
, topicPort
) {
11 this.subscriberPort
= subscriberPort
;
12 this.topicPort
= topicPort
;
14 this.got
= this._init
.bind(this);
17 async
_init(...args
) {
19 // eslint-disable-next-line
20 this.Got
= await
import('got');
21 this.got
= this.Got
.got
.extend({
23 'User-Agent': 'FakeClient',
29 return this.got(...args
);
34 return `http://${this.host}:${this.topicPort}/topic/${id}`;
37 subscriberUrl(id
, extra
= '') {
38 return `http://${this.host}:${this.subscriberPort}/subscriber/${id}${extra}`;
41 static _requestConfig(method
, url
, params
= {}, headers
= {}, body
= undefined) {
46 ...(body
&& { body
}),
48 Object
.entries(params
).forEach(([k
, v
]) => gotConfig
.url
.searchParams
.set(k
, v
));
52 static _formData(obj
) {
53 return Object
.entries(obj
)
54 .map((entry
) => entry
.map(encodeURIComponent
).join('='))
59 async
subscribe(hubUrl
, subscriberId
, topicId
, postData
= {}) {
60 const topicUrl
= this.topicUrl(topicId
);
61 const subscriberUrl
= this.subscriberUrl(subscriberId
);
62 const data
= FakeClient
._formData({
63 'hub.callback': subscriberUrl
,
64 'hub.mode': 'subscribe',
65 'hub.topic': topicUrl
,
66 'hub.lease_seconds': 60,
67 'hub.secret': 'sharedSecret',
70 const config
= FakeClient
._requestConfig('POST', hubUrl
, {}, {
71 'Content-Type': 'application/x-www-form-urlencoded',
74 return await
this.got(config
);
76 this.logger
.error('subscribe', error
, config
);
82 * Set the behavior for a topic id.
84 * @param {Object} behavior
85 * @param {Number} behavior.statusCode
86 * @param {String} behavior.content
87 * @param {String} behavior.contentType
88 * @param {String} behavior.hubUrl
90 async
topicSet(id
, behavior
= {}) {
91 const defaultBehavior
= {
93 content: 'some content',
94 contentType: 'text/plain',
96 const url
= this.topicUrl(id
);
97 const config
= FakeClient
._requestConfig('PUT', url
, {
102 return await
this.got(config
);
104 this.logger
.error('topicSet', error
, config
);
113 async
topicDelete(id
) {
114 const url
= this.topicUrl(id
);
115 const config
= FakeClient
._requestConfig('DELETE', url
);
117 return await
this.got(config
);
119 this.logger
.error('topicDelete', error
, config
);
125 * Set the behavior for a subscriber id verify response.
127 * @param {Object} behavior
128 * @param {Number} behavior.statusCode
129 * @param {Boolean} behavior.matchChallenge
131 async
subscriberSetVerify(id
, behavior
= {}) {
132 const defaultBehavior
= {
134 matchChallenge: true,
136 const url
= this.subscriberUrl(id
, '/verify');
137 const config
= FakeClient
._requestConfig('PUT', url
, {
142 return await
this.got(config
);
144 this.logger
.error('subscriberSetVerify', error
, config
);
150 * Set the behavior for a subscriber id content-update response.
152 * @param {Object} behavior
153 * @param {Number} behavior.statusCode
155 async
subscriberSetContent(id
, behavior
= {}) {
156 const defaultBehavior
= {
159 const url
= this.subscriberUrl(id
, '/content');
160 const config
= FakeClient
._requestConfig('PUT', url
, {
165 return await
this.got(config
);
167 this.logger
.error('subscriberSetContent', error
, config
);
173 * Removes a topic id.
176 async
subscriberDelete(id
) {
177 const url
= this.subscriberUrl(id
);
178 const config
= FakeClient
._requestConfig('DELETE', url
);
180 return await
this.got(config
);
182 this.logger
.error('subscriberDelete', error
, config
);
189 module
.exports
= FakeClient
;