UNPKG

7.28 kBtext/coffeescriptView Raw
1async = require 'async'
2should = require('chai').should()
3rack = require '../.'
4express = require 'express.io'
5easyrequest = require 'request'
6fs = require 'fs'
7{join} = require 'path'
8
9class CustomAsset extends rack.Asset
10 create: (options) ->
11 @emit 'created', contents: fs.readFileSync options.filename
12
13describe 'a dynamic asset builder', ->
14 app = null
15 fixturesDir = join __dirname, 'fixtures'
16
17 it 'should work with any custom asset that takes filename option', (done) ->
18 app = express().http()
19 app.use new rack.DynamicAssets
20 type: CustomAsset
21 urlPrefix: '/static'
22 dirname: join fixturesDir, 'static'
23 app.listen 7076, ->
24 async.parallel [
25 (next) ->
26 easyrequest 'http://localhost:7076/static/blank.txt', (error, response, body) ->
27 response.headers['content-type'].should.equal 'text/plain'
28 body.should.equal fs.readFileSync join(fixturesDir, 'static/blank.txt'), 'utf8'
29 next()
30 (next) ->
31 easyrequest 'http://localhost:7076/static/crazy-man.svg', (error, response, body) ->
32 response.headers['content-type'].should.equal 'image/svg+xml'
33 body.should.equal fs.readFileSync join(fixturesDir, 'static/crazy-man.svg'), 'utf8'
34 next()
35 ], done
36
37 it 'should work with a rack', (done) ->
38 app = express().http()
39 app.use new rack.Rack [
40 new rack.DynamicAssets
41 type: CustomAsset
42 urlPrefix: '/static'
43 dirname: join fixturesDir, 'static'
44 ]
45 app.listen 7076, ->
46 easyrequest 'http://localhost:7076/static/blank.txt', (error, response, body) ->
47 body.should.equal fs.readFileSync join(fixturesDir, 'static/blank.txt'), 'utf8'
48 done()
49
50 it 'should work with no urlPrefix option', (done) ->
51 app = express().http()
52 app.use new rack.DynamicAssets
53 type: CustomAsset
54 dirname: join fixturesDir, 'static'
55 app.listen 7076, ->
56 easyrequest 'http://localhost:7076/blank.txt', (error, response, body) ->
57 response.statusCode.should.equal 200
58 done()
59
60 it 'should work with options option', (done) ->
61 app = express().http()
62 app.use new rack.DynamicAssets
63 type: CustomAsset
64 dirname: join fixturesDir, 'static'
65 options:
66 mimetype: 'text/css'
67 app.listen 7076, ->
68 easyrequest 'http://localhost:7076/blank.txt', (error, response, body) ->
69 response.headers['content-type'].should.equal 'text/css'
70 done()
71
72 it 'should work with a filter', (done) ->
73 app = express().http()
74 app.use new rack.Rack [
75 new rack.DynamicAssets
76 type: CustomAsset
77 urlPrefix: '/string-filter'
78 dirname: join fixturesDir, 'static'
79 filter: 'txt'
80 new rack.DynamicAssets
81 type: CustomAsset
82 urlPrefix: '/function-filter'
83 dirname: join fixturesDir, 'static'
84 filter: (file) -> file.ext is '.svg'
85 ]
86 app.listen 7076, ->
87 async.parallel [
88 (next) ->
89 easyrequest 'http://localhost:7076/string-filter/blank.txt', (error, response, body) ->
90 response.statusCode.should.equal 200
91 next()
92 (next) ->
93 easyrequest 'http://localhost:7076/string-filter/crazy-man.svg', (error, response, body) ->
94 response.statusCode.should.equal 404
95 next()
96 (next) ->
97 easyrequest 'http://localhost:7076/function-filter/blank.txt', (error, response, body) ->
98 response.statusCode.should.equal 404
99 next()
100 (next) ->
101 easyrequest 'http://localhost:7076/function-filter/crazy-man.svg', (error, response, body) ->
102 response.statusCode.should.equal 200
103 next()
104 ], done
105
106 it 'should work with StylusAsset', (done) ->
107 app = express().http()
108 app.use new rack.DynamicAssets
109 type: rack.StylusAsset
110 dirname: join fixturesDir, 'stylus'
111 filter: 'styl'
112 app.listen 7076, ->
113 easyrequest 'http://localhost:7076/simple.css', (error, response, body) ->
114 response.headers['content-type'].should.equal 'text/css'
115 body.should.equal fs.readFileSync join(fixturesDir, 'stylus/simple.css'), 'utf8'
116 done()
117
118 it 'should work with LessAsset', (done) ->
119 app = express().http()
120 app.use new rack.DynamicAssets
121 type: rack.LessAsset
122 dirname: join fixturesDir, 'less'
123 filter: (file) -> file.ext is '.less' and file.name isnt 'another.less' and file.name isnt 'syntax-error.less'
124 app.listen 7076, ->
125 easyrequest 'http://localhost:7076/simple.css', (error, response, body) ->
126 response.headers['content-type'].should.equal 'text/css'
127 body.should.equal fs.readFileSync join(fixturesDir, 'less/simple.css'), 'utf8'
128 done()
129
130 # TODO: re-enable thi test
131 """
132 it 'should work with SassAsset', (done) ->
133 app = express().http()
134 app.use new rack.DynamicAssets
135 type: rack.SassAsset
136 dirname: join fixturesDir, 'sass'
137 filter: 'sass'
138 app.listen 7076, ->
139 easyrequest 'http://localhost:7076/simple.css', (error, response, body) ->
140 response.headers['content-type'].should.equal 'text/css'
141 body.should.equal fs.readFileSync join(fixturesDir, 'sass/simple.css'), 'utf8'
142 done()
143 """
144
145 it 'should work with SnocketsAsset', (done) ->
146 app = express().http()
147 app.use new rack.DynamicAssets
148 type: rack.SnocketsAsset
149 dirname: join fixturesDir, 'snockets'
150 filter: 'coffee'
151 app.listen 7076, ->
152 easyrequest 'http://localhost:7076/app.js', (error, response, body) ->
153 response.headers['content-type'].should.equal 'text/javascript'
154 body.should.equal fs.readFileSync join(fixturesDir, 'snockets/app.js'), 'utf8'
155 done()
156
157 it 'should work with BrowserifyAsset', (done) ->
158 app = express().http()
159 app.use new rack.DynamicAssets
160 type: rack.BrowserifyAsset
161 dirname: join fixturesDir, 'browserify'
162 filter: 'coffee'
163 app.listen 7076, ->
164 easyrequest 'http://localhost:7076/app.js', (error, response, body) ->
165 response.headers['content-type'].should.equal 'text/javascript'
166 body.should.equal fs.readFileSync join(fixturesDir, 'browserify/app.js'), 'utf8'
167 done()
168
169 afterEach (done) -> process.nextTick ->
170 app.server.close done