Skip to content

Commit

Permalink
[console] always set content type to application/json if there is a body
Browse files Browse the repository at this point in the history
  • Loading branch information
jbudz committed Aug 11, 2017
1 parent f8c7d35 commit b7379fa
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 50 deletions.
17 changes: 1 addition & 16 deletions src/core_plugins/console/public/src/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 1 addition & 34 deletions src/core_plugins/console/public/tests/src/content_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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);
});

Expand Down

0 comments on commit b7379fa

Please sign in to comment.