nano-sharp-gem 0.0.1

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 (61) hide show
  1. checksums.yaml +7 -0
  2. data/cuba-4.0.3/CHANGELOG +71 -0
  3. data/cuba-4.0.3/CONTRIBUTING +19 -0
  4. data/cuba-4.0.3/LICENSE +23 -0
  5. data/cuba-4.0.3/README.md +781 -0
  6. data/cuba-4.0.3/cuba.gemspec +18 -0
  7. data/cuba-4.0.3/examples/config.ru +18 -0
  8. data/cuba-4.0.3/examples/measure.rb +17 -0
  9. data/cuba-4.0.3/examples/rack-response.ru +21 -0
  10. data/cuba-4.0.3/examples/views/home.mote +7 -0
  11. data/cuba-4.0.3/examples/views/layout.mote +11 -0
  12. data/cuba-4.0.3/lib/cuba/capybara.rb +13 -0
  13. data/cuba-4.0.3/lib/cuba/render.rb +63 -0
  14. data/cuba-4.0.3/lib/cuba/safe/csrf.rb +47 -0
  15. data/cuba-4.0.3/lib/cuba/safe/secure_headers.rb +43 -0
  16. data/cuba-4.0.3/lib/cuba/safe.rb +23 -0
  17. data/cuba-4.0.3/lib/cuba/test.rb +11 -0
  18. data/cuba-4.0.3/lib/cuba.rb +436 -0
  19. data/cuba-4.0.3/makefile +4 -0
  20. data/cuba-4.0.3/test/accept.rb +77 -0
  21. data/cuba-4.0.3/test/captures.rb +162 -0
  22. data/cuba-4.0.3/test/composition.rb +103 -0
  23. data/cuba-4.0.3/test/cookie.rb +34 -0
  24. data/cuba-4.0.3/test/csrf.rb +140 -0
  25. data/cuba-4.0.3/test/extension.rb +21 -0
  26. data/cuba-4.0.3/test/helper.rb +11 -0
  27. data/cuba-4.0.3/test/host.rb +29 -0
  28. data/cuba-4.0.3/test/integration.rb +114 -0
  29. data/cuba-4.0.3/test/match.rb +86 -0
  30. data/cuba-4.0.3/test/middleware.rb +47 -0
  31. data/cuba-4.0.3/test/number.rb +36 -0
  32. data/cuba-4.0.3/test/on.rb +157 -0
  33. data/cuba-4.0.3/test/param.rb +66 -0
  34. data/cuba-4.0.3/test/path.rb +86 -0
  35. data/cuba-4.0.3/test/plugin.rb +68 -0
  36. data/cuba-4.0.3/test/rack.rb +22 -0
  37. data/cuba-4.0.3/test/redirect.rb +21 -0
  38. data/cuba-4.0.3/test/render.rb +128 -0
  39. data/cuba-4.0.3/test/root.rb +83 -0
  40. data/cuba-4.0.3/test/run.rb +23 -0
  41. data/cuba-4.0.3/test/safe.rb +74 -0
  42. data/cuba-4.0.3/test/segment.rb +45 -0
  43. data/cuba-4.0.3/test/session.rb +21 -0
  44. data/cuba-4.0.3/test/settings.rb +52 -0
  45. data/cuba-4.0.3/test/views/about.erb +1 -0
  46. data/cuba-4.0.3/test/views/about.str +1 -0
  47. data/cuba-4.0.3/test/views/content-yield.erb +1 -0
  48. data/cuba-4.0.3/test/views/custom/abs_path.mote +1 -0
  49. data/cuba-4.0.3/test/views/frag.mote +1 -0
  50. data/cuba-4.0.3/test/views/home.erb +2 -0
  51. data/cuba-4.0.3/test/views/home.mote +1 -0
  52. data/cuba-4.0.3/test/views/home.str +2 -0
  53. data/cuba-4.0.3/test/views/layout-alternative.erb +2 -0
  54. data/cuba-4.0.3/test/views/layout-yield.erb +3 -0
  55. data/cuba-4.0.3/test/views/layout.erb +2 -0
  56. data/cuba-4.0.3/test/views/layout.mote +2 -0
  57. data/cuba-4.0.3/test/views/layout.str +2 -0
  58. data/cuba-4.0.3/test/views/test.erb +1 -0
  59. data/cuba-4.0.3/test/with.rb +42 -0
  60. data/nano-sharp-gem.gemspec +11 -0
  61. metadata +99 -0
