Merge branch 'v1.3-dev' as v1.3.11
[websub-hub] / test-e2e / fake-servers-client.js
1 'use strict';
2
3 /**
4 * Wrapper interface for controlling fake-servers.
5 */
6
7 const axios = require('axios');
8
9 class FakeClient {
10 constructor(host, subscriberPort, topicPort) {
11 this.logger = console;
12 this.host = host;
13 this.subscriberPort = subscriberPort;
14 this.topicPort = topicPort;
15 this.axios = axios.create({
16 validateStatus: (statusCode) => (Math.floor(statusCode / 100)) === 2,
17 headers: {
18 'User-Agent': 'FakeClient',
19 },
20 });
21 }
22
23 topicUrl(id) {
24 return `http://${this.host}:${this.topicPort}/topic/${id}`;
25 }
26
27 subscriberUrl(id, extra = '') {
28 return `http://${this.host}:${this.subscriberPort}/subscriber/${id}${extra}`;
29 }
30
31 static _axiosRequestConfig(method, url, params = {}, headers = {}, data) {
32 const urlObj = new URL(url);
33 const config = {
34 method,
35 url: `${urlObj.origin}${urlObj.pathname}`,
36 params: urlObj.searchParams,
37 headers,
38 ...(data && { data }),
39 responseType: 'text',
40 transformResponse: [ (res) => res ],
41 };
42 Object.entries(params).map(([k, v]) => config.params.set(k, v));
43 return config;
44 }
45
46 async subscribe(hubUrl, subscriberId, topicId, postData = {}) {
47 const topicUrl = this.topicUrl(topicId);
48 const subscriberUrl = this.subscriberUrl(subscriberId);
49 const data = {
50 'hub.callback': subscriberUrl,
51 'hub.mode': 'subscribe',
52 'hub.topic': topicUrl,
53 'hub.lease_seconds': 60,
54 'hub.secret': 'sharedSecret',
55 ...postData,
56 };
57 const formData = new URLSearchParams(data).toString();
58 const headers = {
59 'Content-Type': 'application/x-www-form-urlencoded',
60 };
61
62 try {
63 return this.axios(FakeClient._axiosRequestConfig('POST', hubUrl, {}, headers, formData));
64 } catch (e) {
65 this.logger.error('subscribe', e);
66 throw e;
67 }
68 }
69
70 /**
71 * Set the behavior for a topic id.
72 * @param {String} 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
78 */
79 async topicSet(id, behavior = {}) {
80 const defaultBehavior = {
81 statusCode: 200,
82 content: 'some content',
83 contentType: 'text/plain',
84 };
85 const url = this.topicUrl(id);
86 try {
87 return this.axios(FakeClient._axiosRequestConfig('PUT', url, {
88 ...defaultBehavior,
89 ...behavior,
90 }));
91 } catch (e) {
92 this.logger.error('topicSet', e);
93 throw e;
94 }
95 }
96
97 /**
98 * Remove a topic id.
99 * @param {String} id
100 */
101 async topicDelete(id) {
102 const url =this.topicUrl(id);
103 try {
104 return this.axios(FakeClient._axiosRequestConfig('DELETE', url));
105 } catch (e) {
106 this.logger.error('topicDelete', e);
107 throw e;
108 }
109 }
110
111 /**
112 * Set the behavior for a subscriber id verify response.
113 * @param {String} id
114 * @param {Object} behavior
115 * @param {Number} behavior.statusCode
116 * @param {Boolean} behavior.matchChallenge
117 */
118 async subscriberSetVerify(id, behavior = {}) {
119 const defaultBehavior = {
120 statusCode: 200,
121 matchChallenge: true,
122 };
123 const url = this.subscriberUrl(id, '/verify');
124 try {
125 return this.axios(FakeClient._axiosRequestConfig('PUT', url, {
126 ...defaultBehavior,
127 ...behavior,
128 }));
129 } catch (e) {
130 this.logger.error('subscriberSetVerify', e);
131 throw e;
132 }
133 }
134
135 /**
136 * Set the behavior for a subscriber id content-update response.
137 * @param {String} id
138 * @param {Object} behavior
139 * @param {Number} behavior.statusCode
140 */
141 async subscriberSetContent(id, behavior = {}) {
142 const defaultBehavior = {
143 statusCode: 200,
144 };
145 const url = this.subscriberUrl(id, '/content');
146 try {
147 return this.axios(FakeClient._axiosRequestConfig('PUT', url, {
148 ...defaultBehavior,
149 ...behavior,
150 }));
151 } catch (e) {
152 this.logger.error('subscriberSetContent', e);
153 throw e;
154 }
155 }
156
157 /**
158 * Removes a topic id.
159 * @param {String} id
160 */
161 async subscriberDelete(id) {
162 const url = this.subscriberUrl(id);
163 try {
164 return this.axios(FakeClient._axiosRequestConfig('DELETE', url));
165 } catch (e) {
166 this.logger.error('subscriberDelete', e);
167 throw e;
168 }
169 }
170
171 } // FakeClient
172
173 module.exports = FakeClient;