UNPKG

1.47 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;
24
25/**
26 * Create a client that can have the client config set instead of fetched.
27 * @param {Object} config the config object
28 * @return {MockConfigClient} client
29 */
30exports.createMockConfigClient = function (config) {
31 return new MockConfigClient(config);
32};
33
34/**
35 * Mock request object
36 * @param {Object} body the request body
37 * @param {Object} headers the request headers
38 */
39function Request(body, headers) {
40 this.headers = headers || {};
41 this.body = body || {};
42}
43exports.Request = Request;
44
45/**
46 * Mock response object to capture response details.
47 */
48function Response() {
49}
50exports.Response = Response;
51
52Response.prototype.writeHead = function (code, headers) {
53 this.code = code;
54 this.headers = headers;
55};
56Response.prototype.write = function (str) {
57 this.body = str;
58};
59Response.prototype.end = function () {
60 this.ended = true;
61};
62Response.prototype.wasUnauthorized = function () {
63 return this.code === 401 && this.ended;
64};