tynn 1.4.0 → 2.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +540 -25
- data/lib/tynn.rb +50 -103
- data/lib/tynn/base.rb +97 -0
- data/lib/tynn/default_headers.rb +50 -0
- data/lib/tynn/environment.rb +54 -28
- data/lib/tynn/json.rb +7 -18
- data/lib/tynn/render.rb +16 -12
- data/lib/tynn/request.rb +54 -38
- data/lib/tynn/response.rb +33 -173
- data/lib/tynn/secure_headers.rb +28 -31
- data/lib/tynn/session.rb +68 -34
- data/lib/tynn/settings.rb +56 -27
- data/lib/tynn/ssl.rb +78 -70
- data/lib/tynn/static.rb +12 -21
- data/lib/tynn/test.rb +51 -78
- data/lib/tynn/version.rb +4 -7
- data/test/default_headers_test.rb +21 -9
- data/test/environment_test.rb +57 -16
- data/test/helper.rb +4 -6
- data/test/json_test.rb +48 -10
- data/test/middleware_test.rb +63 -54
- data/test/plugin_test.rb +121 -0
- data/test/render_test.rb +56 -65
- data/test/request_headers_test.rb +33 -0
- data/test/routing_test.rb +111 -0
- data/test/secure_headers_test.rb +29 -17
- data/test/session_test.rb +44 -11
- data/test/settings_test.rb +53 -0
- data/test/ssl_test.rb +107 -35
- data/test/static_test.rb +25 -6
- metadata +33 -38
- data/lib/tynn/all_methods.rb +0 -50
- data/lib/tynn/hmote.rb +0 -34
- data/lib/tynn/not_found.rb +0 -20
- data/lib/tynn/protection.rb +0 -45
- data/test/all_methods_test.rb +0 -16
- data/test/core_test.rb +0 -65
- data/test/hmote_test.rb +0 -78
- data/test/not_found_test.rb +0 -19
- data/test/protection_test.rb +0 -36
data/lib/tynn/static.rb
CHANGED
@@ -1,35 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Tynn
|
2
|
-
#
|
3
|
-
#
|
4
|
+
# Serves static files (javascript files, images, stylesheets, etc).
|
5
|
+
#
|
6
|
+
# By default, these files are served from the <tt>./public</tt> folder.
|
7
|
+
# A different location can be specified through the <tt>:root</tt> option.
|
4
8
|
#
|
5
|
-
#
|
9
|
+
# Under the hood, it uses the Rack::Static middleware.
|
10
|
+
# Thus, supports all the options available by the middleware.
|
6
11
|
#
|
7
12
|
# require "tynn"
|
8
13
|
# require "tynn/static"
|
9
14
|
#
|
10
15
|
# Tynn.plugin(Tynn::Static, ["/js", "/css"])
|
11
|
-
#
|
12
|
-
# By default, serves all requests beginning with the given paths from
|
13
|
-
# the folder +public+ in the current directory (e.g. +public/js/*+,
|
14
|
-
# +public/css/*+). You can change the default by passing the +:root+
|
15
|
-
# option.
|
16
|
-
#
|
17
|
-
# Examples
|
18
|
-
#
|
19
16
|
# Tynn.plugin(Tynn::Static, ["/js", "/css"], root: "assets")
|
20
|
-
#
|
21
|
-
# Under the hood, it uses the +Rack::Static+ middleware. Thus,
|
22
|
-
# supports all the options available by the middleware. Check
|
23
|
-
# {Rack::Static}[http://www.rubydoc.info/gems/rack/Rack/Static]
|
24
|
-
# for more information.
|
25
|
-
#
|
26
|
-
# Examples
|
27
|
-
#
|
28
17
|
# Tynn.plugin(Tynn::Static, ["/js", "/css"], index: "index.html")
|
29
18
|
#
|
19
|
+
# For more information on the supported options, please see
|
20
|
+
# Rack::Static[http://www.rubydoc.info/gems/rack/Rack/Static].
|
21
|
+
#
|
30
22
|
module Static
|
31
|
-
|
32
|
-
def self.setup(app, urls, opts = {})
|
23
|
+
def self.setup(app, urls, opts = {}) # :nodoc:
|
33
24
|
options = opts.dup
|
34
25
|
|
35
26
|
options[:urls] ||= urls
|
data/lib/tynn/test.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Tynn
|
2
|
-
#
|
3
|
-
#
|
4
|
-
# Examples
|
4
|
+
# A simple helper class to simulate requests to the application.
|
5
5
|
#
|
6
6
|
# require "tynn"
|
7
7
|
# require "tynn/test"
|
8
8
|
#
|
9
9
|
# Tynn.define do
|
10
10
|
# root do
|
11
|
-
# res.write("
|
11
|
+
# res.write("Hei!")
|
12
12
|
# end
|
13
13
|
# end
|
14
14
|
#
|
@@ -16,39 +16,29 @@ class Tynn
|
|
16
16
|
# app.get("/")
|
17
17
|
#
|
18
18
|
# app.res.status # => 200
|
19
|
-
# app.res.body # => "
|
19
|
+
# app.res.body # => "Hei!"
|
20
20
|
#
|
21
21
|
class Test
|
22
|
-
|
23
|
-
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# Examples
|
22
|
+
attr_reader :app # :nodoc:
|
23
|
+
|
24
|
+
# Initializes a new Tynn::Test object.
|
27
25
|
#
|
28
26
|
# class API < Tynn
|
29
27
|
# end
|
30
28
|
#
|
31
29
|
# app = Tynn::Test.new(API)
|
32
|
-
# app.get("/json")
|
30
|
+
# app.get("/user.json")
|
33
31
|
#
|
34
32
|
def initialize(app = Tynn)
|
35
33
|
@app = app
|
36
34
|
end
|
37
35
|
|
38
|
-
#
|
39
|
-
#
|
40
|
-
|
41
|
-
return @app
|
42
|
-
end
|
43
|
-
|
44
|
-
# Internal: This module provides the Tynn::Test API methods.
|
45
|
-
# If you don't like the stand-alone version, you can integrate
|
46
|
-
# this module to your preferred testing environment.
|
47
|
-
#
|
48
|
-
# The following example uses Minitest:
|
36
|
+
# This module provides the Tynn::Test API methods. If the stand-alone
|
37
|
+
# version is not preferred, this module can be integrated into the
|
38
|
+
# testing environment. The following example uses Minitest:
|
49
39
|
#
|
50
40
|
# class HomeTest < Minitest::Test
|
51
|
-
# include Tynn::Test::
|
41
|
+
# include Tynn::Test::Methods
|
52
42
|
#
|
53
43
|
# def app
|
54
44
|
# return Tynn
|
@@ -61,113 +51,99 @@ class Tynn
|
|
61
51
|
# end
|
62
52
|
# end
|
63
53
|
#
|
64
|
-
module
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# Examples
|
54
|
+
module Methods
|
55
|
+
# If a request has been issued, returns an instance of
|
56
|
+
# Rack::Request[http://www.rubydoc.info/gems/rack/Rack/Request].
|
57
|
+
# Otherwise, returns <tt>nil</tt>.
|
69
58
|
#
|
70
59
|
# app = Tynn::Test.new
|
71
|
-
# app.get("/", {}, { "HTTP_USER_AGENT" => "Tynn::Test" })
|
60
|
+
# app.get("/", { foo: "foo" }, { "HTTP_USER_AGENT" => "Tynn::Test" })
|
72
61
|
#
|
73
62
|
# app.req.get?
|
74
63
|
# # => true
|
75
64
|
#
|
65
|
+
# app.req.params["foo"]
|
66
|
+
# # => "foo"
|
67
|
+
#
|
76
68
|
# app.req.env["HTTP_USER_AGENT"]
|
77
69
|
# # => "Tynn::Test"
|
78
70
|
#
|
79
|
-
# The returned object is an instance of
|
80
|
-
# {Rack::Request}[http://www.rubydoc.info/gems/rack/Rack/Request].
|
81
|
-
#
|
82
71
|
def req
|
83
72
|
@__req
|
84
73
|
end
|
85
74
|
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
# Examples
|
75
|
+
# If a request has been issued, returns an instance of
|
76
|
+
# Rack::MockResponse[http://www.rubydoc.info/gems/rack/Rack/MockResponse].
|
77
|
+
# Otherwise, returns <tt>nil</tt>.
|
90
78
|
#
|
91
79
|
# app = Tynn::Test.new
|
92
|
-
# app.get("/", name: "
|
80
|
+
# app.get("/", name: "Jane")
|
93
81
|
#
|
94
82
|
# app.res.status
|
95
83
|
# # => 200
|
96
84
|
#
|
97
85
|
# app.res.body
|
98
|
-
# # => "Hello
|
86
|
+
# # => "Hello Jane!"
|
99
87
|
#
|
100
|
-
#
|
101
|
-
#
|
88
|
+
# app.res["Content-Type"]
|
89
|
+
# # => "text/html"
|
102
90
|
#
|
103
91
|
def res
|
104
92
|
@__res
|
105
93
|
end
|
106
94
|
|
107
|
-
#
|
108
|
-
# +params+ and Rack environment.
|
95
|
+
# Issues a <tt>GET</tt> request.
|
109
96
|
#
|
110
|
-
#
|
97
|
+
# [path] A request path.
|
98
|
+
# [params] A Hash of query/post parameters, a String request body,
|
99
|
+
# or <tt>nil</tt>.
|
100
|
+
# [env] A Hash of Rack environment values.
|
111
101
|
#
|
112
102
|
# app = Tynn::Test.new
|
113
|
-
# app.get("/search", name: "
|
103
|
+
# app.get("/search", name: "jane")
|
104
|
+
# app.get("/cart", {}, { "HTTPS" => "on" })
|
114
105
|
#
|
115
106
|
def get(path, params = {}, env = {})
|
116
|
-
request(path, env.merge(method:
|
107
|
+
request(path, env.merge(method: "GET", params: params))
|
117
108
|
end
|
118
109
|
|
119
|
-
#
|
120
|
-
# +params+ and Rack environment.
|
121
|
-
#
|
122
|
-
# Examples
|
110
|
+
# Issues a <tt>POST</tt> request. See #get for more information.
|
123
111
|
#
|
124
112
|
# app = Tynn::Test.new
|
125
|
-
# app.post("/signup", username: "
|
113
|
+
# app.post("/signup", username: "janedoe", password: "secret")
|
126
114
|
#
|
127
115
|
def post(path, params = {}, env = {})
|
128
|
-
request(path, env.merge(method: "POST"
|
116
|
+
request(path, env.merge(method: "POST", params: params))
|
129
117
|
end
|
130
118
|
|
131
|
-
#
|
132
|
-
# +params+ and Rack environment.
|
133
|
-
#
|
134
|
-
# Examples
|
119
|
+
# Issues a <tt>PUT</tt> request. See #get for more information.
|
135
120
|
#
|
136
121
|
# app = Tynn::Test.new
|
137
|
-
# app.put("/users/1", username: "
|
122
|
+
# app.put("/users/1", username: "johndoe", name: "John")
|
138
123
|
#
|
139
124
|
def put(path, params = {}, env = {})
|
140
|
-
request(path, env.merge(method: "PUT"
|
125
|
+
request(path, env.merge(method: "PUT", params: params))
|
141
126
|
end
|
142
127
|
|
143
|
-
#
|
144
|
-
# +params+ and Rack environment.
|
145
|
-
#
|
146
|
-
# Examples
|
128
|
+
# Issues a <tt>PATCH</tt> request. See #get for more information.
|
147
129
|
#
|
148
130
|
# app = Tynn::Test.new
|
149
|
-
# app.patch("/users/1", username: "
|
131
|
+
# app.patch("/users/1", username: "janedoe")
|
150
132
|
#
|
151
133
|
def patch(path, params = {}, env = {})
|
152
|
-
request(path, env.merge(method: "PATCH"
|
134
|
+
request(path, env.merge(method: "PATCH", params: params))
|
153
135
|
end
|
154
136
|
|
155
|
-
#
|
156
|
-
# +params+ and Rack environment.
|
157
|
-
#
|
158
|
-
# Examples
|
137
|
+
# Issues a <tt>DELETE</tt> request. See #get for more information.
|
159
138
|
#
|
160
139
|
# app = Tynn::Test.new
|
161
140
|
# app.delete("/users/1")
|
162
141
|
#
|
163
142
|
def delete(path, params = {}, env = {})
|
164
|
-
request(path, env.merge(method: "DELETE"
|
143
|
+
request(path, env.merge(method: "DELETE", params: params))
|
165
144
|
end
|
166
145
|
|
167
|
-
#
|
168
|
-
# +params+ and Rack environment.
|
169
|
-
#
|
170
|
-
# Examples
|
146
|
+
# Issues a <tt>HEAD</tt> request. See #get for more information.
|
171
147
|
#
|
172
148
|
# app = Tynn::Test.new
|
173
149
|
# app.head("/users/1")
|
@@ -176,16 +152,13 @@ class Tynn
|
|
176
152
|
request(path, env.merge(method: Rack::HEAD, params: params))
|
177
153
|
end
|
178
154
|
|
179
|
-
#
|
180
|
-
# +params+ and Rack environment.
|
181
|
-
#
|
182
|
-
# Examples
|
155
|
+
# Issues a <tt>OPTIONS</tt> request. See #get for more information.
|
183
156
|
#
|
184
157
|
# app = Tynn::Test.new
|
185
158
|
# app.options("/users")
|
186
159
|
#
|
187
160
|
def options(path, params = {}, env = {})
|
188
|
-
request(path, env.merge(method: "OPTIONS"
|
161
|
+
request(path, env.merge(method: "OPTIONS", params: params))
|
189
162
|
end
|
190
163
|
|
191
164
|
private
|
@@ -196,6 +169,6 @@ class Tynn
|
|
196
169
|
end
|
197
170
|
end
|
198
171
|
|
199
|
-
include
|
172
|
+
include Methods
|
200
173
|
end
|
201
174
|
end
|
data/lib/tynn/version.rb
CHANGED
@@ -1,14 +1,26 @@
|
|
1
|
-
|
2
|
-
Tynn.set(:default_headers, "Content-Type" => "text/plain")
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
require_relative "helper"
|
4
|
+
|
5
|
+
class DefaultHeadersTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@app = Class.new(Tynn)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_set_and_get_headers
|
11
|
+
@app.set(:default_headers, "Content-Type" => "text/plain")
|
12
|
+
|
13
|
+
assert_equal "text/plain", @app.default_headers["Content-Type"]
|
8
14
|
end
|
9
15
|
|
10
|
-
|
11
|
-
|
16
|
+
def test_respond_with_headers
|
17
|
+
@app.set(:default_headers, "Content-Type" => "text/plain")
|
12
18
|
|
13
|
-
|
19
|
+
@app.define {}
|
20
|
+
|
21
|
+
ts = Tynn::Test.new(@app)
|
22
|
+
ts.get("/")
|
23
|
+
|
24
|
+
assert_equal @app.default_headers, ts.res.headers
|
25
|
+
end
|
14
26
|
end
|
data/test/environment_test.rb
CHANGED
@@ -1,27 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "helper"
|
1
4
|
require_relative "../lib/tynn/environment"
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
class EnvironmentTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@app = Class.new(Tynn)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_without_rack_env
|
12
|
+
env, ENV["RACK_ENV"] = ENV.to_h, nil
|
6
13
|
|
7
|
-
|
14
|
+
@app.plugin(Tynn::Environment)
|
15
|
+
|
16
|
+
assert_equal :development, @app.environment
|
17
|
+
ensure
|
18
|
+
ENV.replace(env)
|
19
|
+
end
|
8
20
|
|
9
|
-
|
21
|
+
def test_with_rack_env
|
22
|
+
env, ENV["RACK_ENV"] = ENV.to_h, "test"
|
10
23
|
|
11
|
-
|
12
|
-
assert !Tynn.test?
|
13
|
-
assert Tynn.production?
|
24
|
+
@app.plugin(Tynn::Environment)
|
14
25
|
|
26
|
+
assert_equal :test, @app.environment
|
15
27
|
ensure
|
16
|
-
ENV
|
28
|
+
ENV.replace(env)
|
17
29
|
end
|
18
|
-
end
|
19
30
|
|
20
|
-
|
21
|
-
|
31
|
+
def test_set_environment
|
32
|
+
@app.plugin(Tynn::Environment, env: :development)
|
33
|
+
|
34
|
+
@app.set(:environment, :test)
|
35
|
+
|
36
|
+
assert_equal :test, @app.environment
|
37
|
+
end
|
22
38
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
39
|
+
def test_predicate_methods
|
40
|
+
@app.plugin(Tynn::Environment, env: :development)
|
41
|
+
|
42
|
+
assert_equal true, @app.development?
|
43
|
+
assert_equal false, @app.production?
|
44
|
+
assert_equal false, @app.test?
|
45
|
+
assert_equal false, @app.staging?
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_configure
|
49
|
+
@app.plugin(Tynn::Environment)
|
50
|
+
|
51
|
+
@app.set(:environment, :test)
|
52
|
+
|
53
|
+
@app.configure(:test) do
|
54
|
+
@app.set(:test, true)
|
55
|
+
end
|
56
|
+
|
57
|
+
@app.configure(:development, :test) do |app|
|
58
|
+
app.set(:production, false)
|
59
|
+
end
|
60
|
+
|
61
|
+
@app.configure(:production) do
|
62
|
+
raise "This should not be executed"
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_equal true, @app.settings[:test]
|
66
|
+
assert_equal false, @app.settings[:production]
|
67
|
+
end
|
27
68
|
end
|
data/test/helper.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "bundler/setup"
|
4
|
+
require "minitest/autorun"
|
5
|
+
require "minitest/pride"
|
4
6
|
require_relative "../lib/tynn"
|
5
7
|
require_relative "../lib/tynn/test"
|
6
|
-
|
7
|
-
prepare do
|
8
|
-
Tynn.reset!
|
9
|
-
end
|
data/test/json_test.rb
CHANGED
@@ -1,19 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "helper"
|
1
4
|
require_relative "../lib/tynn/json"
|
2
5
|
|
3
|
-
|
4
|
-
|
6
|
+
class JSONTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@app = Class.new(Tynn)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_respond_json_object
|
12
|
+
@app.plugin(Tynn::JSON)
|
5
13
|
|
6
|
-
|
7
|
-
|
8
|
-
|
14
|
+
@app.define do
|
15
|
+
get do
|
16
|
+
json(foo: "foo")
|
17
|
+
end
|
9
18
|
end
|
19
|
+
|
20
|
+
ts = Tynn::Test.new(@app)
|
21
|
+
ts.get("/")
|
22
|
+
|
23
|
+
object = JSON.parse(ts.res.body)
|
24
|
+
|
25
|
+
assert_equal "foo", object["foo"]
|
10
26
|
end
|
11
27
|
|
12
|
-
|
13
|
-
|
28
|
+
def test_respond_json_array
|
29
|
+
@app.plugin(Tynn::JSON)
|
30
|
+
|
31
|
+
@app.define do
|
32
|
+
get do
|
33
|
+
json(%w(foo bar baz))
|
34
|
+
end
|
35
|
+
end
|
14
36
|
|
15
|
-
|
37
|
+
ts = Tynn::Test.new(@app)
|
38
|
+
ts.get("/")
|
16
39
|
|
17
|
-
|
18
|
-
|
40
|
+
assert_equal %w(foo bar baz), JSON.parse(ts.res.body)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_content_type
|
44
|
+
@app.plugin(Tynn::JSON)
|
45
|
+
|
46
|
+
@app.define do
|
47
|
+
get do
|
48
|
+
json(ok: true)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ts = Tynn::Test.new(@app)
|
53
|
+
ts.get("/")
|
54
|
+
|
55
|
+
assert_equal "application/json", ts.res.content_type
|
56
|
+
end
|
19
57
|
end
|