kibana/test/utils/kbn_server.js
Spencer 9ed7e81b62 Backport eslint updates to 5.x (#10139)
* [eslint] update eslint config to 0.3.0

* [eslint] autofix

* [fixtures/hits] reformat to comply with max-len lint rule

* [eslint] enable no-var and autofix

* [eslint] enable prefer-const and autofix

* [eslint] fix autofix-incompatible no-var and prefer-const violations

* [eslint] enable quotes+no-extra-semi and autofix
2017-02-06 21:27:07 -07:00

63 lines
1.8 KiB
JavaScript

import url from 'url';
import { defaultsDeep, set } from 'lodash';
import { header as basicAuthHeader } from './base_auth';
import { kibanaUser, kibanaServer } from '../shield';
import KbnServer from '../../src/server/kbn_server';
import serverConfig from '../server_config';
const SERVER_DEFAULTS = {
server: {
autoListen: false,
xsrf: {
disableProtection: true
}
},
logging: {
quiet: true
},
plugins: {},
optimize: {
enabled: false
},
elasticsearch: {
url: url.format(serverConfig.servers.elasticsearch),
username: kibanaServer.username,
password: kibanaServer.password
}
};
/**
* Creates an instance of KbnServer with default configuration
* tailored for unit tests
*
* @param {object} params Any config overrides for this instance
*/
export function createServer(params = {}) {
params = defaultsDeep({}, params, SERVER_DEFAULTS);
return new KbnServer(params);
}
/**
* Creates request configuration with a basic auth header
*/
export function authOptions() {
const { username, password } = kibanaUser;
const authHeader = basicAuthHeader(username, password);
return set({}, 'headers.Authorization', authHeader);
}
/**
* Makes a request with test headers via hapi server inject()
*
* The given options are decorated with default testing options, so it's
* recommended to use this function instead of using inject() directly whenever
* possible throughout the tests.
*
* @param {KbnServer} kbnServer
* @param {object} options Any additional options or overrides for inject()
* @param {Function} fn The callback to pass as the second arg to inject()
*/
export function makeRequest(kbnServer, options, fn) {
options = defaultsDeep({}, authOptions(), options);
return kbnServer.server.inject(options, fn);
}