2018-09-07 21:36:47 -04:00
|
|
|
import fetchMock from 'fetch-mock';
|
|
|
|
|
2018-11-29 12:48:18 -05:00
|
|
|
import PublicAPI, { SupersetClient, ClientConfig } from '../src/SupersetClient';
|
2018-09-07 21:36:47 -04:00
|
|
|
import throwIfCalled from './utils/throwIfCalled';
|
|
|
|
import { LOGIN_GLOB } from './fixtures/constants';
|
|
|
|
|
|
|
|
describe('SupersetClient', () => {
|
|
|
|
beforeAll(() => {
|
2018-09-18 20:30:01 -04:00
|
|
|
fetchMock.get(LOGIN_GLOB, { csrf_token: '' });
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(fetchMock.restore);
|
|
|
|
|
|
|
|
afterEach(PublicAPI.reset);
|
|
|
|
|
|
|
|
describe('Public API', () => {
|
|
|
|
it('exposes reset, configure, init, get, post, isAuthenticated, and reAuthenticate methods', () => {
|
|
|
|
expect(PublicAPI.configure).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.init).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.get).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.post).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.isAuthenticated).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.reAuthenticate).toEqual(expect.any(Function));
|
|
|
|
expect(PublicAPI.reset).toEqual(expect.any(Function));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws if you call init, get, post, isAuthenticated, or reAuthenticate before configure', () => {
|
|
|
|
expect(PublicAPI.init).toThrow();
|
|
|
|
expect(PublicAPI.get).toThrow();
|
|
|
|
expect(PublicAPI.post).toThrow();
|
|
|
|
expect(PublicAPI.isAuthenticated).toThrow();
|
|
|
|
expect(PublicAPI.reAuthenticate).toThrow();
|
|
|
|
|
|
|
|
expect(PublicAPI.configure).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
// this also tests that the ^above doesn't throw if configure is called appropriately
|
|
|
|
it('calls appropriate SupersetClient methods when configured', () => {
|
|
|
|
const mockGetUrl = '/mock/get/url';
|
|
|
|
const mockPostUrl = '/mock/post/url';
|
|
|
|
const mockGetPayload = { get: 'payload' };
|
|
|
|
const mockPostPayload = { post: 'payload' };
|
|
|
|
fetchMock.get(mockGetUrl, mockGetPayload);
|
|
|
|
fetchMock.post(mockPostUrl, mockPostPayload);
|
|
|
|
|
|
|
|
const initSpy = jest.spyOn(SupersetClient.prototype, 'init');
|
|
|
|
const getSpy = jest.spyOn(SupersetClient.prototype, 'get');
|
|
|
|
const postSpy = jest.spyOn(SupersetClient.prototype, 'post');
|
|
|
|
const authenticatedSpy = jest.spyOn(SupersetClient.prototype, 'isAuthenticated');
|
|
|
|
const csrfSpy = jest.spyOn(SupersetClient.prototype, 'getCSRFToken');
|
|
|
|
|
|
|
|
PublicAPI.configure({});
|
|
|
|
PublicAPI.init();
|
2018-11-29 12:48:18 -05:00
|
|
|
|
2018-10-18 20:53:05 -04:00
|
|
|
expect(initSpy).toHaveBeenCalledTimes(1);
|
2018-11-29 12:48:18 -05:00
|
|
|
expect(authenticatedSpy).toHaveBeenCalledTimes(1);
|
2018-09-07 21:36:47 -04:00
|
|
|
expect(csrfSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
PublicAPI.get({ url: mockGetUrl });
|
|
|
|
PublicAPI.post({ url: mockPostUrl });
|
|
|
|
PublicAPI.isAuthenticated();
|
2018-11-29 12:48:18 -05:00
|
|
|
PublicAPI.reAuthenticate();
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-10-18 20:53:05 -04:00
|
|
|
expect(initSpy).toHaveBeenCalledTimes(2);
|
2018-09-07 21:36:47 -04:00
|
|
|
expect(getSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(postSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(csrfSpy).toHaveBeenCalledTimes(2); // from init() + reAuthenticate()
|
|
|
|
|
|
|
|
initSpy.mockRestore();
|
|
|
|
getSpy.mockRestore();
|
|
|
|
postSpy.mockRestore();
|
|
|
|
authenticatedSpy.mockRestore();
|
|
|
|
csrfSpy.mockRestore();
|
|
|
|
|
|
|
|
fetchMock.reset();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SupersetClient', () => {
|
2018-11-29 12:48:18 -05:00
|
|
|
describe('getUrl', () => {
|
|
|
|
let client;
|
|
|
|
beforeEach(() => {
|
|
|
|
client = new SupersetClient({ protocol: 'https:', host: 'CONFIG_HOST' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses url if passed', () => {
|
|
|
|
expect(client.getUrl({ url: 'myUrl', endpoint: 'blah', host: 'blah' })).toBe('myUrl');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('constructs a valid url from config.protocol + host + endpoint if passed', () => {
|
|
|
|
expect(client.getUrl({ endpoint: '/test', host: 'myhost' })).toBe('https://myhost/test');
|
|
|
|
expect(client.getUrl({ endpoint: '/test', host: 'myhost/' })).toBe('https://myhost/test');
|
|
|
|
expect(client.getUrl({ endpoint: 'test', host: 'myhost' })).toBe('https://myhost/test');
|
|
|
|
expect(client.getUrl({ endpoint: '/test/test//', host: 'myhost/' })).toBe(
|
|
|
|
'https://myhost/test/test//',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('constructs a valid url from config.host + endpoint if host is omitted', () => {
|
|
|
|
expect(client.getUrl({ endpoint: '/test' })).toBe('https://CONFIG_HOST/test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not throw if url, endpoint, and host are', () => {
|
|
|
|
client = new SupersetClient({ protocol: 'https:', host: '' });
|
|
|
|
expect(client.getUrl()).toBe('https:///');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-07 21:36:47 -04:00
|
|
|
describe('CSRF', () => {
|
|
|
|
afterEach(fetchMock.reset);
|
|
|
|
|
2018-10-18 20:53:05 -04:00
|
|
|
it('calls superset/csrf_token/ when init() is called if no CSRF token is passed', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() => {
|
|
|
|
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(1);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-10-18 20:53:05 -04:00
|
|
|
it('does NOT call superset/csrf_token/ when init() is called if a CSRF token is passed', () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
const client = new SupersetClient({ csrfToken: 'abc' });
|
|
|
|
|
|
|
|
return client.init().then(() => {
|
|
|
|
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(0);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls superset/csrf_token/ when init(force=true) is called even if a CSRF token is passed', () => {
|
|
|
|
expect.assertions(4);
|
|
|
|
const initialToken = 'inital_token';
|
|
|
|
const client = new SupersetClient({ csrfToken: initialToken });
|
|
|
|
|
|
|
|
return client.init().then(() => {
|
|
|
|
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(0);
|
|
|
|
expect(client.csrfToken).toBe(initialToken);
|
|
|
|
|
|
|
|
return client.init(true).then(() => {
|
|
|
|
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(1);
|
|
|
|
expect(client.csrfToken).not.toBe(initialToken);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('isAuthenticated() returns true if there is a token and false if not', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(2);
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
expect(client.isAuthenticated()).toBe(false);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() => {
|
|
|
|
expect(client.isAuthenticated()).toBe(true);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-10-18 20:53:05 -04:00
|
|
|
it('isAuthenticated() returns true if a token is passed at configuration', () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
const clientWithoutToken = new SupersetClient({ csrfToken: null });
|
|
|
|
const clientWithToken = new SupersetClient({ csrfToken: 'token' });
|
|
|
|
|
|
|
|
expect(clientWithoutToken.isAuthenticated()).toBe(false);
|
|
|
|
expect(clientWithToken.isAuthenticated()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('init() throws if superset/csrf_token/ returns an error', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
|
|
|
|
fetchMock.get(LOGIN_GLOB, () => Promise.reject({ status: 403 }), {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.init()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(error => {
|
|
|
|
expect(error.status).toBe(403);
|
|
|
|
|
|
|
|
// reset
|
|
|
|
fetchMock.get(
|
|
|
|
LOGIN_GLOB,
|
2018-10-18 20:53:05 -04:00
|
|
|
{ csrf_token: '1234' },
|
2018-09-07 21:36:47 -04:00
|
|
|
{
|
|
|
|
overwriteRoutes: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('init() throws if superset/csrf_token/ does not return a token', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
fetchMock.get(LOGIN_GLOB, {}, { overwriteRoutes: true });
|
|
|
|
|
|
|
|
const client = new SupersetClient({});
|
2018-09-18 00:53:14 -04:00
|
|
|
|
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.init()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
|
|
|
|
// reset
|
|
|
|
fetchMock.get(
|
|
|
|
LOGIN_GLOB,
|
|
|
|
{ csrf_token: 1234 },
|
|
|
|
{
|
|
|
|
overwriteRoutes: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CSRF queuing', () => {
|
2018-09-18 00:53:14 -04:00
|
|
|
it(`client.ensureAuth() returns a promise that rejects init() has not been called`, () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(2);
|
|
|
|
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.ensureAuth()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).toEqual(expect.objectContaining({ error: expect.any(String) }));
|
2018-10-18 20:53:05 -04:00
|
|
|
expect(client.isAuthenticated()).toBe(false);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('client.ensureAuth() returns a promise that resolves if client.init() resolves successfully', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
client
|
|
|
|
.ensureAuth()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(() => {
|
2018-10-18 20:53:05 -04:00
|
|
|
expect(client.isAuthenticated()).toBe(true);
|
2018-09-18 00:53:14 -04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it(`client.ensureAuth() returns a promise that rejects if init() is unsuccessful`, () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
const rejectValue = { status: 403 };
|
|
|
|
fetchMock.get(LOGIN_GLOB, () => Promise.reject(rejectValue), {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
const client = new SupersetClient({});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.init()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).toEqual(expect.objectContaining(rejectValue));
|
|
|
|
|
|
|
|
return client
|
|
|
|
.ensureAuth()
|
|
|
|
.then(throwIfCalled)
|
|
|
|
.catch(error2 => {
|
|
|
|
expect(error2).toEqual(expect.objectContaining(rejectValue));
|
2018-10-18 20:53:05 -04:00
|
|
|
expect(client.isAuthenticated()).toBe(false);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
// reset
|
|
|
|
fetchMock.get(
|
|
|
|
LOGIN_GLOB,
|
|
|
|
{ csrf_token: 1234 },
|
|
|
|
{
|
|
|
|
overwriteRoutes: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('requests', () => {
|
|
|
|
afterEach(fetchMock.reset);
|
2018-11-29 12:48:18 -05:00
|
|
|
const protocol = 'https:';
|
2018-09-07 21:36:47 -04:00
|
|
|
const host = 'HOST';
|
|
|
|
const mockGetEndpoint = '/get/url';
|
|
|
|
const mockPostEndpoint = '/post/url';
|
2018-09-18 00:53:14 -04:00
|
|
|
const mockTextEndpoint = '/text/endpoint';
|
2018-11-29 12:48:18 -05:00
|
|
|
const mockGetUrl = `${protocol}//${host}${mockGetEndpoint}`;
|
|
|
|
const mockPostUrl = `${protocol}//${host}${mockPostEndpoint}`;
|
|
|
|
const mockTextUrl = `${protocol}//${host}${mockTextEndpoint}`;
|
2018-09-18 00:53:14 -04:00
|
|
|
const mockTextJsonResponse = '{ "value": 9223372036854775807 }';
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
fetchMock.get(mockGetUrl, { json: 'payload' });
|
|
|
|
fetchMock.post(mockPostUrl, { json: 'payload' });
|
|
|
|
fetchMock.get(mockTextUrl, mockTextJsonResponse);
|
|
|
|
fetchMock.post(mockTextUrl, mockTextJsonResponse);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('checks for authentication before every get and post request', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(3);
|
|
|
|
const authSpy = jest.spyOn(SupersetClient.prototype, 'ensureAuth');
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
Promise.all([client.get({ url: mockGetUrl }), client.post({ url: mockPostUrl })]).then(
|
|
|
|
() => {
|
|
|
|
expect(fetchMock.calls(mockGetUrl)).toHaveLength(1);
|
|
|
|
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
|
|
|
|
expect(authSpy).toHaveBeenCalledTimes(2);
|
|
|
|
authSpy.mockRestore();
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('sets protocol, host, headers, mode, and credentials from config', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(3);
|
2018-11-29 12:48:18 -05:00
|
|
|
const clientConfig: ClientConfig = {
|
2018-09-07 21:36:47 -04:00
|
|
|
host,
|
|
|
|
protocol,
|
2018-11-29 12:48:18 -05:00
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'include',
|
2018-09-07 21:36:47 -04:00
|
|
|
headers: { my: 'header' },
|
|
|
|
};
|
|
|
|
|
|
|
|
const client = new SupersetClient(clientConfig);
|
2018-09-18 00:53:14 -04:00
|
|
|
|
|
|
|
return client.init().then(() =>
|
|
|
|
client.get({ url: mockGetUrl }).then(() => {
|
|
|
|
const fetchRequest = fetchMock.calls(mockGetUrl)[0][1];
|
|
|
|
expect(fetchRequest.mode).toBe(clientConfig.mode);
|
|
|
|
expect(fetchRequest.credentials).toBe(clientConfig.credentials);
|
|
|
|
expect(fetchRequest.headers).toEqual(expect.objectContaining(clientConfig.headers));
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET', () => {
|
2018-09-18 00:53:14 -04:00
|
|
|
it('makes a request using url or endpoint', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
2018-09-18 00:53:14 -04:00
|
|
|
|
|
|
|
return client.init().then(() =>
|
|
|
|
Promise.all([
|
|
|
|
client.get({ url: mockGetUrl }),
|
|
|
|
client.get({ endpoint: mockGetEndpoint }),
|
|
|
|
]).then(() => {
|
|
|
|
expect(fetchMock.calls(mockGetUrl)).toHaveLength(2);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports parsing a response as text', () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.init()
|
|
|
|
.then(() =>
|
2018-09-18 00:53:14 -04:00
|
|
|
client
|
|
|
|
.get({ url: mockTextUrl, parseMethod: 'text' })
|
|
|
|
.then(({ text }) => {
|
|
|
|
expect(fetchMock.calls(mockTextUrl)).toHaveLength(1);
|
|
|
|
expect(text).toBe(mockTextJsonResponse);
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
})
|
|
|
|
.catch(throwIfCalled),
|
|
|
|
)
|
|
|
|
.catch(throwIfCalled);
|
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('allows overriding host, headers, mode, and credentials per-request', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(3);
|
2018-11-29 12:48:18 -05:00
|
|
|
const clientConfig: ClientConfig = {
|
2018-09-07 21:36:47 -04:00
|
|
|
host,
|
|
|
|
protocol,
|
2018-11-29 12:48:18 -05:00
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'include',
|
2018-09-07 21:36:47 -04:00
|
|
|
headers: { my: 'header' },
|
|
|
|
};
|
|
|
|
|
2018-11-29 12:48:18 -05:00
|
|
|
const overrideConfig: ClientConfig = {
|
2018-09-07 21:36:47 -04:00
|
|
|
host: 'override_host',
|
2018-11-29 12:48:18 -05:00
|
|
|
mode: 'no-cors',
|
|
|
|
credentials: 'omit',
|
2018-09-07 21:36:47 -04:00
|
|
|
headers: { my: 'override', another: 'header' },
|
|
|
|
};
|
|
|
|
|
|
|
|
const client = new SupersetClient(clientConfig);
|
2018-09-18 00:53:14 -04:00
|
|
|
|
|
|
|
return client
|
2018-09-07 21:36:47 -04:00
|
|
|
.init()
|
|
|
|
.then(() =>
|
|
|
|
client
|
|
|
|
.get({ url: mockGetUrl, ...overrideConfig })
|
|
|
|
.then(() => {
|
|
|
|
const fetchRequest = fetchMock.calls(mockGetUrl)[0][1];
|
|
|
|
expect(fetchRequest.mode).toBe(overrideConfig.mode);
|
|
|
|
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
|
|
|
|
expect(fetchRequest.headers).toEqual(
|
|
|
|
expect.objectContaining(overrideConfig.headers),
|
|
|
|
);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
2018-09-07 21:36:47 -04:00
|
|
|
})
|
|
|
|
.catch(throwIfCalled),
|
|
|
|
)
|
|
|
|
.catch(throwIfCalled);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST', () => {
|
2018-09-18 00:53:14 -04:00
|
|
|
it('makes a request using url or endpoint', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
Promise.all([
|
|
|
|
client.post({ url: mockPostUrl }),
|
|
|
|
client.post({ endpoint: mockPostEndpoint }),
|
|
|
|
]).then(() => {
|
|
|
|
expect(fetchMock.calls(mockPostUrl)).toHaveLength(2);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('allows overriding host, headers, mode, and credentials per-request', () => {
|
2018-11-29 12:48:18 -05:00
|
|
|
const clientConfig: ClientConfig = {
|
2018-09-07 21:36:47 -04:00
|
|
|
host,
|
|
|
|
protocol,
|
2018-11-29 12:48:18 -05:00
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'include',
|
2018-09-07 21:36:47 -04:00
|
|
|
headers: { my: 'header' },
|
|
|
|
};
|
|
|
|
|
2018-11-29 12:48:18 -05:00
|
|
|
const overrideConfig: ClientConfig = {
|
2018-09-07 21:36:47 -04:00
|
|
|
host: 'override_host',
|
2018-11-29 12:48:18 -05:00
|
|
|
mode: 'no-cors',
|
|
|
|
credentials: 'omit',
|
2018-09-07 21:36:47 -04:00
|
|
|
headers: { my: 'override', another: 'header' },
|
|
|
|
};
|
|
|
|
|
|
|
|
const client = new SupersetClient(clientConfig);
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
client.post({ url: mockPostUrl, ...overrideConfig }).then(() => {
|
|
|
|
const fetchRequest = fetchMock.calls(mockPostUrl)[0][1];
|
|
|
|
expect(fetchRequest.mode).toBe(overrideConfig.mode);
|
|
|
|
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
|
|
|
|
expect(fetchRequest.headers).toEqual(expect.objectContaining(overrideConfig.headers));
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('supports parsing a response as text', () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
|
|
|
return client.init().then(() =>
|
|
|
|
client.post({ url: mockTextUrl, parseMethod: 'text' }).then(({ text }) => {
|
|
|
|
expect(fetchMock.calls(mockTextUrl)).toHaveLength(1);
|
|
|
|
expect(text).toBe(mockTextJsonResponse);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes postPayload key,values in the body', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
const postPayload = { number: 123, array: [1, 2, 3] };
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
client.post({ url: mockPostUrl, postPayload }).then(() => {
|
|
|
|
const formData = fetchMock.calls(mockPostUrl)[0][1].body;
|
|
|
|
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
|
|
|
|
Object.keys(postPayload).forEach(key => {
|
|
|
|
expect(formData.get(key)).toBe(JSON.stringify(postPayload[key]));
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
it('respects the stringify parameter for postPayload key,values', () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(3);
|
|
|
|
const postPayload = { number: 123, array: [1, 2, 3] };
|
|
|
|
const client = new SupersetClient({ protocol, host });
|
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return client.init().then(() =>
|
|
|
|
client.post({ url: mockPostUrl, postPayload, stringify: false }).then(() => {
|
|
|
|
const formData = fetchMock.calls(mockPostUrl)[0][1].body;
|
|
|
|
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
|
|
|
|
Object.keys(postPayload).forEach(key => {
|
|
|
|
expect(formData.get(key)).toBe(String(postPayload[key]));
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
|
2018-09-18 00:53:14 -04:00
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
|
|
|
);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|