From f8c7d356999eb85e9c2f3253dd93280e13c99746 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Mon, 12 Jun 2017 16:11:50 -0500 Subject: [PATCH 1/3] [console] Always set content-type for requests to es --- src/core_plugins/console/public/src/es.js | 41 +++++++------ .../console/public/tests/src/content_type.js | 59 +++++++++++++++++++ .../console/public/tests/tests.js | 1 + 3 files changed, 82 insertions(+), 19 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..2849ba768db66 100644 --- a/src/core_plugins/console/public/src/es.js +++ b/src/core_plugins/console/public/src/es.js @@ -7,6 +7,26 @@ export function getVersion() { return esVersion; } +export function getContentType(body) { + if (!body) return; + + let contentType; + try { + // there is more than one line + const lines = body.split('\n').filter(Boolean); + + if (lines.length < 2) throw new Error('not multiline json') + + // each line must be valid json + lines.forEach(line => JSON.parse(line)); + contentType = 'application/x-ndjson'; + } + catch (e) { + contentType = 'application/json'; + } + return contentType; +} + export function send(method, path, data) { var wrappedDfd = $.Deferred(); @@ -15,29 +35,12 @@ 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..2eb76b809ce7c --- /dev/null +++ b/src/core_plugins/console/public/tests/src/content_type.js @@ -0,0 +1,59 @@ +import { getContentType } from '../../src/es'; + +const { test, module, equal } = window.QUnit; + +const APPLICATION_NEWLINE_JSON = 'application/x-ndjson'; +const APPLICATION_JSON = 'application/json'; + +module('Content type'); + +test('line delimited json without formatting', () => { + const contentType = getContentType([ + JSON.stringify({ + foo: 'baz' + }), + JSON.stringify({ + foo: 'bar' + }) + ].join('\n')) + + equal(contentType, APPLICATION_NEWLINE_JSON); +}); + +// not to spec, fallback to json +test('line delimited json with formatting', () => { + const contentType = getContentType([ + JSON.stringify({ + foo: 'baz' + }), + JSON.stringify({ + foo: 'bar' + }, null, 2) + ].join('\n')) + + equal(contentType, APPLICATION_JSON); +}); + +test('one line of of json without formatting', () => { + const contentType = getContentType([ + JSON.stringify({ + foo: 'baz' + }) + ].join('\n')) + + equal(contentType, APPLICATION_JSON); +}); + +test('json over multiple lines', () => { + const contentType = getContentType(JSON.stringify({ + foo: 'bar' + }, null, 2)); + + 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'); From b7379fa9e278ec7bce4eb2360a0d425592ca0cc1 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 11 Aug 2017 13:44:36 -0500 Subject: [PATCH 2/3] [console] always set content type to application/json if there is a body --- src/core_plugins/console/public/src/es.js | 17 +-------- .../console/public/tests/src/content_type.js | 35 +------------------ 2 files changed, 2 insertions(+), 50 deletions(-) diff --git a/src/core_plugins/console/public/src/es.js b/src/core_plugins/console/public/src/es.js index 2849ba768db66..c554628a77d34 100644 --- a/src/core_plugins/console/public/src/es.js +++ b/src/core_plugins/console/public/src/es.js @@ -9,22 +9,7 @@ export function getVersion() { export function getContentType(body) { if (!body) return; - - let contentType; - try { - // there is more than one line - const lines = body.split('\n').filter(Boolean); - - if (lines.length < 2) throw new Error('not multiline json') - - // each line must be valid json - lines.forEach(line => JSON.parse(line)); - contentType = 'application/x-ndjson'; - } - catch (e) { - contentType = 'application/json'; - } - return contentType; + return 'application/json'; } export function send(method, path, data) { diff --git a/src/core_plugins/console/public/tests/src/content_type.js b/src/core_plugins/console/public/tests/src/content_type.js index 2eb76b809ce7c..07aa23937a8b4 100644 --- a/src/core_plugins/console/public/tests/src/content_type.js +++ b/src/core_plugins/console/public/tests/src/content_type.js @@ -2,12 +2,11 @@ import { getContentType } from '../../src/es'; const { test, module, equal } = window.QUnit; -const APPLICATION_NEWLINE_JSON = 'application/x-ndjson'; const APPLICATION_JSON = 'application/json'; module('Content type'); -test('line delimited json without formatting', () => { +test('body', () => { const contentType = getContentType([ JSON.stringify({ foo: 'baz' @@ -17,38 +16,6 @@ test('line delimited json without formatting', () => { }) ].join('\n')) - equal(contentType, APPLICATION_NEWLINE_JSON); -}); - -// not to spec, fallback to json -test('line delimited json with formatting', () => { - const contentType = getContentType([ - JSON.stringify({ - foo: 'baz' - }), - JSON.stringify({ - foo: 'bar' - }, null, 2) - ].join('\n')) - - equal(contentType, APPLICATION_JSON); -}); - -test('one line of of json without formatting', () => { - const contentType = getContentType([ - JSON.stringify({ - foo: 'baz' - }) - ].join('\n')) - - equal(contentType, APPLICATION_JSON); -}); - -test('json over multiple lines', () => { - const contentType = getContentType(JSON.stringify({ - foo: 'bar' - }, null, 2)); - equal(contentType, APPLICATION_JSON); }); From 0fcce2f176847f152c12efa6a5976f1fa27ff95b Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 11 Aug 2017 13:46:02 -0500 Subject: [PATCH 3/3] remove extra space --- src/core_plugins/console/public/src/es.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core_plugins/console/public/src/es.js b/src/core_plugins/console/public/src/es.js index c554628a77d34..f032251e45450 100644 --- a/src/core_plugins/console/public/src/es.js +++ b/src/core_plugins/console/public/src/es.js @@ -20,8 +20,6 @@ export function send(method, path, data) { method = "POST"; } - - var options = { url: '../api/console/proxy?' + formatQueryString({ path, method }), data,