Skip to content

Commit

Permalink
crowdin#188 - Encode the Crowdin-API-FileName
Browse files Browse the repository at this point in the history
  • Loading branch information
ade1705 committed Oct 10, 2022
1 parent e1aa636 commit 95773d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/uploadStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ export class UploadStorage extends CrowdinApi {
): Promise<ResponseObject<UploadStorageModel.Storage>> {
const url = `${this.url}/storages`;
const config = this.defaultConfig();
config.headers['Crowdin-API-FileName'] = fileName;
config.headers['Crowdin-API-FileName'] = this.urlEncodeFileName(fileName);
if (contentType) {
config.headers['Content-Type'] = contentType;
} else {
Expand All @@ -970,6 +970,13 @@ export class UploadStorage extends CrowdinApi {
return this.post(url, request, config);
}

/**
* @param fileName file name
*/
urlEncodeFileName(fileName: string): string {
return encodeURIComponent(fileName);
}

/**
* @param storageId storage identifier
* @see https://support.crowdin.com/api/v2/#operation/api.storages.get
Expand Down
9 changes: 8 additions & 1 deletion tests/uploadStorage/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Upload Storage API', () => {
const api: UploadStorage = new UploadStorage(credentials);
const storageId = 2;
const fileName = 'words.txt';
const urlEncodedFileName = encodeURIComponent(fileName);
const fileContent = 'test text.';

const limit = 25;
Expand All @@ -36,7 +37,7 @@ describe('Upload Storage API', () => {
})
.post('/storages', fileContent, {
reqheaders: {
'Crowdin-API-FileName': fileName,
'Crowdin-API-FileName': urlEncodedFileName,
Authorization: `Bearer ${api.token}`,
},
})
Expand Down Expand Up @@ -87,4 +88,10 @@ describe('Upload Storage API', () => {
it('Delete storage', async () => {
await api.deleteStorage(storageId);
});

it('URL encodes the filename', async () => {
const fileName = 'Беларусь';
const urlEncodeFileName = api.urlEncodeFileName(fileName);
expect(urlEncodeFileName).toBe('%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C');
});
});

0 comments on commit 95773d4

Please sign in to comment.