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,103 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "composing on top of a PATH" do
4
+ Services = Cuba.new do
5
+ on "services/:id" do |id|
6
+ res.write "View #{id}"
7
+ end
8
+ end
9
+
10
+ Cuba.define do
11
+ on "provider" do
12
+ run Services
13
+ end
14
+ end
15
+
16
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/provider/services/101" }
17
+
18
+ _, _, resp = Cuba.call(env)
19
+
20
+ assert_response resp, ["View 101"]
21
+ end
22
+
23
+ test "redefining not_found" do
24
+ class Users < Cuba
25
+ def not_found
26
+ res.status = 404
27
+ res.write "Not found!"
28
+ end
29
+
30
+ define do
31
+ on root do
32
+ res.write "Users"
33
+ end
34
+ end
35
+ end
36
+
37
+ class Cuba
38
+ def not_found
39
+ res.status = 404
40
+ res.write "Error 404"
41
+ end
42
+ end
43
+
44
+ Cuba.define do
45
+ on "users" do
46
+ run Users
47
+ end
48
+ end
49
+
50
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users" }
51
+
52
+ _, _, resp = Cuba.call(env)
53
+
54
+ assert_response resp, ["Users"]
55
+
56
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users/42" }
57
+
58
+ status, _, resp = Cuba.call(env)
59
+
60
+ assert_response resp, ["Not found!"]
61
+ assert_equal status, 404
62
+
63
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/guests" }
64
+
65
+ status, _, resp = Cuba.call(env)
66
+
67
+ assert_response resp, ["Error 404"]
68
+ assert_equal status, 404
69
+ end
70
+
71
+ test "multi mount" do
72
+ A = Cuba.new do
73
+ on "a" do
74
+ res.write "View a"
75
+ end
76
+ end
77
+
78
+ B = Cuba.new do
79
+ on "b" do
80
+ res.write "View b"
81
+ end
82
+ end
83
+
84
+ class Cuba
85
+ def mount(app)
86
+ result = app.call(req.env)
87
+ halt result if result[0] != 404
88
+ end
89
+ end
90
+
91
+ Cuba.define do
92
+ on default do
93
+ mount A
94
+ mount B
95
+ end
96
+ end
97
+
98
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/b" }
99
+
100
+ _, _, resp = Cuba.call(env)
101
+
102
+ assert_response resp, ["View b"]
103
+ end
@@ -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", "bar=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=bar", "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"],
33
+ headers["set-cookie"]
34
+ end
@@ -0,0 +1,140 @@
1
+ require_relative "helper"
2
+ require "cuba/safe/csrf"
3
+ require "cuba/test"
4
+
5
+ def assert_no_raise
6
+ yield
7
+ success
8
+ end
9
+
10
+ class UnsafeRequest < RuntimeError; end
11
+
12
+ scope do
13
+ setup do
14
+ Cuba.reset!
15
+
16
+ Cuba.use(Rack::Session::Cookie,
17
+ secret: "R6zSBQWz0VGVSwvT8THurhJwaVqzpnsH27J5FoI58pxoIciDQYvE4opVvDTLMyfjj7c5inIc6PDNaQWvArMvK3")
18
+ Cuba.plugin(Cuba::Safe::CSRF)
19
+ end
20
+
21
+ test "safe http methods" do
22
+ Cuba.define do
23
+ raise UnsafeRequest if csrf.unsafe?
24
+ end
25
+
26
+ assert_no_raise do
27
+ get "/"
28
+ head "/"
29
+ end
30
+ end
31
+
32
+ test "invalid csrf param" do
33
+ Cuba.define do
34
+ if csrf.unsafe?
35
+ csrf.reset!
36
+ end
37
+
38
+ res.write(csrf.token)
39
+ end
40
+
41
+ get "/"
42
+
43
+ old_token = last_response.body
44
+
45
+ post "/", "csrf_token" => "nonsense"
46
+
47
+ new_token = last_response.body
48
+
49
+ assert(old_token != new_token)
50
+ end
51
+
52
+ test "valid csrf param" do
53
+ Cuba.define do
54
+ raise unless csrf.safe?
55
+
56
+ on get do
57
+ res.write(csrf.token)
58
+ end
59
+
60
+ on post do
61
+ res.write("safe")
62
+ end
63
+ end
64
+
65
+ get "/"
66
+
67
+ csrf_token = last_response.body
68
+
69
+ assert(!csrf_token.empty?)
70
+
71
+ assert_no_raise do
72
+ post "/", "csrf_token" => csrf_token
73
+ end
74
+ end
75
+
76
+ test "http header" do
77
+ csrf_token = SecureRandom.hex(32)
78
+
79
+ Cuba.define do
80
+ session[:csrf_token] = csrf_token
81
+ raise if csrf.unsafe?
82
+ end
83
+
84
+ assert_no_raise do
85
+ post "/", {}, { "HTTP_X_CSRF_TOKEN" => csrf_token }
86
+ end
87
+ end
88
+
89
+ test "sub app raises too" do
90
+ class App < Cuba
91
+ define do
92
+ on post do
93
+ res.write("unsafe")
94
+ end
95
+ end
96
+ end
97
+
98
+ Cuba.define do
99
+ raise UnsafeRequest unless csrf.safe?
100
+
101
+ on "app" do
102
+ run(App)
103
+ end
104
+ end
105
+
106
+ assert_raise(UnsafeRequest) do
107
+ post "/app"
108
+ end
109
+ end
110
+
111
+ test "only sub app" do
112
+ class App < Cuba
113
+ define do
114
+ raise UnsafeRequest unless csrf.safe?
115
+
116
+ on post do
117
+ res.write("unsafe")
118
+ end
119
+ end
120
+ end
121
+
122
+ Cuba.define do
123
+ on "app" do
124
+ run(App)
125
+ end
126
+
127
+ on default do
128
+ res.write("safe")
129
+ end
130
+ end
131
+
132
+ assert_no_raise do
133
+ post "/"
134
+ end
135
+
136
+ assert_raise(UnsafeRequest) do
137
+ post "/app"
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ setup do
4
+ Cuba.define do
5
+ on "styles" do
6
+ on extension("css") do |file|
7
+ res.write file
8
+ end
9
+ end
10
+ end
11
+
12
+ { "SCRIPT_NAME" => "/", "PATH_INFO" => "/styles" }
13
+ end
14
+
15
+ test "/styles/reset.css" do |env|
16
+ env["PATH_INFO"] += "/reset.css"
17
+
18
+ _, _, resp = Cuba.call(env)
19
+
20
+ assert_response resp, ["reset"]
21
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
+ require "cuba"
3
+
4
+ prepare { Cuba.reset! }
5
+
6
+ def assert_response(body, expected)
7
+ arr = body.map { |line| line.strip }
8
+
9
+ flunk "#{arr.inspect} != #{expected.inspect}" unless arr == expected
10
+ print "."
11
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "matches a host" do
4
+ Cuba.define do
5
+ on host("example.com") do
6
+ res.write "worked"
7
+ end
8
+ end
9
+
10
+ env = { "HTTP_HOST" => "example.com" }
11
+
12
+ _, _, resp = Cuba.call(env)
13
+
14
+ assert_response resp, ["worked"]
15
+ end
16
+
17
+ test "matches a host with a regexp" do
18
+ Cuba.define do
19
+ on host(/example/) do
20
+ res.write "worked"
21
+ end
22
+ end
23
+
24
+ env = { "HTTP_HOST" => "example.com" }
25
+
26
+ _, _, resp = Cuba.call(env)
27
+
28
+ assert_response resp, ["worked"]
29
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "resetting" do
4
+ old = Cuba.app
5
+ assert old.object_id == Cuba.app.object_id
6
+
7
+ Cuba.reset!
8
+ assert old.object_id != Cuba.app.object_id
9
+ end
10
+
11
+ class Middle
12
+ def initialize(app, first, second, &block)
13
+ @app, @first, @second, @block = app, first, second, block
14
+ end
15
+
16
+ def call(env)
17
+ env["m.first"] = @first
18
+ env["m.second"] = @second
19
+ env["m.block"] = @block.call
20
+
21
+ @app.call(env)
22
+ end
23
+ end
24
+
25
+ test "use passes in the arguments and block" do
26
+ Cuba.use Middle, "First", "Second" do
27
+ "this is the block"
28
+ end
29
+
30
+ Cuba.define do
31
+ on get do
32
+ on "hello" do
33
+ "Default"
34
+ end
35
+ end
36
+ end
37
+
38
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello",
39
+ "SCRIPT_NAME" => "/" }
40
+
41
+ Cuba.call(env)
42
+
43
+ assert "First" == env["m.first"]
44
+ assert "Second" == env["m.second"]
45
+ assert "this is the block" == env["m.block"]
46
+ end
47
+
48
+ test "reset and use" do
49
+ Cuba.use Middle, "First", "Second" do
50
+ "this is the block"
51
+ end
52
+
53
+ Cuba.define do
54
+ on get do
55
+ on "hello" do
56
+ res.write "Default"
57
+ end
58
+ end
59
+ end
60
+
61
+ Cuba.reset!
62
+
63
+ Cuba.use Middle, "1", "2" do
64
+ "3"
65
+ end
66
+
67
+ Cuba.define do
68
+ on get do
69
+ on "hello" do
70
+ res.write "2nd Default"
71
+ end
72
+ end
73
+ end
74
+
75
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello",
76
+ "SCRIPT_NAME" => "/" }
77
+
78
+ status, headers, resp = Cuba.call(env)
79
+
80
+ assert_equal 200, status
81
+ assert "text/html; charset=utf-8" == headers["content-type"]
82
+ assert_response resp, ["2nd Default"]
83
+
84
+ assert "1" == env["m.first"]
85
+ assert "2" == env["m.second"]
86
+ assert "3" == env["m.block"]
87
+ end
88
+
89
+ test "custom response" do
90
+ class MyResponse < Cuba::Response
91
+ def foobar
92
+ write "Default"
93
+ end
94
+ end
95
+
96
+ Cuba.settings[:res] = MyResponse
97
+
98
+ Cuba.define do
99
+ on get do
100
+ on "hello" do
101
+ res.foobar
102
+ end
103
+ end
104
+ end
105
+
106
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello",
107
+ "SCRIPT_NAME" => "/" }
108
+
109
+ status, headers, resp = Cuba.call(env)
110
+
111
+ assert 200 == status
112
+ assert "text/html; charset=utf-8" == headers["content-type"]
113
+ assert_response resp, ["Default"]
114
+ end
@@ -0,0 +1,86 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ setup do
4
+ { "SCRIPT_NAME" => "/", "PATH_INFO" => "/posts/123" }
5
+ end
6
+
7
+ test "text-book example" do |env|
8
+ Cuba.define do
9
+ on "posts/:id" do |id|
10
+ res.write id
11
+ end
12
+ end
13
+
14
+ _, _, resp = Cuba.call(env)
15
+
16
+ assert_response resp, ["123"]
17
+ end
18
+
19
+ test "multi-param" do |env|
20
+ Cuba.define do
21
+ on "u/:uid/posts/:id" do |uid, id|
22
+ res.write uid
23
+ res.write id
24
+ end
25
+ end
26
+
27
+ env["PATH_INFO"] = "/u/jdoe/posts/123"
28
+
29
+ _, _, resp = Cuba.call(env)
30
+
31
+ assert_response resp, ["jdoe", "123"]
32
+ end
33
+
34
+ test "regex nesting" do |env|
35
+ Cuba.define do
36
+ on(/u\/(\w+)/) do |uid|
37
+ res.write uid
38
+
39
+ on(/posts\/(\d+)/) do |id|
40
+ res.write id
41
+ end
42
+ end
43
+ end
44
+
45
+ env["PATH_INFO"] = "/u/jdoe/posts/123"
46
+
47
+ _, _, resp = Cuba.call(env)
48
+
49
+ assert_response resp, ["jdoe", "123"]
50
+ end
51
+
52
+ test "regex nesting colon param style" do |env|
53
+ Cuba.define do
54
+ on(/u:(\w+)/) do |uid|
55
+ res.write uid
56
+
57
+ on(/posts:(\d+)/) do |id|
58
+ res.write id
59
+ end
60
+ end
61
+ end
62
+
63
+ env["PATH_INFO"] = "/u:jdoe/posts:123"
64
+
65
+ _, _, resp = Cuba.call(env)
66
+
67
+ assert_response resp, ["jdoe", "123"]
68
+ end
69
+
70
+ test "symbol matching" do |env|
71
+ Cuba.define do
72
+ on "user", :id do |uid|
73
+ res.write uid
74
+
75
+ on "posts", :pid do |id|
76
+ res.write id
77
+ end
78
+ end
79
+ end
80
+
81
+ env["PATH_INFO"] = "/user/jdoe/posts/123"
82
+
83
+ _, _, resp = Cuba.call(env)
84
+
85
+ assert_response resp, ["jdoe", "123"]
86
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ class Shrimp
4
+ def initialize(app, foo:)
5
+ @app = app
6
+ @foo = foo
7
+ end
8
+
9
+ def call(env)
10
+ status, headers, resp = @app.call(env)
11
+
12
+ arr = []
13
+ resp.each { |e| arr << e }
14
+
15
+ [status, headers, arr.reverse]
16
+ end
17
+ end
18
+
19
+ test do
20
+ class API < Cuba
21
+ use Shrimp, foo: 'bar'
22
+
23
+ define do
24
+ on "v1/test" do
25
+ res.write "OK"
26
+ res.write "1"
27
+ res.write "2"
28
+ end
29
+ end
30
+ end
31
+
32
+ Cuba.define do
33
+ on "api" do
34
+ run API
35
+ end
36
+ end
37
+
38
+ _, _, body = Cuba.call({ "PATH_INFO" => "/api/v1/test", "SCRIPT_NAME" => "/" })
39
+
40
+ arr = []
41
+
42
+ body.each do |line|
43
+ arr << line
44
+ end
45
+
46
+ assert_equal ["2", "1", "OK"], arr
47
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ setup do
4
+ { "SCRIPT_NAME" => "/", "PATH_INFO" => "/about/1/2" }
5
+ end
6
+
7
+ test "paths and numbers" do |env|
8
+ Cuba.define do
9
+ on "about" do
10
+ on :one, :two do |one, two|
11
+ res.write one
12
+ res.write two
13
+ end
14
+ end
15
+ end
16
+
17
+ _, _, resp = Cuba.call(env)
18
+
19
+ assert_response resp, ["1", "2"]
20
+ end
21
+
22
+ test "paths and decimals" do |env|
23
+ Cuba.define do
24
+ on "about" do
25
+ on(/(\d+)/) do |one|
26
+ res.write one
27
+ end
28
+ end
29
+ end
30
+
31
+ env["PATH_INFO"] = "/about/1.2"
32
+
33
+ _, _, resp = Cuba.call(env)
34
+
35
+ assert_response resp, []
36
+ end