Skip to content

Commit

Permalink
fix(platformclient): use FormData object as checkToken request body
Browse files Browse the repository at this point in the history
  • Loading branch information
gdostie committed Sep 27, 2019
1 parent ec0ad12 commit 9bb6c0e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/APICore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ export default class API {
async post<T>(
url: string,
body: any,
args: RequestInit = {method: 'post', body: JSON.stringify(body)}
args: RequestInit = {method: 'post', body: JSON.stringify(body), headers: {'Content-Type': 'application/json'}}
): Promise<T> {
return await this.request<T>(url, args);
}

async put<T>(url: string, body: any, args: RequestInit = {method: 'put', body: JSON.stringify(body)}): Promise<T> {
async postForm<T>(url: string, form: FormData, args: RequestInit = {method: 'post', body: form}): Promise<T> {
return await this.request<T>(url, args);
}

async put<T>(
url: string,
body: any,
args: RequestInit = {method: 'put', body: JSON.stringify(body), headers: {'Content-Type': 'application/json'}}
): Promise<T> {
return await this.request<T>(url, args);
}

Expand All @@ -56,8 +64,8 @@ export default class API {
const init: RequestInit = {
...args,
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${this.config.accessTokenRetriever()}`,
Authorization: `Bearer ${this.config.accessTokenRetriever()}`,
Accept: 'application/json',
...(args.headers || {}),
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/PlatformClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export class PlatformClient extends PlatformResources {
}

private async checkToken() {
return this.API.post('/oauth/check_token', {token: this.options.accessTokenRetriever()});
const formData = new FormData();
formData.set('token', this.options.accessTokenRetriever());
return this.API.postForm<any>('/oauth/check_token', formData);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/tests/APICore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ describe('APICore', () => {
expect(url).toBe(`${testConfig.host}${testData.route}`);
expect(options.method).toBe('post');
expect(options.body).toBe(JSON.stringify(testData.body));
expect(options.headers).toEqual(expect.objectContaining({'Content-Type': 'application/json'}));
expect(response).toEqual(testData.response);
});
});

describe('postForm', () => {
test('simple request', async () => {
const formMock: jest.Mocked<FormData> = jest.fn() as any;
const fetchMock = global.fetch.mockResponseOnce(JSON.stringify(testData.response));
const response = await api.postForm(testData.route, formMock);

expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, options] = fetchMock.mock.calls[0];

expect(url).toBe(`${testConfig.host}${testData.route}`);
expect(options.method).toBe('post');
expect(options.body).toBe(formMock);
expect(options.headers).not.toEqual(expect.objectContaining({'Content-Type': 'application/json'}));
expect(response).toEqual(testData.response);
});
});
Expand All @@ -78,6 +96,7 @@ describe('APICore', () => {
expect(url).toBe(`${testConfig.host}${testData.route}`);
expect(options.method).toBe('put');
expect(options.body).toBe(JSON.stringify(testData.body));
expect(options.headers).toEqual(expect.objectContaining({'Content-Type': 'application/json'}));
expect(response).toEqual(testData.response);
});
});
Expand Down
22 changes: 15 additions & 7 deletions src/tests/PlatformClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,31 @@ describe('PlatformClient', () => {
});

describe('initialize', () => {
const mockedFormData = {
set: jest.fn(),
};

beforeEach(() => {
(global as any).FormData = jest.fn(() => mockedFormData);
});

it('should check if the retrieved token is valid', async () => {
const platform = new PlatformClient(baseOptions);
const APIMockInstance = APIMock.mock.instances[0];

await platform.initialize();

expect(APIMockInstance.post).toHaveBeenCalledTimes(1);
expect(APIMockInstance.post).toHaveBeenCalledWith(
'/oauth/check_token',
expect.objectContaining({token: baseOptions.accessTokenRetriever()})
);
expect(APIMockInstance.postForm).toHaveBeenCalledTimes(1);
expect(APIMockInstance.postForm).toHaveBeenCalledWith('/oauth/check_token', mockedFormData);
expect(mockedFormData.set).toHaveBeenCalledTimes(1);
expect(mockedFormData.set).toHaveBeenCalledWith('token', baseOptions.accessTokenRetriever());
});

it('should throw an error if the check token call fails', async () => {
const platform = new PlatformClient(baseOptions);
const APIPostMock: jest.Mock<ReturnType<typeof API.prototype.post>> = APIMock.mock.instances[0].post as any;
APIPostMock.mockRejectedValue(new Error('invalid token'));
const APIPostFormMock: jest.Mock<ReturnType<typeof API.prototype.postForm>> = APIMock.mock.instances[0]
.postForm as any;
APIPostFormMock.mockRejectedValue(new Error('invalid token'));

try {
await platform.initialize();
Expand Down

0 comments on commit 9bb6c0e

Please sign in to comment.