mojito 0.1.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. data/VERSION +1 -1
  2. data/lib/mojito.rb +21 -44
  3. data/lib/mojito/base.rb +29 -58
  4. data/lib/mojito/controllers.rb +10 -0
  5. data/lib/mojito/controllers/method.rb +65 -0
  6. data/lib/mojito/controllers/runtime.rb +80 -0
  7. data/lib/mojito/{matchers → controllers/runtime}/environment.rb +1 -3
  8. data/lib/mojito/{matchers → controllers/runtime}/methods.rb +1 -3
  9. data/lib/mojito/{matchers → controllers/runtime}/path.rb +4 -5
  10. data/lib/mojito/{matchers → controllers/runtime}/session.rb +1 -3
  11. data/lib/mojito/{matchers → controllers/runtime}/url_scheme.rb +1 -3
  12. data/lib/mojito/{matchers → controllers/runtime}/virtual_host.rb +1 -3
  13. data/lib/mojito/controllers/sinatra.rb +11 -0
  14. data/lib/mojito/helpers/exception_handling.rb +3 -5
  15. data/lib/mojito/helpers/shortcuts.rb +0 -2
  16. data/lib/mojito/rendering.rb +10 -6
  17. data/lib/mojito/rendering/content.rb +0 -2
  18. data/lib/mojito/rendering/content_types.rb +0 -2
  19. data/lib/mojito/rendering/delegation.rb +0 -2
  20. data/lib/mojito/rendering/file.rb +0 -2
  21. data/lib/mojito/rendering/status_codes.rb +0 -2
  22. data/lib/mojito/rendering/templates.rb +2 -4
  23. data/lib/mojito/request_extensions.rb +4 -0
  24. data/lib/mojito/utils/rspec.rb +26 -0
  25. data/spec/mojito/base_spec.rb +18 -2
  26. data/spec/mojito/controllers/method_spec.rb +56 -0
  27. data/spec/mojito/{matchers → controllers/runtime}/methods_spec.rb +4 -2
  28. data/spec/mojito/{matchers → controllers/runtime}/path_spec.rb +11 -10
  29. data/spec/mojito/{matchers → controllers/runtime}/session_spec.rb +2 -0
  30. data/spec/mojito/controllers/runtime/url_scheme_spec.rb +24 -0
  31. data/spec/mojito/controllers/runtime/virtual_host_spec.rb +24 -0
  32. data/spec/mojito/helpers_spec.rb +2 -0
  33. data/spec/mojito/rendering/content_spec.rb +4 -2
  34. data/spec/mojito/rendering/delegation_spec.rb +9 -9
  35. data/spec/mojito/rendering/file_spec.rb +9 -9
  36. data/spec/mojito/rendering/status_codes_spec.rb +6 -3
  37. data/spec/mojito/rendering/templates_spec.rb +18 -8
  38. data/spec/mojito/rendering_spec.rb +2 -0
  39. data/spec/mojito/request_extensions_spec.rb +3 -1
  40. data/spec/mojito_spec.rb +5 -3
  41. metadata +39 -35
  42. data/lib/mojito/matchers.rb +0 -23
  43. data/spec/mojito/matchers/url_scheme_spec.rb +0 -22
  44. data/spec/mojito/matchers/virtual_host_spec.rb +0 -22
  45. data/spec/mojito/matchers_spec.rb +0 -5
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
- module Mojito::Matchers
3
+ module Mojito::Controllers::Runtime
4
4
 
5
5
  module VirtualHost
6
6
 
@@ -19,6 +19,4 @@ module Mojito::Matchers
19
19
 
20
20
  end
21
21
 
22
- Mojito::PLUGINS[:virtual_host] = VirtualHost
23
-
24
22
  end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ module Mojito::Controllers
4
+
5
+ module Sinatra
6
+
7
+
8
+
9
+ end
10
+
11
+ end
@@ -23,10 +23,10 @@ module Mojito
23
23
 
24
24
  def self.included(type)
25
25
  type.instance_exec do
