cuba 3.0.0.rc2 → 3.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
data/cuba.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cuba"
3
- s.version = "3.0.0.rc2"
3
+ s.version = "3.0.0.rc3"
4
4
  s.summary = "Microframework for web applications."
5
5
  s.description = "Cuba is a microframework for web applications."
6
6
  s.authors = ["Michel Martens"]
data/lib/cuba.rb CHANGED
@@ -1,8 +1,55 @@
1
1
  require "rack"
2
+ require "time"
2
3
 
3
4
  class Cuba
4
5
  class RedefinitionError < StandardError; end
5
6
 
7
+ class Response
8
+ attr_accessor :status
9
+
10
+ attr :headers
11
+
12
+ def initialize(status = 200, headers = { "Content-Type" => "text/html" })
13
+ @status = status
14
+ @headers = headers
15
+ @body = []
16
+ @length = 0
17
+ end
18
+
19
+ def [](key)
20
+ @headers[key]
21
+ end
22
+
23
+ def []=(key, value)
24
+ @headers[key] = value
25
+ end
26
+
27
+ def write(str)
28
+ s = str.to_s
29
+
30
+ @length += s.bytesize
31
+ @headers["Content-Length"] = @length.to_s
32
+ @body << s
33
+ end
34
+
35
+ def redirect(path, status = 302)
36
+ @headers = { "Location" => path }
37
+ @status = status
38
+ end
39
+
40
+ def finish
41
+ [@status, @headers, @body]
42
+ end
43
+
44
+ def set_cookie(key, value)
45
+ Rack::Utils.set_cookie_header!(@headers, key, value)
46
+ end
47
+
48
+ def delete_cookie(key, value = {})
49
+ Rack::Utils.delete_cookie_header!(@headers, key, value)
50
+ end
51
+ end
52
+
6
53
  @@methods = []
7
54
 
8
55
  class << self
@@ -74,7 +121,7 @@ class Cuba
74
121
  def call!(env)
75
122
  @env = env
76
123
  @req = Rack::Request.new(env)
77
- @res = Rack::Response.new
124
+ @res = Cuba::Response.new
78
125
 
79
126
  # This `catch` statement will either receive a
80
127
  # rack response tuple via a `halt`, or will
data/test/accept.rb CHANGED
@@ -7,12 +7,12 @@ test "accept mimetypes" do
7
7
  end
8
8
  end
9
9
 
