Skip to content

Commit

Permalink
chore: remove usages of mime module from infrastructure
Browse files Browse the repository at this point in the history
Follow-up to 4d84e35
  • Loading branch information
aslushnikov committed Feb 10, 2020
1 parent 84f5700 commit 4fcc63f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 7 deletions.
11 changes: 9 additions & 2 deletions test/golden-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
const path = require('path');
const fs = require('fs');
const Diff = require('text-diff');
const mime = require('mime');
const PNG = require('pngjs').PNG;
const jpeg = require('jpeg-js');
const pixelmatch = require('pixelmatch');

module.exports = {compare};

const extensionToMimeType = {
'png': 'image/png',
'txt': 'text/plain',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
};

const GoldenComparators = {
'image/png': compareImages,
'image/jpeg': compareImages,
Expand Down Expand Up @@ -98,7 +104,8 @@ function compare(goldenPath, outputPath, actual, goldenName) {
};
}
const expected = fs.readFileSync(expectedPath);
const mimeType = mime.getType(goldenName);
const extension = goldenName.substring(goldenName.lastIndexOf('.') + 1);
const mimeType = extensionToMimeType[extension];
const comparator = GoldenComparators[mimeType];
if (!comparator) {
return {
Expand Down
6 changes: 3 additions & 3 deletions test/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ module.exports.describe = function({testRunner, expect, playwright, FFOX, CHROMI
).toBe(
fs.readFileSync(path.join(__dirname, '/assets/file-to-upload.txt')).toString()
);
expect(file2.name).toBe('file-to-upload.png');
expect(file2.name).toBe('pptr.png');
expect(file2.type).toBe('image/png');
expect(
fs.readFileSync(file2.path).toString()
).toBe(
fs.readFileSync(path.join(__dirname, '/assets/file-to-upload.png')).toString()
fs.readFileSync(path.join(__dirname, '/assets/pptr.png')).toString()
);
callback();
});
Expand All @@ -147,7 +147,7 @@ module.exports.describe = function({testRunner, expect, playwright, FFOX, CHROMI
<input type="submit" value="Submit">
</form>`)
await (await page.$('input[name=file1]')).setInputFiles(path.join(__dirname, '/assets/file-to-upload.txt'));
await (await page.$('input[name=file2]')).setInputFiles(path.join(__dirname, '/assets/file-to-upload.png'));
await (await page.$('input[name=file2]')).setInputFiles(path.join(__dirname, '/assets/pptr.png'));
page.click('input[type=submit]');
await result;
});
Expand Down
204 changes: 202 additions & 2 deletions utils/testserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const https = require('https');
const url = require('url');
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const WebSocketServer = require('ws').Server;

const fulfillSymbol = Symbol('fullfil callback');
Expand Down Expand Up @@ -248,7 +247,8 @@ class TestServer {
response.end(`File not found: ${filePath}`);
return;
}
const mimeType = mime.getType(filePath);
const extension = filePath.substring(filePath.lastIndexOf('.') + 1);
const mimeType = extensionToMime[extension] || 'application/octet-stream';
const isTextEncoding = /^text\/|^application\/(javascript|json)/.test(mimeType);
const contentType = isTextEncoding ? `${mimeType}; charset=utf-8` : mimeType;
response.setHeader('Content-Type', contentType);
Expand All @@ -269,4 +269,204 @@ class TestServer {
}
}

const extensionToMime = {
'ai': 'application/postscript',
'apng': 'image/apng',
'appcache': 'text/cache-manifest',
'au': 'audio/basic',
'bmp': 'image/bmp',
'cer': 'application/pkix-cert',
'cgm': 'image/cgm',
'coffee': 'text/coffeescript',
'conf': 'text/plain',
'crl': 'application/pkix-crl',
'css': 'text/css',
'csv': 'text/csv',
'def': 'text/plain',
'doc': 'application/msword',
'dot': 'application/msword',
'drle': 'image/dicom-rle',
'dtd': 'application/xml-dtd',
'ear': 'application/java-archive',
'emf': 'image/emf',
'eps': 'application/postscript',
'exr': 'image/aces',
'fits': 'image/fits',
'g3': 'image/g3fax',
'gbr': 'application/rpki-ghostbusters',
'gif': 'image/gif',
'glb': 'model/gltf-binary',
'gltf': 'model/gltf+json',
'gz': 'application/gzip',
'h261': 'video/h261',
'h263': 'video/h263',
'h264': 'video/h264',
'heic': 'image/heic',
'heics': 'image/heic-sequence',
'heif': 'image/heif',
'heifs': 'image/heif-sequence',
'htm': 'text/html',
'html': 'text/html',
'ics': 'text/calendar',
'ief': 'image/ief',
'ifb': 'text/calendar',
'iges': 'model/iges',
'igs': 'model/iges',
'in': 'text/plain',
'ini': 'text/plain',
'jade': 'text/jade',
'jar': 'application/java-archive',
'jls': 'image/jls',
'jp2': 'image/jp2',
'jpe': 'image/jpeg',
'jpeg': 'image/jpeg',
'jpf': 'image/jpx',
'jpg': 'image/jpeg',
'jpg2': 'image/jp2',
'jpgm': 'video/jpm',
'jpgv': 'video/jpeg',
'jpm': 'image/jpm',
'jpx': 'image/jpx',
'js': 'application/javascript',
'json': 'application/json',
'json5': 'application/json5',
'jsx': 'text/jsx',
'jxr': 'image/jxr',
'kar': 'audio/midi',
'ktx': 'image/ktx',
'less': 'text/less',
'list': 'text/plain',
'litcoffee': 'text/coffeescript',
'log': 'text/plain',
'm1v': 'video/mpeg',
'm21': 'application/mp21',
'm2a': 'audio/mpeg',
'm2v': 'video/mpeg',
'm3a': 'audio/mpeg',
'm4a': 'audio/mp4',
'm4p': 'application/mp4',
'man': 'text/troff',
'manifest': 'text/cache-manifest',
'markdown': 'text/markdown',
'mathml': 'application/mathml+xml',
'md': 'text/markdown',
'mdx': 'text/mdx',
'me': 'text/troff',
'mesh': 'model/mesh',
'mft': 'application/rpki-manifest',
'mid': 'audio/midi',
'midi': 'audio/midi',
'mj2': 'video/mj2',
'mjp2': 'video/mj2',
'mjs': 'application/javascript',
'mml': 'text/mathml',
'mov': 'video/quicktime',
'mp2': 'audio/mpeg',
'mp21': 'application/mp21',
'mp2a': 'audio/mpeg',
'mp3': 'audio/mpeg',
'mp4': 'video/mp4',
'mp4a': 'audio/mp4',
'mp4s': 'application/mp4',
'mp4v': 'video/mp4',
'mpe': 'video/mpeg',
'mpeg': 'video/mpeg',
'mpg': 'video/mpeg',
'mpg4': 'video/mp4',
'mpga': 'audio/mpeg',
'mrc': 'application/marc',
'ms': 'text/troff',
'msh': 'model/mesh',
'n3': 'text/n3',
'oga': 'audio/ogg',
'ogg': 'audio/ogg',
'ogv': 'video/ogg',
'ogx': 'application/ogg',
'otf': 'font/otf',
'p10': 'application/pkcs10',
'p7c': 'application/pkcs7-mime',
'p7m': 'application/pkcs7-mime',
'p7s': 'application/pkcs7-signature',
'p8': 'application/pkcs8',
'pdf': 'application/pdf',
'pki': 'application/pkixcmp',
'pkipath': 'application/pkix-pkipath',
'png': 'image/png',
'ps': 'application/postscript',
'pskcxml': 'application/pskc+xml',
'qt': 'video/quicktime',
'rmi': 'audio/midi',
'rng': 'application/xml',
'roa': 'application/rpki-roa',
'roff': 'text/troff',
'rsd': 'application/rsd+xml',
'rss': 'application/rss+xml',
'rtf': 'application/rtf',
'rtx': 'text/richtext',
's3m': 'audio/s3m',
'sgi': 'image/sgi',
'sgm': 'text/sgml',
'sgml': 'text/sgml',
'shex': 'text/shex',
'shtml': 'text/html',
'sil': 'audio/silk',
'silo': 'model/mesh',
'slim': 'text/slim',
'slm': 'text/slim',
'snd': 'audio/basic',
'spx': 'audio/ogg',
'stl': 'model/stl',
'styl': 'text/stylus',
'stylus': 'text/stylus',
'svg': 'image/svg+xml',
'svgz': 'image/svg+xml',
't': 'text/troff',
't38': 'image/t38',
'text': 'text/plain',
'tfx': 'image/tiff-fx',
'tif': 'image/tiff',
'tiff': 'image/tiff',
'tr': 'text/troff',
'ts': 'video/mp2t',
'tsv': 'text/tab-separated-values',
'ttc': 'font/collection',
'ttf': 'font/ttf',
'ttl': 'text/turtle',
'txt': 'text/plain',
'uri': 'text/uri-list',
'uris': 'text/uri-list',
'urls': 'text/uri-list',
'vcard': 'text/vcard',
'vrml': 'model/vrml',
'vtt': 'text/vtt',
'war': 'application/java-archive',
'wasm': 'application/wasm',
'wav': 'audio/wav',
'weba': 'audio/webm',
'webm': 'video/webm',
'webmanifest': 'application/manifest+json',
'webp': 'image/webp',
'wmf': 'image/wmf',
'woff': 'font/woff',
'woff2': 'font/woff2',
'wrl': 'model/vrml',
'x3d': 'model/x3d+xml',
'x3db': 'model/x3d+fastinfoset',
'x3dbz': 'model/x3d+binary',
'x3dv': 'model/x3d-vrml',
'x3dvz': 'model/x3d+vrml',
'x3dz': 'model/x3d+xml',
'xaml': 'application/xaml+xml',
'xht': 'application/xhtml+xml',
'xhtml': 'application/xhtml+xml',
'xm': 'audio/xm',
'xml': 'text/xml',
'xsd': 'application/xml',
'xsl': 'application/xml',
'xslt': 'application/xslt+xml',
'yaml': 'text/yaml',
'yml': 'text/yaml',
'zip': 'application/zip'
};

module.exports = {TestServer};

0 comments on commit 4fcc63f

Please sign in to comment.