UNPKG

1.6 kBJavaScriptView Raw
1'use strict';
2
3const util = require('util');
4const PubSub = require('../');
5
6exports.createMockClient = function (config, sendCallback) {
7 config = config || {};
8 let client = new PubSub(config);
9 client._send = function () {
10 sendCallback && sendCallback.apply(null, arguments);
11 };
12 return client;
13};
14
15/**
16 * Mock client for avoiding config fetch
17 * @class
18 */
19function MockConfigClient() {
20 return PubSub.apply(this, arguments);
21}
22util.inherits(MockConfigClient, PubSub);
23MockConfigClient.prototype.fetchConfig = () => null;
24MockConfigClient.prototype.updateConfig = function (config) {
25 this._parseConfig(Object.assign(this.config || {}, config));
26};
27
28/**
29 * Create a client that can have the client config set instead of fetched.
30 * @param {Object} config the config object
31 * @return {MockConfigClient} client
32 */
33exports.createMockConfigClient = function (config) {
34 return new MockConfigClient(config);
35};
36
37/**
38 * Mock request object
39 * @param {Object} body the request body
40 * @param {Object} headers the request headers
41 */
42function Request(body, headers) {
43 this.headers = headers || {};
44 this.body = body || {};
45}
46exports.Request = Request;
47
48/**
49 * Mock response object to capture response details.
50 */
51function Response() {
52}
53exports.Response = Response;
54
55Response.prototype.writeHead = function (code, headers) {
56 this.code = code;
57 this.headers = headers;
58};
59Response.prototype.write = function (str) {
60 this.body = str;
61};
62Response.prototype.end = function () {
63 this.ended = true;
64};
65Response.prototype.wasUnauthorized = function () {
66 return this.code === 401 && this.ended;
67};