Skip to content

Commit

Permalink
fix(cubejs-client-core): Remove Content-Type header from requests in …
Browse files Browse the repository at this point in the history
…HttpTransport (#709)

* fix(cubejs-client-core): Remove Content-Type header from requests in HttpTransport
  • Loading branch information
oguzbilgener committed Jun 10, 2020
1 parent d3c735b commit f6e366c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/cubejs-client-core/src/HttpTransport.js
Expand Up @@ -30,11 +30,12 @@ class HttpTransport {

let spanCounter = 1;

// Currently, all methods make GET requests. If a method makes a request with a body payload,
// remember to add a 'Content-Type' header.
const runRequest = () => fetch(
`${this.apiUrl}/${method}${searchParams.toString().length ? `?${searchParams}` : ''}`, {
headers: {
Authorization: this.authorization,
'Content-Type': 'application/json',
'x-request-id': baseRequestId && `${baseRequestId}-span-${spanCounter++}`,
...this.headers
}
Expand Down
56 changes: 56 additions & 0 deletions packages/cubejs-client-core/src/HttpTransport.test.js
@@ -0,0 +1,56 @@
/* eslint-disable import/first */
/* eslint-disable import/newline-after-import */
/* globals describe,test,expect,jest,afterEach */
import '@babel/runtime/regenerator';
jest.mock('cross-fetch');
import fetch from 'cross-fetch';
import HttpTransport from './HttpTransport';

describe('HttpTransport', () => {
const apiUrl = 'http://localhost:3000/cubejs-api/v1';
const query = {
measures: ['Orders.count'],
dimensions: ['Users.country']
};
const queryUrlEncoded = '%7B%22measures%22%3A%5B%22Orders.count%22%5D%2C%22dimensions%22%3A%5B%22Users.country%22%5D%7D';

afterEach(() => {
fetch.mockClear();
});

test('it serializes the query object and sends it in the query string', async () => {
const transport = new HttpTransport({
authorization: 'token',
apiUrl,
});
const req = transport.request('load', { query });
await req.subscribe(() => { });
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(`${apiUrl}/load?query=${queryUrlEncoded}`, {
headers: {
Authorization: 'token',
}
});
});

test('it passes extra headers and serializes extra params', async () => {
const extraParams = { foo: 'bar' };
const serializedExtraParams = encodeURIComponent(JSON.stringify(extraParams));
const transport = new HttpTransport({
authorization: 'token',
apiUrl,
headers: {
'X-Extra-Header': '42'
}
});
const req = transport.request('meta', { extraParams });
await req.subscribe(() => { });
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(`${apiUrl}/meta?extraParams=${serializedExtraParams}`, {
headers: {
Authorization: 'token',
'X-Extra-Header': '42'
}
});
});
});

0 comments on commit f6e366c

Please sign in to comment.