mojito 0.1.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mojito.rb +21 -44
- data/lib/mojito/base.rb +29 -58
- data/lib/mojito/controllers.rb +10 -0
- data/lib/mojito/controllers/method.rb +65 -0
- data/lib/mojito/controllers/runtime.rb +80 -0
- data/lib/mojito/{matchers → controllers/runtime}/environment.rb +1 -3
- data/lib/mojito/{matchers → controllers/runtime}/methods.rb +1 -3
- data/lib/mojito/{matchers → controllers/runtime}/path.rb +4 -5
- data/lib/mojito/{matchers → controllers/runtime}/session.rb +1 -3
- data/lib/mojito/{matchers → controllers/runtime}/url_scheme.rb +1 -3
- data/lib/mojito/{matchers → controllers/runtime}/virtual_host.rb +1 -3
- data/lib/mojito/controllers/sinatra.rb +11 -0
- data/lib/mojito/helpers/exception_handling.rb +3 -5
- data/lib/mojito/helpers/shortcuts.rb +0 -2
- data/lib/mojito/rendering.rb +10 -6
- data/lib/mojito/rendering/content.rb +0 -2
- data/lib/mojito/rendering/content_types.rb +0 -2
- data/lib/mojito/rendering/delegation.rb +0 -2
- data/lib/mojito/rendering/file.rb +0 -2
- data/lib/mojito/rendering/status_codes.rb +0 -2
- data/lib/mojito/rendering/templates.rb +2 -4
- data/lib/mojito/request_extensions.rb +4 -0
- data/lib/mojito/utils/rspec.rb +26 -0
- data/spec/mojito/base_spec.rb +18 -2
- data/spec/mojito/controllers/method_spec.rb +56 -0
- data/spec/mojito/{matchers → controllers/runtime}/methods_spec.rb +4 -2
- data/spec/mojito/{matchers → controllers/runtime}/path_spec.rb +11 -10
- data/spec/mojito/{matchers → controllers/runtime}/session_spec.rb +2 -0
- data/spec/mojito/controllers/runtime/url_scheme_spec.rb +24 -0
- data/spec/mojito/controllers/runtime/virtual_host_spec.rb +24 -0
- data/spec/mojito/helpers_spec.rb +2 -0
- data/spec/mojito/rendering/content_spec.rb +4 -2
- data/spec/mojito/rendering/delegation_spec.rb +9 -9
- data/spec/mojito/rendering/file_spec.rb +9 -9
- data/spec/mojito/rendering/status_codes_spec.rb +6 -3
- data/spec/mojito/rendering/templates_spec.rb +18 -8
- data/spec/mojito/rendering_spec.rb +2 -0
- data/spec/mojito/request_extensions_spec.rb +3 -1
- data/spec/mojito_spec.rb +5 -3
- metadata +39 -35
- data/lib/mojito/matchers.rb +0 -23
- data/spec/mojito/matchers/url_scheme_spec.rb +0 -22
- data/spec/mojito/matchers/virtual_host_spec.rb +0 -22
- data/spec/mojito/matchers_spec.rb +0 -5
@@ -23,10 +23,10 @@ module Mojito
|
|
23
23
|
|
24
24
|
def self.included(type)
|
25
25
|
type.instance_exec do
|
26
|
-
|
27
|
-
define_method :
|
26
|
+
old_call_with_handlers = method(:call_with_handlers)
|
27
|
+
define_method :call_with_handlers do
|
28
28
|
begin
|
29
|
-
|
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
|
data/lib/mojito/rendering.rb
CHANGED
@@ -9,11 +9,15 @@ module Mojito::Rendering
|
|
9
9
|
require 'mojito/rendering/status_codes'
|
10
10
|
require 'mojito/rendering/templates'
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
@@ -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
|
@@ -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
|
data/spec/mojito/base_spec.rb
CHANGED
@@ -7,8 +7,8 @@ require 'mojito'
|
|
7
7
|
describe Mojito::Base do
|
8
8
|
|
9
9
|
subject do
|
10
|
-
Mojito.
|
11
|
-
on
|
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::
|
9
|
+
describe Mojito::Controllers::Runtime::Methods do
|
8
10
|
|
9
11
|
subject do
|
10
|
-
Mojito.
|
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::
|
9
|
+
describe Mojito::Controllers::Runtime::Path do
|
8
10
|
|
9
|
-
subject { Mojito.
|
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?,
|
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?,
|
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?,
|
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.
|
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').
|
68
|
-
it { subject.get('/hello/Fred').
|
69
|
-
it { subject.get('/hello/Fred/another/Barney').
|
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
|
|
@@ -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
|
data/spec/mojito/helpers_spec.rb
CHANGED
@@ -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.
|
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('/').
|
17
|
+
it { subject.get('/').should respond_with(200, 'test content') }
|
16
18
|
|
17
19
|
end
|