26
- old_dispatch = instance_method(:dispatch)
27
- define_method :dispatch do
26
+ old_call_with_handlers = method(:call_with_handlers)
27
+ define_method :call_with_handlers do
28
28
  begin
29
- old_dispatch.bind(self).call
29
+ old_call_with_handlers.bind(self).call
30
30
  rescue Exception => e
31
31
  __handle_error e
32
32
  end
@@ -79,8 +79,6 @@ module Mojito
79
79
 
80
80
  end
81
81
 
82
- Mojito::PLUGINS[:exception_handling] = ExceptionHandling
83
-
84
82
  end
85
83
 
86
84
  end
@@ -32,6 +32,4 @@ module Mojito::Helpers
32
32
 
33
33
  end
34
34
 
35
- Mojito::PLUGINS[:shortcuts] = Shortcuts
36
-
37
35
  end
@@ -9,11 +9,15 @@ module Mojito::Rendering
9
9
  require 'mojito/rendering/status_codes'
10
10
  require 'mojito/rendering/templates'
11
11
 
12
- include Content
13
- include ContentTypes
14
- include Delegation
15
- include File
16
- include StatusCodes
17
- include Templates
12
+ def self.included(type)
13
+ type.instance_exec do
14
+ include Content
15
+ include ContentTypes
16
+ include Delegation
17
+ include File
18
+ include StatusCodes
19
+ include Templates
20
+ end
21
+ end
18
22
 
19
23
  end
@@ -19,6 +19,4 @@ module Mojito::Rendering
19
19
 
20
20
  end
21
21
 
22
- Mojito::PLUGINS[:content] = Content
23
-
24
22
  end
@@ -17,6 +17,4 @@ module Mojito::Rendering
17
17
 
18
18
  end
19
19
 
20
- Mojito::PLUGINS[:content_types] = ContentTypes
21
-
22
20
  end
@@ -10,6 +10,4 @@ module Mojito::Rendering
10
10
 
11
11
  end
12
12
 
13
- Mojito::PLUGINS[:delegation] = Delegation
14
-
15
13
  end
@@ -51,6 +51,4 @@ module Mojito::Rendering
51
51
 
52
52
  end
53
53
 
54
- Mojito::PLUGINS[:file] = File
55
-
56
54
  end
@@ -35,6 +35,4 @@ module Mojito::Rendering
35
35
 
36
36
  end
37
37
 
38
- Mojito::PLUGINS[:status_codes] = StatusCodes
39
-
40
38
  end
@@ -8,13 +8,13 @@ module Mojito::Rendering
8
8
  require 'mime/types'
9
9
 
10
10
  def template(*args, &block)
11
- locals = Hash === args.last ? args.pop : self.locals
11
+ locals = Hash === args.last ? args.pop : self.request.locals
12
12
  template = if args.size == 2
13
13
  Tilt[args.first].new { args.last }
14
14
  elsif args.size == 1
15
15
  file = Where.cdir(1) + args.first
16
16
  Mojito::R::StatusCodes.instance_method(:not_found!).bind(self).call unless file.exist?
17
- if %r{\.(?<extension>\w+)\.\w+$} =~ file.to_s
17
+ if not response.include?('Content-Type') and %r{\.(?<extension>\w+)\.\w+$} =~ file.to_s
18
18
  response['Content-Type'] = MIME::Types.type_for(extension)
19
19
  end
20
20
  Tilt[file.to_s].new file.to_s
@@ -24,6 +24,4 @@ module Mojito::Rendering
24
24
 
25
25
  end
26
26
 
27
- Mojito::PLUGINS[:templates] = Templates
28
-
29
27
  end
@@ -2,6 +2,10 @@
2
2
 
3
3
  class ::Rack::Request
4
4
 
5
+ def dup
6
+ Rack::Request.new(@env.dup)
7
+ end
8
+
5
9
  def captures
6
10
  @env['mojito/captures'] ||= []
7
11
  end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec/expectations'