@@ -0,0 +1,157 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "executes on true" do
4
+ Cuba.define do
5
+ on true do
6
+ res.write "+1"
7
+ end
8
+ end
9
+
10
+ _, _, resp = Cuba.call({})
11
+
12
+ assert_response resp, ["+1"]
13
+ end
14
+
15
+ test "executes on non-false" do
16
+ Cuba.define do
17
+ on "123" do
18
+ res.write "+1"
19
+ end
20
+ end
21
+
22
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/123", "SCRIPT_NAME" => "/" })
23
+
24
+ assert_response resp, ["+1"]
25
+ end
26
+
27
+ test "ensures SCRIPT_NAME and PATH_INFO are reverted" do
28
+ Cuba.define do
29
+ on lambda { env["SCRIPT_NAME"] = "/hello"; false } do
30
+ res.write "Unreachable"
31
+ end
32
+ end
33
+
34
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/hello" }
35
+
36
+ _, _, resp = Cuba.call(env)
37
+
38
+ assert_equal "/", env["SCRIPT_NAME"]
39
+ assert_equal "/hello", env["PATH_INFO"]
40
+ assert_response resp, []
41
+ end
42
+
43
+ test "skips consecutive matches" do
44
+ Cuba.define do
45
+ on true do
46
+ env["foo"] = "foo"
47
+
48
+ res.write "foo"
49
+ end
50
+
51
+ on true do
52
+ env["bar"] = "bar"
53
+
54
+ res.write "bar"
55
+ end
56
+ end
57
+
58
+ env = {}
59
+
60
+ _, _, resp = Cuba.call(env)
61
+
62
+ assert_equal "foo", env["foo"]
63
+ assert_response resp, ["foo"]
64
+
65
+ assert ! env["bar"]
66
+ end
67
+
68
+ test "finds first match available" do
69
+ Cuba.define do
70
+ on false do
71
+ res.write "foo"
72
+ end
73
+
74
+ on true do
75
+ res.write "bar"
76
+ end
77
+ end
78
+
79
+ _, _, resp = Cuba.call({})
80
+
81
+ assert_response resp, ["bar"]
82
+ end
83
+
84
+ test "reverts a half-met matcher" do
85
+ Cuba.define do
86
+ on "post", false do
87
+ res.write "Should be unmet"
88
+ end
89
+ end
90
+
91
+ env = { "PATH_INFO" => "/post", "SCRIPT_NAME" => "/" }
92
+ _, _, resp = Cuba.call(env)
93
+
94
+ assert_response resp, []
95
+ assert_equal "/post", env["PATH_INFO"]
96
+ assert_equal "/", env["SCRIPT_NAME"]
97
+ end
98
+
99
+ test "responds 404 if conditions are not met" do
100
+ Cuba.define do
101
+ on root do
102
+ res.write("Should be unmet")
103
+ end
104
+ end
105
+
106
+ env = { "PATH_INFO" => "/notexists", "SCRIPT_NAME" => "/" }
107
+ status, _, body = Cuba.call(env)
108
+
109
+ assert_equal 404, status
110
+ assert body.empty?
111
+ end
112
+
113
+ test "responds 404 if nested conditions are not met" do
114
+ Cuba.define do
115
+ on get do
116
+ on root do
117
+ res.write("Should be unmet")
118
+ end
119
+ end
120
+
121
+ on default do
122
+ res.write("Should be unmet")
123
+ end
124
+ end
125
+
126
+ env = {
127
+ "REQUEST_METHOD" => "GET",
128
+ "PATH_INFO" => "/notexists",
129
+ "SCRIPT_NAME" => "/"
130
+ }
131
+
132
+ status, _, body = Cuba.call(env)
133
+
134
+ assert_equal 404, status
135
+ assert body.empty?
136
+ end
137
+
138
+ test "responds 200 even with an empty body if status is set" do
139
+ Cuba.define do
140
+ on get do
141
+ on root do
142
+ res.status = 200
143
+ end
144
+ end
145
+ end
146
+
147
+ env = {
148
+ "REQUEST_METHOD" => "GET",
149
+ "PATH_INFO" => "/",
150
+ "SCRIPT_NAME" => "/"
151
+ }
152
+
153
+ status, _, body = Cuba.call(env)
154
+
155
+ assert_equal 200, status
156
+ assert body.empty?
157
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+ require "stringio"
3
+
4
+ prepare do
5
+ Cuba.define do
6
+ on get, "signup", param("email") do |email|
7
+ res.write email
8
+ end
9
+
10
+ on get, "login", param("username", "guest") do |username|
11
+ res.write username
12
+ end
13
+
14
+ on default do
15
+ res.write "No email"
16
+ end
17
+ end
18
+ end
19
+
20
+ test "yields a param" do
21
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/signup",
22
+ "SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
23
+ "QUERY_STRING" => "email=john@doe.com" }
24
+
25
+ _, _, resp = Cuba.call(env)
26
+
27
+ assert_response resp, ["john@doe.com"]
28
+ end
29
+
30
+ test "doesn't yield a missing param" do
31
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/signup",
32
+ "SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
33
+ "QUERY_STRING" => "" }
34
+
35
+ _, _, resp = Cuba.call(env)
36
+
37
+ assert_response resp, ["No email"]
38
+ end
39
+
40
+ test "doesn't yield an empty param" do
41
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/signup",
42
+ "SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
43
+ "QUERY_STRING" => "email=" }
44
+
45
+ _, _, resp = Cuba.call(env)
46
+
47
+ assert_response resp, ["No email"]
48
+ end
49
+
50
+ test "yields a default param" do
51
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/login",
52
+ "SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
53
+ "QUERY_STRING" => "username=john" }
54
+
55
+ _, _, resp = Cuba.call(env)
56
+
57
+ assert_response resp, ["john"]
58
+
59
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/login",
60
+ "SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
61
+ "QUERY_STRING" => "" }
62
+
63
+ _, _, resp = Cuba.call(env)
64
+
65
+ assert_response resp, ["guest"]
66
+ end
@@ -0,0 +1,86 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ setup do
4
+ { "SCRIPT_NAME" => "/", "PATH_INFO" => "/about" }
5
+ end
6
+
7
+ test "one level path" do |env|
8
+ Cuba.define do
9
+ on "about" do
10
+ res.write "About"
11
+ end
12
+ end
13
+
14
+ _, _, resp = Cuba.call(env)
15
+
16
+ assert_response resp, ["About"]
17
+ end
18
+
19
+ test "two level nested paths" do |env|
20
+ Cuba.define do
21
+ on "about" do
22
+ on "1" do
23
+ res.write "+1"
24
+ end
25
+
26
+ on "2" do
27
+ res.write "+2"
28
+ end
29
+ end
30
+ end
31
+
32
+ env["PATH_INFO"] = "/about/1"
33
+
34
+ _, _, resp = Cuba.call(env)
35
+
36
+ assert_response resp, ["+1"]
37
+
38
+ env["PATH_INFO"] = "/about/2"
39
+
40
+ _, _, resp = Cuba.call(env)
41
+
42
+ assert_response resp, ["+2"]
43
+ end
44
+
45
+ test "two level inlined paths" do |env|
46
+ Cuba.define do
47
+ on "a/b" do
48
+ res.write "a"
49
+ res.write "b"
50
+ end
51
+ end
52
+
53
+ env["PATH_INFO"] = "/a/b"
54
+
55
+ _, _, resp = Cuba.call(env)
56
+
57
+ assert_response resp, ["a", "b"]
58
+ end
59
+
60
+ test "a path with some regex captures" do |env|
61
+ Cuba.define do
62
+ on "user(\\d+)" do |uid|
63
+ res.write uid
64
+ end
65
+ end
66
+
67
+ env["PATH_INFO"] = "/user123"
68
+
69
+ _, _, resp = Cuba.call(env)
70
+
71
+ assert_response resp, ["123"]
72
+ end
73
+
74
+ test "matching the root" do |env|
75
+ Cuba.define do
76
+ on "" do
77
+ res.write "Home"
78
+ end
79
+ end
80
+
81
+ env["PATH_INFO"] = "/"
82
+
83
+ _, _, resp = Cuba.call(env)
84
+
85
+ assert_response resp, ["Home"]
86
+ end
@@ -0,0 +1,68 @@
1
+ require_relative "helper"
2
+
3
+ scope do
4
+ module Helper
5
+ def clean(str)
6
+ str.strip
7
+ end
8
+ end
9
+
10
+ test do
11
+ Cuba.plugin Helper
12
+
13
+ Cuba.define do
14
+ on default do
15
+ res.write clean " foo "
16
+ end
17
+ end
18
+
19
+ _, _, body = Cuba.call({})
20
+
21
+ assert_response body, ["foo"]
22
+ end
23
+ end
24
+
25
+ scope do
26
+ module Number
27
+ def num
28
+ 1
29
+ end
30
+ end
31
+
32
+ module Plugin
33
+ def self.setup(app)
34
+ app.plugin Number
35
+ end
36
+
37
+ def bar
38
+ "baz"
39
+ end
40
+
41
+ module ClassMethods
42
+ def foo
43
+ "bar"
44
+ end
45
+ end
46
+ end
47
+
48
+ setup do
49
+ Cuba.plugin Plugin
50
+
51
+ Cuba.define do
52
+ on default do
53
+ res.write bar
54
+ res.write num
55
+ end
56
+ end
57
+ end
58
+
59
+ test do
60
+ assert_equal "bar", Cuba.foo
61
+ end
62
+
63
+ test do
64
+ _, _, body = Cuba.call({})
65
+
66
+ assert_response body, ["baz", "1"]
67
+ end
68
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+ require "cuba/test"
3
+
4
+ scope do
5
+ test do
6
+ Cuba.define do
7
+ on root do
8
+ res.write "home"
9
+ end
10
+
11
+ on "about" do
12
+ res.write "about"
13
+ end
14
+ end
15
+
16
+ get "/"
17
+ assert_equal "home", last_response.body
18
+
19
+ get "/about"
20
+ assert_equal "about", last_response.body
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "redirect" do
4
+ Cuba.define do
5
+ on "hello" do
6
+ res.write "hello, world"
7
+ end
8
+
9
+ on "" do
10
+ res.redirect "/hello"
11
+ end
12
+ end
13
+
14
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
15
+
16
+ status, headers, body = Cuba.call(env)
17
+
18
+ assert_equal status, 302
19
+ assert_equal headers, { "location" => "/hello" }
20
+ assert_response body, []
21
+ end
@@ -0,0 +1,128 @@
1
+ require_relative "helper"
2
+
3
+ require "cuba/render"
4
+
5
+ test "doesn't override the settings if they already exist" do
6
+ Cuba.settings[:render] = {
7
+ :views => "./test/views",
8
+ :template_engine => "haml"
9
+ }
10
+
11
+ Cuba.plugin Cuba::Render
12
+
13
+ assert_equal "./test/views", Cuba.settings[:render][:views]
14
+ assert_equal "haml", Cuba.settings[:render][:template_engine]
15
+ end
16
+
17
+ scope do
18
+ setup do
19
+ Cuba.plugin Cuba::Render
20
+ Cuba.settings[:render][:views] = "./test/views"
21
+ Cuba.settings[:render][:template_engine] = "erb"
22
+
23
+ Cuba.define do
24
+ on "home" do
25
+ res.write view("home", name: "Agent Smith", title: "Home")
26
+ end
27
+
28
+ on "about" do
29
+ res.write partial("about", title: "About Cuba")
30
+ end
31
+
32
+ on "render" do
33
+ render("about", title: "About Cuba")
34
+ end
35
+ end
36
+ end
37
+
38
+ test "partial" do
39
+ _, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
40
+
41
+ assert_response body, ["<h1>About Cuba</h1>"]
42
+ end
43
+
44
+ test "view" do
45
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
46
+
47
+ assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
48
+ end
49
+
50
+ test "render" do
51
+ _, _, body = Cuba.call({ "PATH_INFO" => "/render", "SCRIPT_NAME" => "/" })
52
+
53
+ assert_response body, ["<title>Cuba: About Cuba</title>\n<h1>About Cuba</h1>"]
54
+ end
55
+
56
+ test "partial with str as engine" do
57
+ Cuba.settings[:render][:template_engine] = "str"
58
+
59
+ _, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
60
+
61
+ assert_response body, ["<h1>About Cuba</h1>"]
62
+ end
63
+
64
+ test "view with str as engine" do
65
+ Cuba.settings[:render][:template_engine] = "str"
66
+
67
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
68
+
69
+ assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
70
+ end
71
+
72
+ test "custom default layout support" do
73
+ Cuba.settings[:render][:layout] = "layout-alternative"
74
+
75
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
76
+
77
+ assert_response body, ["<title>Alternative Layout: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
78
+ end
79
+ end
80
+
81
+ test "caching behavior" do
82
+ Thread.current[:_cache] = nil
83
+
84
+ Cuba.plugin Cuba::Render
85
+ Cuba.settings[:render][:views] = "./test/views"
86
+
87
+ Cuba.define do
88
+ on "foo/:i" do |i|
89
+ res.write partial("test", title: i)
90
+ end
91
+ end
92
+
93
+ 10.times do |i|
94
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/foo/#{i}", "SCRIPT_NAME" => "" })
95
+ end
96
+
97
+ assert_equal 1, Thread.current[:_cache].instance_variable_get(:@cache).size
98
+ end
99
+
100
+ test "overrides layout" do
101
+ Cuba.plugin Cuba::Render
102
+ Cuba.settings[:render][:views] = "./test/views"
103
+
104
+ Cuba.define do
105
+ on true do
106
+ res.write view("home", { name: "Agent Smith", title: "Home" }, "layout-alternative")
107
+ end
108
+ end
109
+
110
+ _, _, body = Cuba.call({})
111
+
112
+ assert_response body, ["<title>Alternative Layout: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
113
+ end
114
+
115
+ test "ensures content-type header is set" do
116
+ Cuba.plugin(Cuba::Render)
117
+
118
+ Cuba.define do
119
+ on default do
120
+ res.status = 403
121
+ render("about", title: "Hello Cuba")
122
+ end
123
+ end
124
+
125
+ _, headers, _ = Cuba.call({})
126
+
127
+ assert_equal("text/html; charset=utf-8", headers["content-type"])
128
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "matching an empty segment" do
4
+ Cuba.define do
5
+ on "" do
6
+ res.write req.path
7
+ end
8
+ end
9
+
10
+ env = {
11
+ "SCRIPT_NAME" => "",
12
+ "PATH_INFO" => "/"
13
+ }
14
+
15
+ _, _, resp = Cuba.call(env)
16
+
17
+ assert_response resp, ["/"]
18
+ end
19
+
20
+ test "nested empty segments" do
21
+ Cuba.define do
22
+ on "" do
23
+ on "" do
24
+ on "1" do
25
+ res.write "IT WORKS!"
26
+ res.write req.path
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ env = {
33
+ "SCRIPT_NAME" => "",
34
+ "PATH_INFO" => "///1"
35
+ }
36
+
37
+ _, _, resp = Cuba.call(env)
38
+
39
+ assert_response resp, ["IT WORKS!", "///1"]
40
+ end
41
+
42
+ test "/events/? scenario" do
43
+ class Events < Cuba
44
+ define do
45
+ on root do
46
+ res.write "Hooray"
47
+ end
48
+ end
49
+ end
50
+
51
+ Cuba.define do
52
+ on "events" do
53
+ run Events
54
+ end
55
+ end
56
+
57
+ env = {
58
+ "SCRIPT_NAME" => "",
59
+ "PATH_INFO" => "/events"
60
+ }
61
+
62
+ _, _, resp = Cuba.call(env)
63
+
64
+ assert_response resp, ["Hooray"]
65
+
66
+ env = {
67
+ "SCRIPT_NAME" => "",
68
+ "PATH_INFO" => "/events/"
69
+ }
70
+
71
+ _, _, resp = Cuba.call(env)
72
+
73
+ assert_response resp, ["Hooray"]
74
+
75
+ env = {
76
+ "SCRIPT_NAME" => "",
77
+ "PATH_INFO" => "/events/a"
78
+ }
79
+
80
+ _, _, resp = Cuba.call(env)
81
+
82
+ assert_response resp, []
83
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "redirect canonical example" do
4
+ Cuba.define do
5
+ def redirect(*args)
6
+ run Cuba.new { on(true) { res.redirect(*args) }}
7
+ end
8
+
9
+ on "account" do
10
+ redirect "/login", 307
11
+
12
+ res.write "Super secure content"
13
+ end
14
+ end
15
+
16
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/account" }
17
+
18
+ status, headers, resp = Cuba.call(env)
19
+
20
+ assert_equal "/login", headers["location"]
21
+ assert_equal 307, status
22
+ assert_response resp, []
23
+ end