Skip to content

Commit

Permalink
Test server can serve bundles and files by original or distribution n…
Browse files Browse the repository at this point in the history
…ame, and non-js files are served raw
  • Loading branch information
ismasan committed Jun 19, 2011
1 parent c90acaa commit 2d8e504
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
jbundle (0.1.1)
jbundle (0.1.2)
closure-compiler
rack
thor
Expand Down
8 changes: 6 additions & 2 deletions lib/jbundle.rb
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/jbundle/builder.rb
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/jbundle/command_line.rb
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/jbundle/server.rb
@@ -1,3 +1,4 @@
require 'rack'
module JBundle

class Server
Expand All @@ -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"]]
Expand Down
66 changes: 43 additions & 23 deletions spec/server_spec.rb
Expand Up @@ -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.