4
+
5
+ RSpec::Matchers.define :respond_with do |*args|
6
+ status = if Integer === args.first
7
+ args.shift
8
+ else
9
+ nil
10
+ end
11
+ headers = if Hash === args.last
12
+ args.pop
13
+ else
14
+ {}
15
+ end
16
+ body = args.first
17
+
18
+ match do |actual|
19
+ (status ? actual.status == status : true) and
20
+ (body ? actual.body == body : true) and
21
+ headers.all? do |name, value|
22
+ actual.headers[name] == value
23
+ end
24
+ end
25
+
26
+ end
@@ -7,8 +7,8 @@ require 'mojito'
7
7
  describe Mojito::Base do
8
8
 
9
9
  subject do
10
- Mojito.base_application :status_codes do
11
- on Mojito::Matchers::PATH('test.:extension') do ok! end
10
+ Mojito::C.runtime_controller Mojito::R::StatusCodes do
11
+ on PATH('test.:extension') do ok! end
12
12
  end.mock_request
13
13
  end
14
14
 
@@ -19,4 +19,20 @@ describe Mojito::Base do
19
19
  it { subject.get('/test.rb').headers.should include('Content-Type') }
20
20
  it { subject.get('/test.rb').headers['Content-Type'].should == 'application/x-ruby' }
21
21
 
22
+ context do
23
+
24
+ subject do
25
+ Class.new.tap do |c|
26
+ c.class_exec do
27
+ include Mojito::Base
28
+ end
29
+ end
30
+ end
31
+
32
+ it { subject.should respond_to(:new).with(1).argument }
33
+ it { subject.should respond_to(:call).with(1).argument }
34
+ it { subject.should respond_to(:dispatch).with(1).argument }
35
+
36
+ end
37
+
22
38
  end
@@ -0,0 +1,56 @@
1
+ # encoding: UTF-8
2
+ require 'simplecov' and SimpleCov.start do
3
+ add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
5
+ end
6
+ require 'mojito'
7
+ require 'mojito/utils/rspec'
8
+
9
+ describe Mojito::Controllers::Method do
10
+
11
+ context do
12
+ subject { Mojito::Controllers::Method }
13
+
14
+ it { subject.args_for(0, '').should == [[], ''] }
15
+ it { subject.args_for(0, 'Fred/Barney').should == [[], 'Fred/Barney'] }
16
+ it { subject.args_for(1, 'Fred').should == [['Fred'], ''] }
17
+ it { subject.args_for(1, 'Fred/Barney').should == [['Fred'], 'Barney'] }
18
+ it { subject.args_for(2, 'Fred/Barney/Wilma').should == [['Fred', 'Barney'], 'Wilma'] }
19
+ it { subject.args_for(-1, 'Fred/Barney/Wilma').should == [['Fred', 'Barney', 'Wilma'], ''] }
20
+ it { subject.args_for(-1, '').should == [[], ''] }
21
+ it { subject.args_for(-2, '').should == [nil, ''] }
22
+ it { subject.args_for(-4, 'Fred/Barney').should == [nil, 'Fred/Barney'] }
23
+
24
+ end
25
+
26
+ subject do
27
+ Mojito::Controllers.method_controller(Mojito::Rendering) do
28
+ def test_method
29
+ write 'Test method'
30
+ end
31
+ def hello(name)
32
+ write "Hello #{name}"
33
+ end
34
+ def hello_all(first_name, *names)
35
+ write "Hello #{[first_name, *names].join(', ')}"
36
+ end
37
+ end.mock_request
38
+ end
39
+
40
+ it { subject.get('/test_method').should respond_with(200, 'Test method') }
41
+ it { subject.get('/test_method/another_parameter').should respond_with(200, 'Test method') }
42
+
43
+ it { subject.get('/hello/Fred').should respond_with(200, 'Hello Fred') }
44
+ it { subject.get('/hello/Fred+Flintstone').should respond_with(200, 'Hello Fred Flintstone') }
45
+ it { subject.get('/hello/Fred/Barney').should respond_with(200, 'Hello Fred') }
46
+
47
+ it { subject.get('/hello').should respond_with(404) }
48
+
49
+ it { subject.get('/hello_all/Fred/Barney').should respond_with(200, 'Hello Fred, Barney') }
50
+ it { subject.get('/hello_all/Fred/Barney/Wilma').should respond_with(200, 'Hello Fred, Barney, Wilma') }
51
+
52
+ it { subject.get('/hello_all').should respond_with(404) }
53
+
54
+ it { subject.get('/not_defined_method').should respond_with(404) }
55
+
56
+ end
@@ -1,13 +1,15 @@
1
1
  # encoding: UTF-8