10
- env = { "HTTP_ACCEPT" => "application/xml",
10
+ env = { "HTTP_ACCEPT" => "application/xml",
11
11
  "SCRIPT_NAME" => "/", "PATH_INFO" => "/post" }
12
12
 
13
- _, _, resp = Cuba.call(env)
13
+ _, _, body = Cuba.call(env)
14
14
 
15
- assert_equal ["application/xml"], resp.body
15
+ assert_response body, ["application/xml"]
16
16
  end
17
17
 
18
18
  test "tests don't fail when you don't specify an accept type" do
@@ -26,7 +26,7 @@ test "tests don't fail when you don't specify an accept type" do
26
26
  end
27
27
  end
28
28
 
29
- _, _, resp = Cuba.call({})
29
+ _, _, body = Cuba.call({})
30
30
 
31
- assert_equal ["Default action"], resp.body
31
+ assert_response body, ["Default action"]
32
32
  end
data/test/captures.rb CHANGED
@@ -11,7 +11,7 @@ test "doesn't yield HOST" do
11
11
 
12
12
  _, _, resp = Cuba.call(env)
13
13
 
14
- assert_equal ["0"], resp.body
14
+ assert_response resp, ["0"]
15
15
  end
16
16
 
17
17
  test "doesn't yield the verb" do
@@ -25,7 +25,7 @@ test "doesn't yield the verb" do
25
25
 
26
26
  _, _, resp = Cuba.call(env)
27
27
 
28
- assert_equal ["0"], resp.body
28
+ assert_response resp, ["0"]
29
29
  end
30
30
 
31
31
  test "doesn't yield the path" do
@@ -40,7 +40,7 @@ test "doesn't yield the path" do
40
40
 
41
41
  _, _, resp = Cuba.call(env)
42
42
 
43
- assert_equal ["0"], resp.body
43
+ assert_response resp, ["0"]
44
44
  end
45
45
 
46
46
  test "yields the segment" do
@@ -55,7 +55,7 @@ test "yields the segment" do
55
55
 
56
56
  _, _, resp = Cuba.call(env)
57
57
 
58
- assert_equal ["johndoe"], resp.body
58
+ assert_response resp, ["johndoe"]
59
59
  end
60
60
 
61
61
  test "yields a number" do
@@ -70,7 +70,7 @@ test "yields a number" do
70
70
 
71
71
  _, _, resp = Cuba.call(env)
72
72
 
73
- assert_equal ["101"], resp.body
73
+ assert_response resp, ["101"]
74
74
  end
75
75
 
76
76
  test "yield a file name with a matching extension" do
@@ -85,7 +85,7 @@ test "yield a file name with a matching extension" do
85
85
 
86
86
  _, _, resp = Cuba.call(env)
87
87
 
88
- assert_equal ["app"], resp.body
88
+ assert_response resp, ["app"]
89
89
  end
90
90
 
91
91
  test "yields a segment per nested block" do
@@ -106,7 +106,7 @@ test "yields a segment per nested block" do
106
106
 
107
107
  _, _, resp = Cuba.call(env)
108
108
 
109
- assert_equal ["one", "two", "three"], resp.body
109
+ assert_response resp, ["one", "two", "three"]
110
110
  end
111
111
 
112
112
  test "consumes a slash if needed" do
@@ -121,5 +121,5 @@ test "consumes a slash if needed" do
121
121
 
122
122
  _, _, resp = Cuba.call(env)
123
123
 
124
- assert_equal ["foo/bar.css"], resp.body
124
+ assert_response resp, ["foo/bar.css"]
125
125
  end
data/test/composition.rb CHANGED
@@ -17,5 +17,5 @@ test "composing on top of a PATH" do
17
17
 
18
18
  _, _, resp = Cuba.call(env)
19
19
 
20
- assert_equal ["View 101"], resp.body
21
- end
20
+ assert_response resp, ["View 101"]
21
+ end
data/test/cookie.rb ADDED
@@ -0,0 +1,34 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "set cookie" do
4
+ Cuba.define do
5
+ on default do
6
+ res.set_cookie("foo", "bar")
7
+ res.set_cookie("bar", "baz")
8
+ res.write "Hello"
9
+ end
10
+ end
11
+
12
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
13
+
14
+ _, headers, body = Cuba.call(env)
15
+
16
+ assert_equal "foo=bar\nbar=baz", headers["Set-Cookie"]
17
+ end
18
+
19
+ test "delete cookie" do
20
+ Cuba.define do
21
+ on default do
22
+ res.set_cookie("foo", "bar")
23
+ res.delete_cookie("foo")
24
+ res.write "Hello"
25
+ end
26
+ end
27
+
28
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
29
+
30
+ _, headers, body = Cuba.call(env)
31
+
32
+ assert_equal "foo=; expires=Thu, 01-Jan-1970 00:00:00 GMT",
33
+ headers["Set-Cookie"]
34
+ end
data/test/extension.rb CHANGED
@@ -17,5 +17,5 @@ test "/styles/reset.css" do |env|
17
17
 
18
18
  _, _, resp = Cuba.call(env)
19
19
 
20
- assert_equal ["reset"], resp.body
20
+ assert_response resp, ["reset"]
21
21
  end
data/test/helper.rb CHANGED
@@ -7,5 +7,5 @@ def assert_response(body, expected)
7
7
  arr = []
8
8
  body.each { |line| arr << line }
9
9
 
10
- assert_equal arr, expected
11
- end
10
+ flunk "#{arr.inspect} != #{expected.inspect}" unless arr == expected
11
+ end
data/test/host.rb CHANGED
@@ -11,7 +11,7 @@ test "matches a host" do
11
11
 
12
12
  _, _, resp = Cuba.call(env)
13
13
 
14
- assert_equal ["worked"], resp.body
14
+ assert_response resp, ["worked"]
15
15
  end
16
16
 
17
17
  test "matches a host with a regexp" do
@@ -25,5 +25,5 @@ test "matches a host with a regexp" do
25
25
 
26
26
  _, _, resp = Cuba.call(env)
27
27
 
28
- assert_equal ["worked"], resp.body
28
+ assert_response resp, ["worked"]
29
29
  end
data/test/integration.rb CHANGED
@@ -79,14 +79,9 @@ test "reset and use" do
79
79
 
80
80
  assert 200 == status
81
81
  assert "text/html" == headers["Content-Type"]
82
- assert ["2nd Default"] == resp.body
82
+ assert_response resp, ["2nd Default"]
83
83
 
84
84
  assert "1" == env["m.first"]
85
85
  assert "2" == env["m.second"]
86
86
  assert "3" == env["m.block"]
87
87
  end
88
-
89
- test "examples" do
90
- `cd example && rake -I../lib`
91
- assert $?.exitstatus == 0
92
- end
data/test/match.rb CHANGED
@@ -13,7 +13,7 @@ test "text-book example" do |env|
13
13
 
14
14
  _, _, resp = Cuba.call(env)
15
15
 
16
- assert_equal ["123"], resp.body
16
+ assert_response resp, ["123"]
17
17
  end
18
18
 
19
19
  test "multi-param" do |env|
@@ -28,7 +28,7 @@ test "multi-param" do |env|
28
28
 
29
29
  _, _, resp = Cuba.call(env)
30
30
 
31
- assert_equal ["jdoe", "123"], resp.body
31
+ assert_response resp, ["jdoe", "123"]
32
32
  end
33
33
 
34
34
  test "regex nesting" do |env|
@@ -46,7 +46,7 @@ test "regex nesting" do |env|
46
46
 
47
47
  _, _, resp = Cuba.call(env)
48
48
 
49
- assert_equal ["jdoe", "123"], resp.body
49
+ assert_response resp, ["jdoe", "123"]
50
50
  end
51
51
 
52
52
  test "regex nesting colon param style" do |env|
@@ -64,7 +64,7 @@ test "regex nesting colon param style" do |env|
64
64
 
65
65
  _, _, resp = Cuba.call(env)
66
66
 
67
- assert_equal ["jdoe", "123"], resp.body
67
+ assert_response resp, ["jdoe", "123"]
68
68
  end
69
69
 
70
70
  test "symbol matching" do |env|
@@ -82,5 +82,5 @@ test "symbol matching" do |env|
82
82
 
83
83
  _, _, resp = Cuba.call(env)
84
84
 
85
- assert_equal ["jdoe", "123"], resp.body
85
+ assert_response resp, ["jdoe", "123"]
86
86
  end
data/test/middleware.rb CHANGED
@@ -8,7 +8,10 @@ class Shrimp
8
8
  def call(env)
9
9
  status, headers, resp = @app.call(env)
10
10
 
11
- [status, headers, resp.body.reverse]
11
+ arr = []
12
+ resp.each { |e| arr << e }
13
+
14
+ [status, headers, arr.reverse]
12
15
  end
13
16
  end
14
17
 
data/test/number.rb CHANGED
@@ -16,7 +16,7 @@ test "paths and numbers" do |env|
16
16
 
17
17
  _, _, resp = Cuba.call(env)
18
18
 
19
- assert_equal ["1", "2"], resp.body
19
+ assert_response resp, ["1", "2"]
20
20
  end
21
21
 
22
22
  test "paths and decimals" do |env|
@@ -32,5 +32,5 @@ test "paths and decimals" do |env|
32
32
 
33
33
  _, _, resp = Cuba.call(env)
34
34
 
35
- assert_equal [], resp.body
35
+ assert_response resp, []
36
36
  end
data/test/on.rb CHANGED
@@ -9,7 +9,7 @@ test "executes on true" do
9
9
 
10
10
  _, _, resp = Cuba.call({})
11
11
 
12
- assert_equal ["+1"], resp.body
12
+ assert_response resp, ["+1"]
13
13
  end
14
14
 
15
15
  test "executes on non-false" do
@@ -21,7 +21,7 @@ test "executes on non-false" do
21
21
 
22
22
  _, _, resp = Cuba.call({ "PATH_INFO" => "/123", "SCRIPT_NAME" => "/" })
23
23
 
24
- assert_equal ["+1"], resp.body
24
+ assert_response resp, ["+1"]
25
25
  end
26
26
 
27
27
  test "ensures SCRIPT_NAME and PATH_INFO are reverted" do
@@ -37,7 +37,7 @@ test "ensures SCRIPT_NAME and PATH_INFO are reverted" do
37
37
 
38
38
  assert_equal "/", env["SCRIPT_NAME"]
39
39
  assert_equal "/hello", env["PATH_INFO"]
40
- assert_equal [], resp.body
40
+ assert_response resp, []
41
41
  end
42
42
 
43
43
  test "skips consecutive matches" do
@@ -60,7 +60,7 @@ test "skips consecutive matches" do
60
60
  _, _, resp = Cuba.call(env)
61
61
 
62
62
  assert_equal "foo", env["foo"]
63
- assert_equal ["foo"], resp.body
63
+ assert_response resp, ["foo"]
64
64
 
65
65
  assert ! env["bar"]
66
66
  end
@@ -78,7 +78,7 @@ test "finds first match available" do
78
78
 
79
79
  _, _, resp = Cuba.call({})
80
80
 
81
- assert_equal ["bar"], resp.body
81
+ assert_response resp, ["bar"]
82
82
  end
83
83
 
84
84
  test "reverts a half-met matcher" do
@@ -91,7 +91,7 @@ test "reverts a half-met matcher" do
91
91
  env = { "PATH_INFO" => "/post", "SCRIPT_NAME" => "/" }
92
92
  _, _, resp = Cuba.call(env)
93
93
 
94
- assert_equal [], resp.body
94
+ assert_response resp, []
95
95
  assert_equal "/post", env["PATH_INFO"]
96
96
  assert_equal "/", env["SCRIPT_NAME"]
97
- end
97
+ end
data/test/param.rb CHANGED
@@ -20,7 +20,7 @@ test "yields a param" do
20
20
 
21
21
  _, _, resp = Cuba.call(env)
22
22
 
23
- assert_equal ["john@doe.com"], resp.body
23
+ assert_response resp, ["john@doe.com"]
24
24
  end
25
25
 
26
26
  test "doesn't yield a missing param" do
@@ -30,7 +30,7 @@ test "doesn't yield a missing param" do
30
30
 
31
31
  _, _, resp = Cuba.call(env)
32
32
 
33
- assert_equal ["No email"], resp.body
33
+ assert_response resp, ["No email"]
34
34
  end
35
35
 
36
36
  test "doesn't yield an empty param" do
@@ -40,5 +40,5 @@ test "doesn't yield an empty param" do
40
40
 
41
41
  _, _, resp = Cuba.call(env)
42
42
 
43
- assert_equal ["No email"], resp.body
43
+ assert_response resp, ["No email"]
44
44
  end
data/test/path.rb CHANGED
@@ -13,7 +13,7 @@ test "one level path" do |env|
13
13
 
14
14
  _, _, resp = Cuba.call(env)
15
15
 
16
- assert_equal ["About"], resp.body
16
+ assert_response resp, ["About"]
17
17
  end
18
18
 
19
19
  test "two level nested paths" do |env|
@@ -33,13 +33,13 @@ test "two level nested paths" do |env|
33
33
 
34
34
  _, _, resp = Cuba.call(env)
35
35
 
36
- assert_equal ["+1"], resp.body
36
+ assert_response resp, ["+1"]
37
37
 
38
38
  env["PATH_INFO"] = "/about/2"
39
39
 
40
40
  _, _, resp = Cuba.call(env)
41
41
 
42
- assert_equal ["+2"], resp.body
42
+ assert_response resp, ["+2"]
43
43
  end
44
44
 
45
45
  test "two level inlined paths" do |env|
@@ -54,7 +54,7 @@ test "two level inlined paths" do |env|
54
54
 
55
55
  _, _, resp = Cuba.call(env)
56
56
 
57
- assert_equal ["a", "b"], resp.body
57
+ assert_response resp, ["a", "b"]
58
58
  end
59
59
 
60
60
  test "a path with some regex captures" do |env|
@@ -68,7 +68,7 @@ test "a path with some regex captures" do |env|
68
68
 
69
69
  _, _, resp = Cuba.call(env)
70
70
 
71
- assert_equal ["123"], resp.body
71
+ assert_response resp, ["123"]
72
72
  end
73
73
 
74
74
  test "matching the root" do |env|
@@ -82,5 +82,5 @@ test "matching the root" do |env|
82
82
 
83
83
  _, _, resp = Cuba.call(env)
84
84
 
85
- assert_equal ["Home"], resp.body
86
- end
85
+ assert_response resp, ["Home"]
86
+ end
data/test/render.rb CHANGED
@@ -90,5 +90,5 @@ test "simple layout support" do
90
90
 
91
91
  _, _, resp = Cuba.call({})
92
92
 
93
- assert_equal ["Header\nThis is the actual content.\nFooter\n"], resp.body
93
+ assert_response resp, ["Header\nThis is the actual content.\nFooter\n"]
94
94
  end
data/test/root.rb CHANGED
@@ -14,7 +14,7 @@ test "matching an empty segment" do
14
14
 
15
15
  _, _, resp = Cuba.call(env)
16
16
 
17
- assert_equal ["/"], resp.body
17
+ assert_response resp, ["/"]
18
18
  end
19
19
 
20
20
  test "nested empty segments" do
@@ -36,5 +36,5 @@ test "nested empty segments" do
36
36
 
37
37
  _, _, resp = Cuba.call(env)
38
38
 
39
- assert_equal ["IT WORKS!", "///1"], resp.body
40
- end
39
+ assert_response resp, ["IT WORKS!", "///1"]
40
+ end
data/test/run.rb CHANGED
@@ -15,9 +15,9 @@ test "redirect canonical example" do
15
15
 
16
16
  env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/account" }
17
17
 
18
- _, _, resp = Cuba.call(env)
18
+ status, headers, resp = Cuba.call(env)
19
19
 
20
- assert_equal "/login", resp["Location"]
21
- assert_equal 307, resp.status
22
- assert_equal [], resp.body
23
- end
20
+ assert_equal "/login", headers["Location"]
21
+ assert_equal 307, status
22
+ assert_response resp, []
23
+ end
data/test/segment.rb CHANGED
@@ -17,7 +17,7 @@ test "matches numeric ids" do |env|
17
17
 
18
18
  _, _, resp = Cuba.call(env)
19
19
 
20
- assert_equal ["1"], resp.body
20
+ assert_response resp, ["1"]
21
21
  end
22
22
 
23
23
  test "matches decimal numbers" do |env|
@@ -25,7 +25,7 @@ test "matches decimal numbers" do |env|
25
25
 
26
26
  _, _, resp = Cuba.call(env)
27
27
 
28
- assert_equal ["1.1"], resp.body
28
+ assert_response resp, ["1.1"]
29
29
  end
30
30
 
31
31
  test "matches slugs" do |env|
@@ -33,7 +33,7 @@ test "matches slugs" do |env|
33
33
 
34
34
  _, _, resp = Cuba.call(env)
35
35
 
36
- assert_equal ["my-blog-post-about-cuba"], resp.body
36
+ assert_response resp, ["my-blog-post-about-cuba"]
37
37
  end
38
38
 
39
39
  test "matches only the first segment available" do |env|
@@ -41,5 +41,5 @@ test "matches only the first segment available" do |env|
41
41
 
42
42
  _, _, resp = Cuba.call(env)
43
43
 
44
- assert_equal ["one"], resp.body
45
- end
44
+ assert_response resp, ["one"]
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.rc2
4
+ version: 3.0.0.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000 Z
12
+ date: 2012-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2152217600 !ruby/object:Gem::Requirement
16
+ requirement: &2151932460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152217600
24
+ version_requirements: *2151932460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: cutest
27
- requirement: &2152216880 !ruby/object:Gem::Requirement
27
+ requirement: &2151931380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152216880
35
+ version_requirements: *2151931380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: capybara
38
- requirement: &2152216340 !ruby/object:Gem::Requirement
38
+ requirement: &2151929200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152216340
46
+ version_requirements: *2151929200
47
47
  description: Cuba is a microframework for web applications.
48
48
  email:
49
49
  - michel@soveran.com
@@ -62,6 +62,7 @@ files:
62
62
  - test/accept.rb
63
63
  - test/captures.rb
64
64
  - test/composition.rb
65
+ - test/cookie.rb
65
66
  - test/extension.rb
66
67
  - test/helper.rb
67
68
  - test/host.rb