Skip to content

Commit

Permalink
feat: use proper file extension based on content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinechalifour committed Aug 24, 2019
1 parent 2ac020f commit 8fdf5e2
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 41 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"devDependencies": {
"@release-it/conventional-changelog": "^1.1.0",
"@types/axios": "^0.14.0",
"@types/content-type": "^1.1.3",
"@types/cosmiconfig": "^5.0.3",
"@types/fs-extra": "^8.0.0",
"@types/jest": "^24.0.17",
Expand Down Expand Up @@ -81,6 +82,7 @@
"awilix-koa": "4.0.0",
"axios": "0.19.0",
"chalk": "2.4.2",
"content-type": "^1.0.4",
"cosmiconfig": "5.2.1",
"fs-extra": "8.1.0",
"koa": "2.8.1",
Expand Down
7 changes: 4 additions & 3 deletions src/infrastructure/repository/RequestRepositoryFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('persistResponseForRequest', () => {
const CASES = [
'APPLICATION/JSON',
'application/json',
'application/json; chartset=UTF-8;',
'application/json; charset=utf-8',
];

CASES.forEach(contentType => {
Expand Down Expand Up @@ -92,6 +92,7 @@ describe('persistResponseForRequest', () => {
const CASES = [
'APPLICATION/XML',
'application/xml',
'application/xml; charset=utf-8',
'TEXT/XML',
'text/xml',
];
Expand Down Expand Up @@ -203,7 +204,7 @@ describe('persistResponseForRequest', () => {
`${OUTPUT_DIRECTORY}/get__text-${inputRequest.id}/metadata.json`
);
const bodyContent = await fs.readFile(
`${OUTPUT_DIRECTORY}/get__text-${inputRequest.id}/body.txt`,
`${OUTPUT_DIRECTORY}/get__text-${inputRequest.id}/body`,
'utf-8'
);

Expand Down Expand Up @@ -241,7 +242,7 @@ describe('persistResponseForRequest', () => {
`${OUTPUT_DIRECTORY}/get__really_long_url-${inputRequest.id}/metadata.json`
);
const bodyContent = await fs.readFile(
`${OUTPUT_DIRECTORY}/get__really_long_url-${inputRequest.id}/body.txt`,
`${OUTPUT_DIRECTORY}/get__really_long_url-${inputRequest.id}/body`,
'utf-8'
);

Expand Down
37 changes: 7 additions & 30 deletions src/infrastructure/repository/RequestRepositoryFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from 'path';
import fs from 'fs-extra';

import { getProjectDirectory, getRequestDirectory } from '../../utils/path';
import {
getProjectDirectory,
getRequestDirectory,
getFileExtension,
} from '../../utils/path';
import { RequestRepository } from '../../domain/repository';
import { Request, Response } from '../../domain/entity';

Expand All @@ -10,25 +14,6 @@ interface Dependencies {
cacheDirectory: string;
}

function isJson(contentType: string | undefined) {
return (
contentType && contentType.toLowerCase().indexOf('application/json') >= 0
);
}

function isXml(contentType: string | undefined) {
if (!contentType) {
return false;
}

const lowerCaseContentType = contentType.toLowerCase();

return (
lowerCaseContentType.indexOf('application/xml') >= 0 ||
lowerCaseContentType.indexOf('text/xml') >= 0
);
}

export class RequestRepositoryFile implements RequestRepository {
private targetUrl: string;
private cacheDirectory: string;
Expand Down Expand Up @@ -141,19 +126,11 @@ export class RequestRepositoryFile implements RequestRepository {
private async getResponseBodyFilePath(request: Request) {
const metadata = await this.getRequestMetaData(request);
const contentType = metadata.responseHeaders['content-type'];
let fileExtension: string;

if (isJson(contentType)) {
fileExtension = 'json';
} else if (isXml(contentType)) {
fileExtension = 'xml';
} else {
fileExtension = 'txt';
}
const fileExtension = getFileExtension(contentType);

return path.join(
this.getRequestDirectoryPath(request),
`body.${fileExtension}`
`body${fileExtension}`
);
}

Expand Down
147 changes: 146 additions & 1 deletion src/utils/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Request } from '../domain/entity';
import { getProjectDirectory, getRequestDirectory } from './path';
import {
getProjectDirectory,
getRequestDirectory,
getFileExtension,
} from './path';

describe('getProjectDirectory', () => {
it('should return the project directory', () => {
Expand Down Expand Up @@ -42,3 +46,144 @@ describe('getRequestDirectory', () => {
);
});
});

describe('getFileExtension', () => {
it.each([
['application/json', '.json'],
['application/json; charset=utf-8', '.json'],
['application/xml', '.xml'],
['application/xml; charset=utf-8', '.xml'],
['application/envoy', '.evy'],
['application/fractals', '.fif'],
['application/futuresplash', '.spl'],
['application/hta', '.hta'],
['application/internet-property-stream', '.acx'],
['application/mac-binhex40', '.hqx'],
['application/msword', '.doc'],
['application/octet-stream', ''],
['application/oda', '.oda'],
['application/olescript', '.axs'],
['application/pdf', '.pdf'],
['application/pics-rules', '.prf'],
['application/pkcs10', '.p10'],
['application/pkix-crl', '.crl'],
['application/postscript', '.eps'],
['application/rtf', '.rtf'],
['application/xml', '.xml'],
['application/set-payment-initiation', '.setpay'],
['application/set-registration-initiation', '.setreg'],
['application/vnd.ms-excel', '.xls'],
['application/vnd.ms-outlook', '.msg'],
['application/vnd.ms-pkicertstore', '.sst'],
['application/vnd.ms-pkiseccat', '.cat'],
['application/vnd.ms-pkistl', '.stl'],
['application/vnd.ms-powerpoint', '.ppt'],
['application/vnd.ms-project', '.mpp'],
['application/vnd.ms-works', '.wks'],
['application/winhlp', '.hlp'],
['application/x-bcpio', '.bcpio'],
['application/x-cdf', '.cdf'],
['application/x-compress', '.z'],
['application/x-compressed', '.tgz'],
['application/x-cpio', '.cpio'],
['application/x-csh', '.csh'],
['application/x-director', '.dcr'],
['application/x-dvi', '.dvi'],
['application/x-gtar', '.gtar'],
['application/x-gzip', '.gz'],
['application/x-hdf', '.hdf'],
['application/x-internet-signup', '.ins'],
['application/x-iphone', '.iii'],
['application/x-javascript', '.js'],
['application/x-latex', '.latex'],
['application/x-msaccess', '.mdb'],
['application/x-mscardfile', '.crd'],
['application/x-msclip', '.clp'],
['application/x-msdownload', '.dll'],
['application/x-msmediaview', '.m13'],
['application/x-msmetafile', '.wmf'],
['application/x-msmoney', '.mny'],
['application/x-mspublisher', '.pub'],
['application/x-msschedule', '.scd'],
['application/x-msterminal', '.trm'],
['application/x-mswrite', '.wri'],
['application/x-netcdf', '.cdf'],
['application/x-perfmon', '.pma'],
['application/x-pkcs12', '.p12'],
['application/x-pkcs7-certificates', '.p7b'],
['application/x-pkcs7-certreqresp', '.p7r'],
['application/x-pkcs7-mime', '.p7c'],
['application/x-pkcs7-signature', '.p7s'],
['application/x-sh', '.sh'],
['application/x-shar', '.shar'],
['application/x-shockwave-flash', '.swf'],
['application/x-stuffit', '.sit'],
['application/x-sv4cpio', '.sv4cpio'],
['application/x-sv4crc', '.sv4crc'],
['application/x-tar', '.tar'],
['application/x-tcl', '.tcl'],
['application/x-tex', '.tex'],
['application/x-texinfo', '.texi'],
['application/x-troff', '.roff'],
['application/x-troff-man', '.man'],
['application/x-troff-me', '.me'],
['application/x-troff-ms', '.ms'],
['application/x-ustar', '.ustar'],
['application/x-wais-source', '.src'],
['application/x-x509-ca-cert', '.cer'],
['application/ynd.ms-pkipko', '.pko'],
['application/zip', '.zip'],
['audio/basic', '.au'],
['audio/mid', '.mid'],
['audio/mpeg', '.mp3'],
['audio/x-aiff', '.aif'],
['audio/x-mpegurl', '.m3u'],
['audio/x-pn-realaudio', '.ra'],
['audio/x-wav', '.wav'],
['image/bmp', '.bmp'],
['image/cis-cod', '.cod'],
['image/gif', '.gif'],
['image/ief', '.ief'],
['image/jpeg', '.jpg'],
['image/pipeg', '.jfif'],
['image/svg+xml', '.svg'],
['image/tiff', '.tiff'],
['image/x-cmu-raster', '.ras'],
['image/x-cmx', '.cmx'],
['image/x-icon', '.ico'],
['image/x-portable-anymap', '.pnm'],
['image/x-portable-bitmap', '.pbm'],
['image/x-portable-graymap', '.pgm'],
['image/x-portable-pixmap', '.ppm'],
['image/x-rgb', '.rgb'],
['image/x-xbitmap', '.xbm'],
['image/x-xpixmap', '.xpm'],
['image/x-xwindowdump', '.xwd'],
['message/rfc822', '.mhtml'],
['text/css', '.css'],
['text/h323', '.323'],
['text/html', '.html'],
['text/iuls', '.uls'],
['text/plain', '.txt'],
['text/xml', '.xml'],
['text/richtext', '.rtx'],
['text/scriptlet', '.sct'],
['text/tab-separated-values', '.tsv'],
['text/webviewhtml', '.htt'],
['text/x-component', '.htc'],
['text/x-setext', '.etx'],
['text/x-vcard', '.vcf'],
['video/mpeg', '.mpeg'],
['video/mp4', '.mp4'],
['video/quicktime', '.mov'],
['video/x-la-asf', '.lsf'],
['video/x-ms-asf', '.asf'],
['video/x-msvideo', '.avi'],
['video/x-sgi-movie', '.movie'],
['x-world/x-vrml', '.vrml'],
['invalid;type;;', ''],
['', ''],
])('%s returns %s', (contentType: string, expectedExtension: string) => {
expect(getFileExtension(contentType)).toEqual(expectedExtension);
});
});
Loading

0 comments on commit 8fdf5e2

Please sign in to comment.