/
middleware_spec.rb
199 lines (149 loc) · 5.18 KB
/
middleware_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require "rake-pipeline/middleware"
require "rake-pipeline/filters"
require "rack/test"
describe "Rake::Pipeline Middleware" do
include Rack::Test::Methods
inputs = {
"app/javascripts/jquery.js" => "var jQuery = {};\n",
"app/javascripts/sproutcore.js" => <<-HERE.gsub(/^ {6}/, ''),
var SC = {};
assert(SC);
SC.hi = function() { console.log("hi"); };
HERE
"app/index.html" => "<html>HI</html>",
"app/javascripts/index.html" => "<html>JAVASCRIPT</html>",
"app/empty_dir" => nil
}
expected_output = <<-HERE.gsub(/^ {4}/, '')
var jQuery = {};
var SC = {};
SC.hi = function() { console.log("hi"); };
HERE
assetfile_source = <<-HERE.gsub(/^ {4}/, '')
require "#{tmp}/../support/spec_helpers/filters"
output "public"
map "/dynamic-request.js" do
[200, { "Content-Type" => "text/plain" }, ["I am dynamic!"]]
end
input "#{tmp}", "app/**/*" do
match "*.js" do
concat "javascripts/application.js"
filter(Rake::Pipeline::SpecHelpers::Filters::StripAssertsFilter) { |input| input }
end
# copy the rest
concat { |input| input.sub(%r|^app/|, '') }
end
HERE
modified_assetfile_source = <<-HERE.gsub(/^ {4}/, '')
require "#{tmp}/../support/spec_helpers/filters"
output "public"
input "#{tmp}", "app/**/*" do
match "*.js" do
concat { "javascripts/app.js" }
filter(Rake::Pipeline::SpecHelpers::Filters::StripAssertsFilter) { |input| input }
end
# copy the rest
concat { |input| input.sub(%r|^app/|, '') }
end
HERE
app = middleware = nil
let(:app) { middleware }
before do
assetfile_path = File.join(tmp, "Assetfile")
File.open(assetfile_path, "w") { |file| file.write(assetfile_source) }
project = Rake::Pipeline::Project.new assetfile_path
app = lambda { |env| [404, {}, ['not found']] }
middleware = Rake::Pipeline::Middleware.new(app, project)
end
describe "dynamic requests" do
it "returns the value from the given block for paths that have been mapped" do
get "/dynamic-request.js"
last_response.should be_ok
last_response.headers["Content-Type"].should == "text/plain"
last_response.body.should == "I am dynamic!"
end
end
describe "static requests" do
before do
inputs.each do |name, string|
path = File.join(tmp, name)
if string
mkdir_p File.dirname(path)
File.open(path, "w") { |file| file.write(string) }
else
mkdir_p path
end
end
get "/javascripts/application.js"
end
it "returns files relative to the output directory" do
last_response.should be_ok
last_response.body.should == expected_output
last_response.headers["Content-Type"].should == "application/javascript"
end
it "updates the output when files change" do
age_existing_files
File.open(File.join(tmp, "app/javascripts/jquery.js"), "w") do |file|
file.write "var jQuery = {};\njQuery.trim = function() {};\n"
end
expected = <<-HERE.gsub(/^ {6}/, '')
var jQuery = {};
jQuery.trim = function() {};
var SC = {};
SC.hi = function() { console.log("hi"); };
HERE
get "/javascripts/application.js"
last_response.body.should == expected
last_response.headers["Content-Type"].should == "application/javascript"
end
it "updates the output when new files are added" do
age_existing_files
File.open(File.join(tmp, "app/javascripts/history.js"), "w") do |file|
file.write "var History = {};\n"
end
expected = <<-HERE.gsub(/^ {6}/, '')
var History = {};
var jQuery = {};
var SC = {};
SC.hi = function() { console.log("hi"); };
HERE
get "/javascripts/application.js"
last_response.body.should == expected
last_response.headers["Content-Type"].should == "application/javascript"
end
it "returns index.html for directories" do
get "/"
last_response.body.should == "<html>HI</html>"
last_response.headers["Content-Type"].should == "text/html"
get "/javascripts"
last_response.body.should == "<html>JAVASCRIPT</html>"
last_response.headers["Content-Type"].should == "text/html"
end
it "ignores directories without index.html" do
get "/empty_dir"
last_response.body.should == "not found"
last_response.status.should == 404
end
it "falls back to the app" do
get "/zomg.notfound"
last_response.body.should == "not found"
last_response.status.should == 404
end
it "recreates the pipeline when the Assetfile changes" do
get "/javascripts/app.js"
last_response.body.should == "not found"
last_response.status.should == 404
File.open(File.join(tmp, "Assetfile"), "w") do |file|
file.write(modified_assetfile_source)
end
expected = <<-HERE.gsub(/^ {6}/, '')
var jQuery = {};
var SC = {};
SC.hi = function() { console.log("hi"); };
HERE
get "/javascripts/app.js"
last_response.body.should == expected
last_response.headers["Content-Type"].should == "application/javascript"
end
end
end