Skip to content

Commit

Permalink
Merge pull request #190 from ade1705/#188-Encode_the_Crowdin-API-File…
Browse files Browse the repository at this point in the history
…Name

#188 - Encode the Crowdin-API-FileName
  • Loading branch information
andrii-bodnar authored Oct 12, 2022
2 parents e1aa636 + f4a5c22 commit 106ea58
Show file tree
Hide file tree
Showing 2 changed files with 22 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
15 changes: 14 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,16 @@ describe('Upload Storage API', () => {
it('Delete storage', async () => {
await api.deleteStorage(storageId);
});

it('URL encodes a Cyrillic filename', async () => {
const fileName = 'абвгд';
const urlEncodeFileName = api.urlEncodeFileName(fileName);
expect(urlEncodeFileName).toBe('%D0%B0%D0%B1%D0%B2%D0%B3%D0%B4');
});

it('URL encodes a non-Cyrillic filename', async () => {
const fileName = 'filename';
const urlEncodeFileName = api.urlEncodeFileName(fileName);
expect(urlEncodeFileName).toBe('filename');
});
});

0 comments on commit 106ea58

Please sign in to comment.