When serving output files, intelligently determine the appropriate content-type.

cherry pick 6fa3fda3ed28239cf3ac9720629f9707663aa197 from datajoy.
This commit is contained in:
Shane Kilkelly
2015-09-21 14:04:08 +01:00
parent d83efdbc98
commit 29be2dc700
3 changed files with 79 additions and 5 deletions

View File

@@ -0,0 +1,51 @@
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
modulePath = require('path').join __dirname, '../../../app/js/ContentTypeMapper'
describe 'ContentTypeMapper', ->
beforeEach ->
@ContentTypeMapper = SandboxedModule.require modulePath
describe 'map', ->
it 'should map .txt to text/plain', ->
content_type = @ContentTypeMapper.map('example.txt')
content_type.should.equal 'text/plain'
it 'should map .csv to text/csv', ->
content_type = @ContentTypeMapper.map('example.csv')
content_type.should.equal 'text/csv'
it 'should map .pdf to application/pdf', ->
content_type = @ContentTypeMapper.map('example.pdf')
content_type.should.equal 'application/pdf'
it 'should fall back to octet-stream', ->
content_type = @ContentTypeMapper.map('example.unknown')
content_type.should.equal 'application/octet-stream'
describe 'coercing web files to plain text', ->
it 'should map .js to plain text', ->
content_type = @ContentTypeMapper.map('example.js')
content_type.should.equal 'text/plain'
it 'should map .html to plain text', ->
content_type = @ContentTypeMapper.map('example.html')
content_type.should.equal 'text/plain'
it 'should map .css to plain text', ->
content_type = @ContentTypeMapper.map('example.css')
content_type.should.equal 'text/plain'
describe 'image files', ->
it 'should map .png to image/png', ->
content_type = @ContentTypeMapper.map('example.png')
content_type.should.equal 'image/png'
it 'should map .jpeg to image/jpeg', ->
content_type = @ContentTypeMapper.map('example.jpeg')
content_type.should.equal 'image/jpeg'