2
2
  require 'simplecov' and SimpleCov.start do
3
3
  add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
4
5
  end
5
6
  require 'mojito'
7
+ require 'mojito/utils/rspec'
6
8
 
7
- describe Mojito::Matchers::Methods do
9
+ describe Mojito::Controllers::Runtime::Methods do
8
10
 
9
11
  subject do
10
- Mojito.application Mojito::Matchers::Methods do
12
+ Mojito::C.runtime_controller Mojito::R::Content, Mojito::Controllers::Runtime::Methods do
11
13
  on GET() do write 'get' ; halt! end
12
14
  on POST() do write 'post' ; halt! end
13
15
  on HEAD() do write 'head' ; halt! end
@@ -1,15 +1,17 @@
1
1
  # encoding: UTF-8
2
2
  require 'simplecov' and SimpleCov.start do
3
3
  add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
4
5
  end
5
6
  require 'mojito'
7
+ require 'mojito/utils/rspec'
6
8
 
7
- describe Mojito::Matchers::Path do
9
+ describe Mojito::Controllers::Runtime::Path do
8
10
 
9
- subject { Mojito.application.new Rack::MockRequest.env_for('http://localhost/hello/world/rest') }
11
+ subject { Mojito::C.runtime_controller(Mojito::H::Shortcuts).new Rack::MockRequest.env_for('http://localhost/hello/world/rest') }
10
12
 
11
13
  it do
12
- subject.send(:__match?, Mojito::M::PATH('hello'))
14
+ subject.send(:__match?, subject.PATH('hello'))
13
15
  subject.captures.should be_empty
14
16
  subject.locals.should be_empty
15
17
  subject.path_info.should == '/world/rest'
@@ -23,7 +25,7 @@ describe Mojito::Matchers::Path do
23
25
  end
24
26
 
25
27
  it do
26
- subject.send(:__match?, Mojito::M::PATH('hello/:name'))
28
+ subject.send(:__match?, subject.PATH('hello/:name'))
27
29
  subject.request.captures.should == ['world']
28
30
  subject.request.locals.should == { 'name' => 'world' }
29
31
  subject.request.path_info.should == '/rest'
@@ -37,7 +39,7 @@ describe Mojito::Matchers::Path do
37
39
  end
38
40
 
39
41
  it do
40
- subject.send(:__match?, Mojito::M::PATH(%r{hello/(?<name>[^/]+)}))
42
+ subject.send(:__match?, subject.PATH(%r{hello/(?<name>[^/]+)}))
41
43
  subject.request.captures.should == ['world']
42
44
  subject.request.locals.should == { 'name' => 'world' }
43
45
  subject.request.path_info.should == '/rest'
@@ -52,7 +54,7 @@ describe Mojito::Matchers::Path do
52
54
 
53
55
  context do
54
56
  subject do
55
- Mojito.base_application Mojito::M::Path, Mojito::R::Content do
57
+ Mojito::C.runtime_controller Mojito::R::Content do
56
58
  on PATH('hello/:name') do
57
59
  on PATH('another/:name') do
58
60
  write request.locals[:name]
@@ -64,10 +66,9 @@ describe Mojito::Matchers::Path do
64
66
  end.mock_request
65
67
  end
66
68
 
