Skip to content

Commit

Permalink
[console] Replace text/plain fallback with application/json (#12294)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
jbudz committed Aug 18, 2017
1 parent e589ad4 commit b1fe469
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/core_plugins/console/public/src/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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',
Expand Down
26 changes: 26 additions & 0 deletions src/core_plugins/console/public/tests/src/content_type.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions src/core_plugins/console/public/tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit b1fe469

Please sign in to comment.