From 61c3f38d32eb9ebffe97b5daec3983fcce640f40 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 18 Aug 2017 10:01:52 -0500 Subject: [PATCH] [console] Replace text/plain fallback with application/json (#12294) * [console] Always set content-type for requests to es * [console] always set content type to application/json if there is a body * remove extra space --- src/core_plugins/console/public/src/es.js | 26 +++++-------------- .../console/public/tests/src/content_type.js | 26 +++++++++++++++++++ .../console/public/tests/tests.js | 1 + 3 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/core_plugins/console/public/tests/src/content_type.js diff --git a/src/core_plugins/console/public/src/es.js b/src/core_plugins/console/public/src/es.js index 1e7c84008291d..f032251e45450 100644 --- a/src/core_plugins/console/public/src/es.js +++ b/src/core_plugins/console/public/src/es.js @@ -7,6 +7,11 @@ export function getVersion() { return esVersion; } +export function getContentType(body) { + if (!body) return; + return 'application/json'; +} + export function send(method, path, data) { var wrappedDfd = $.Deferred(); @@ -15,29 +20,10 @@ export function send(method, path, data) { method = "POST"; } - let contentType; - if (data) { - try { - JSON.parse(data); - contentType = 'application/json'; - } - catch (e) { - try { - data.split('\n').forEach(line => { - if (!line) return; - JSON.parse(line); - }); - contentType = 'application/x-ndjson'; - } catch (e){ - contentType = 'text/plain'; - } - } - } - var options = { url: '../api/console/proxy?' + formatQueryString({ path, method }), data, - contentType, + contentType: getContentType(data), cache: false, crossDomain: true, type: 'POST', diff --git a/src/core_plugins/console/public/tests/src/content_type.js b/src/core_plugins/console/public/tests/src/content_type.js new file mode 100644 index 0000000000000..07aa23937a8b4 --- /dev/null +++ b/src/core_plugins/console/public/tests/src/content_type.js @@ -0,0 +1,26 @@ +import { getContentType } from '../../src/es'; + +const { test, module, equal } = window.QUnit; + +const APPLICATION_JSON = 'application/json'; + +module('Content type'); + +test('body', () => { + const contentType = getContentType([ + JSON.stringify({ + foo: 'baz' + }), + JSON.stringify({ + foo: 'bar' + }) + ].join('\n')) + + equal(contentType, APPLICATION_JSON); +}); + +test('no body', () => { + const contentType = getContentType(''); + + equal(contentType, undefined); +}); diff --git a/src/core_plugins/console/public/tests/tests.js b/src/core_plugins/console/public/tests/tests.js index e2daf02804a14..3bd6cec320222 100644 --- a/src/core_plugins/console/public/tests/tests.js +++ b/src/core_plugins/console/public/tests/tests.js @@ -18,6 +18,7 @@ require('ui/chrome') QUnit.config.autostart = false; QUnit.init(); + require('./src/content_type.js'); require('./src/utils_tests.js'); require('./src/url_autocomplete_tests.js'); require('./src/url_params_tests.js');