67
- it { subject.get('/hello/Fred').status.should == 200 }
68
- it { subject.get('/hello/Fred').body.should == 'Fred' }
69
- it { subject.get('/hello/Fred/another/Barney').status.should == 200 }
70
- it { subject.get('/hello/Fred/another/Barney').body.should == 'Barney' }
69
+ it { subject.get('/hello/Fred').should respond_with(200, 'Fred') }
70
+ it { subject.get('/hello/Fred+Flintstone').should respond_with(200, 'Fred Flintstone') }
71
+ it { subject.get('/hello/Fred/another/Barney').should respond_with(200, 'Barney') }
71
72
 
72
73
  end
73
74
 
@@ -1,7 +1,9 @@
1
1
  # encoding: UTF-8
2
2
  require 'simplecov' and SimpleCov.start do
3
3
  add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
4
5
  end
5
6
  require 'mojito'
7
+ require 'mojito/utils/rspec'
6
8
 
7
9
 
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require 'simplecov' and SimpleCov.start do
3
+ add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
5
+ end
6
+ require 'mojito'
7
+ require 'mojito/utils/rspec'
8
+
9
+ describe Mojito::Controllers::Runtime::UrlScheme do
10
+
11
+ subject do
12
+ Mojito::C.runtime_controller Mojito::Controllers::Runtime::UrlScheme, Mojito::R::Content do
13
+ on SCHEME(:http) do write 'insecure' ; halt! end
14
+ on SCHEME(:https) do write 'secure' ; halt! end
15
+ end.mock_request
16
+ end
17
+
18
+ it { subject.get('http://localhost/').should respond_with(200, 'insecure') }
19
+ it { subject.get('http://localhost:7777/').should respond_with(200, 'insecure') }
20
+ it { subject.get('https://localhost:80/').should respond_with(200, 'secure') }
21
+ it { subject.get('https://localhost/').should respond_with(200, 'secure') }
22
+ it { subject.get('otherprotocol://test/').should respond_with(404) }
23
+
24
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require 'simplecov' and SimpleCov.start do
3
+ add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
5
+ end
6
+ require 'mojito'
7
+ require 'mojito/utils/rspec'
8
+
9
+ describe Mojito::Controllers::Runtime::VirtualHost do
10
+
11
+ subject do
12
+ Mojito::C.runtime_controller Mojito::Controllers::Runtime::VirtualHost, Mojito::R::Content do
13
+ on HOST('localhost') do write 'localhost' ; halt! end
14
+ on HOST('test:8080') do write 'test' ; halt! end
15
+ end.mock_request
16
+ end
17
+
18
+ it { subject.get('http://localhost:4444/').should respond_with(200, 'localhost') }
19
+ it { subject.get('http://localhost/').should respond_with(200, 'localhost') }
20
+ it { subject.get('http://localhost:80/').should respond_with(200, 'localhost') }
21
+ it { subject.get('http://test:8080/').should respond_with(200, 'test') }
22
+ it { subject.get('http://test/').should respond_with(404) }
23
+
24
+ end
@@ -1,8 +1,10 @@
1
1
  # encoding: UTF-8
2
2
  require 'simplecov' and SimpleCov.start do
3
3
  add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
4
5
  end
5
6
  require 'mojito'
7
+ require 'mojito/utils/rspec'
6
8
 
7
9
  describe Mojito::Helpers::ExceptionHandling do
8
10
 
@@ -1,17 +1,19 @@
1
1
  # encoding: UTF-8
2
2
  require 'simplecov' and SimpleCov.start do
3
3
  add_filter "spec/"
4
+ add_filter "lib/mojito/utils/rspec.rb"
4
5
  end
5
6
  require 'mojito'
7
+ require 'mojito/utils/rspec'
6
8
 
7
9
  describe Mojito::Rendering::Content do
8
10
 
9
11
  subject do
10
- Mojito.base_application Mojito::Rendering::Content do
12
+ Mojito::C.runtime_controller Mojito::Rendering::Content do
11
13
  on do write 'test content' ; halt! end
12
14
  end.mock_request
13
15
  end
14
16
 
15
- it { subject.get('/').body.should == 'test content' }
17
+ it { subject.get('/').should respond_with(200, 'test content') }
16
18
 
17
19
  end