Skip to content

Commit

Permalink
Server spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed May 30, 2011
1 parent 4034890 commit 716a6c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/jbundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module JBundle
JFILE = 'Jfile'

class NoJFileError < StandardError;end
class NoBundleError < StandardError;end

class << self

Expand Down Expand Up @@ -50,7 +51,7 @@ def write!

def build(name)
found = config.bundles_and_files.detect {|f| f.name == name}
raise "No bundle or file found with name #{name}" unless found
raise NoBundleError, "No bundle or file found with name #{name}" unless found
Builder.new(config).build_one found
end

Expand Down
15 changes: 10 additions & 5 deletions lib/jbundle/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ module JBundle

class Server


def initialize(jfile = JBundle::JFILE)
@jfile = jfile
end

# Configure JBundle on every request.
# Expensive but allows for reloading changes to JFile
def call(env)
JBundle.config_from_file(@jfile)
bundle_name = env['PATH_INFO'].sub('/', '')
if !bundle_name == ''
raise 'You need to define a bundle name ie. /my_bundle.js'
begin
JBundle.config_from_file(@jfile)
bundle_name = env['PATH_INFO'].sub('/', '')
[200, {'Content-Type' => 'application/x-javascript'}, [JBundle.build(bundle_name).src]]
rescue NoBundleError => boom
[404, {'Content-Type' => 'text/plain'}, ["No bundle defined. Try /[bundle_name].js as per your JFile"]]
rescue NoJFileError => boom
[404, {'Content-Type' => 'text/plain'}, [boom.message]]
end
[200, {'Content-Type' => 'application/x-javascript'}, [JBundle.build(bundle_name).src]]

end

end
Expand Down
11 changes: 11 additions & 0 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
@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
Expand All @@ -25,6 +31,11 @@
@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

end

0 comments on commit 716a6c7

Please sign in to comment.