Permalink
Browse files

Test server can serve bundles and files by original or distribution n…

…ame, and non-js files are served raw
  • Loading branch information...
1 parent c90acaa commit 2d8e5049a8a59c31c4a06147e823b3d56e4ea34a @ismasan committed Jun 19, 2011
Showing with 60 additions and 30 deletions.
  1. +1 −1 Gemfile.lock
  2. +6 −2 lib/jbundle.rb
  3. +5 −1 lib/jbundle/builder.rb
  4. +1 −2 lib/jbundle/command_line.rb
  5. +4 −1 lib/jbundle/server.rb
  6. +43 −23 spec/server_spec.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- jbundle (0.1.1)
+ jbundle (0.1.2)
closure-compiler
rack
thor
View
@@ -50,9 +50,13 @@ def write!
end
def build(name)
- found = config.bundles_and_files.detect {|f| f.original_name == name}
+ Builder.new(config).build_one find(name)
+ end
+
+ def find(name)
+ found = config.bundles_and_files.detect {|f| f.original_name == name || f.name == name}
raise NoBundleError, "No bundle or file found with name #{name}" unless found
- Builder.new(config).build_one found
+ found
end
def config_from_file(file)
View
@@ -13,8 +13,12 @@ def initialize(name, file_list, config)
@name, @dir = parse_name(name)
end
+ def ext
+ @ext ||= ::File.extname(name)
+ end
+
def buildable?
- BUILDABLE_FILES.include?(::File.extname(name))
+ BUILDABLE_FILES.include?(ext)
end
# This only makes sense for one-file objects
@@ -25,11 +25,10 @@ def bundle
method_option :port, :default => "5555", :aliases => "-p"
method_option :jfile, :default => JBundle::JFILE, :aliases => "-j"
def server
- require 'rack'
JBundle.config_from_file(options[:jfile])
say "Starting test server on http://localhost:#{options[:port]}. Available bundles:", :yellow
JBundle.config.bundles_and_files.each do |f|
- say "- /#{f.original_name}", :green
+ say "- /#{f.original_name} ==> /#{f.name}", :green
end
handler = Rack::Handler.default
View
@@ -1,3 +1,4 @@
+require 'rack'
module JBundle
class Server
@@ -13,7 +14,9 @@ def call(env)
bundle_name = env['PATH_INFO'].sub('/', '')
begin
JBundle.config_from_file(@jfile)
- [200, {'Content-Type' => 'application/x-javascript'}, [JBundle.build(bundle_name).src]]
+ compiler = JBundle.build(bundle_name)
+ body = compiler.buildable? ? compiler.src : compiler.raw_src
+ [200, {'Content-Type' => ::Rack::Mime.mime_type(compiler.ext)}, [body]]
rescue NoBundleError => boom
p = bundle_name == '' ? '[bundle_name].js' : bundle_name
[404, {'Content-Type' => 'text/plain'}, ["No bundle defined. Try defining /#{p} in your JFile"]]
View
@@ -9,33 +9,53 @@
end
describe '#call' do
- before do
- @response = @server.call({'PATH_INFO' => '/foo.js'})
- end
-
- it 'should be 404 if no JFile found' do
- s = JBundle::Server.new("doesn-not-exist")
- r = s.call({'PATH_INFO' => '/foo.js'})
- r[0].should == 404
- end
-
- it 'should be 200 OK' do
- @response[0].should == 200
- end
-
- it 'should be a javascript response' do
- @response[1]['Content-Type'].should == 'application/x-javascript'
- end
- it 'should return content for given bundle' do
- @response[2].should == [JBundle.build('foo.js').src]
+ describe 'with JavaScript bundle' do
+ before do
+ @response = @server.call({'PATH_INFO' => '/foo.js'})
+ end
+
+ it 'should be 404 if no JFile found' do
+ s = JBundle::Server.new("doesn-not-exist")
+ r = s.call({'PATH_INFO' => '/foo.js'})
+ r[0].should == 404
+ end
+
+ it 'should be 200 OK' do
+ @response[0].should == 200
+ end
+
+ it 'should be a javascript response' do
+ @response[1]['Content-Type'].should == 'application/javascript'
+ end
+
+ it 'should return content for given bundle' do
+ @response[2].should == [JBundle.build('foo.js').src]
+ end
+
+ it 'should be 404 when no bundle found' do
+ r = @server.call({'PATH_INFO' => '/nonexisting.js'})
+ r[0].should == 404
+ end
end
- it 'should be 404 when no bundle found' do
- r = @server.call({'PATH_INFO' => '/nonexisting.js'})
- r[0].should == 404
+ describe 'with non-JS file' do
+
+ it 'should find and serve by original name' do
+ response = @server.call({'PATH_INFO' => '/nested/foo.txt'})
+ response[0].should == 200
+ response[1]['Content-Type'].should == 'text/plain'
+ response[2].should == ["This file is in a nested subdirectory\n"]
+ end
+
+ it 'should find and serve by distribution file name' do
+ response = @server.call({'PATH_INFO' => '/flat_foo.txt'})
+ response[0].should == 200
+ response[1]['Content-Type'].should == 'text/plain'
+ response[2].should == ["This file is in a nested subdirectory\n"]
+ end
+
end
-
end
end

0 comments on commit 2d8e504

Please sign in to comment.