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.
@@ -1,35 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tynn
2
- # Public: Adds support for static files (javascript files, images,
3
- # stylesheets, etc).
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
- # Examples
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
- # Internal: Configures Rack::Static middleware.
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
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tynn
2
- # Public: A simple helper class to simulate requests to your application.
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("hei")
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 # => "hei"
19
+ # app.res.body # => "Hei!"
20
20
  #
21
21
  class Test
22
- # Public: Initializes a new Tynn::Test object.
23
- #
24
- # app - The application class to test (default: Tynn).
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
- # Internal: Returns the application class that handles the
39
- # mock requests. Required by Tynn::Test::InstanceMethods.
40
- def app
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::InstanceMethods
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 InstanceMethods
65
- # Public: Returns the current request object or +nil+ if no requests
66
- # have been issued yet.
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
- # Public: Returns the current response object or +nil+ if no requests
87
- # have been issued yet.
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: "alice")
80
+ # app.get("/", name: "Jane")
93
81
  #
94
82
  # app.res.status
95
83
  # # => 200
96
84
  #
97
85
  # app.res.body
98
- # # => "Hello alice!"
86
+ # # => "Hello Jane!"
99
87
  #
100
- # The returned object is an instance of
101
- # {Rack::MockResponse}[http://www.rubydoc.info/gems/rack/Rack/MockResponse]
88
+ # app.res["Content-Type"]
89
+ # # => "text/html"
102
90
  #
103
91
  def res
104
92
  @__res
105
93
  end
106
94
 
107
- # Public: Issues a GET request for the given +path+ with the given
108
- # +params+ and Rack environment.
95
+ # Issues a <tt>GET</tt> request.
109
96
  #
110
- # Examples
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: "alice")
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: Rack::GET, params: params))
107
+ request(path, env.merge(method: "GET", params: params))
117
108
  end
118
109
 
119
- # Public: Issues a POST request for the given +path+ with the given
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: "alice", password: "secret")
113
+ # app.post("/signup", username: "janedoe", password: "secret")
126
114
  #
127
115
  def post(path, params = {}, env = {})
128
- request(path, env.merge(method: "POST".freeze, params: params))
116
+ request(path, env.merge(method: "POST", params: params))
129
117
  end
130
118
 
131
- # Public: Issues a PUT request for the given +path+ with the given
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: "bob", name: "Bob")
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".freeze, params: params))
125
+ request(path, env.merge(method: "PUT", params: params))
141
126
  end
142
127
 
143
- # Public: Issues a PATCH request for the given +path+ with the given
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: "alice")
131
+ # app.patch("/users/1", username: "janedoe")
150
132
  #
151
133
  def patch(path, params = {}, env = {})
152
- request(path, env.merge(method: "PATCH".freeze, params: params))
134
+ request(path, env.merge(method: "PATCH", params: params))
153
135
  end
154
136
 
155
- # Public: Issues a DELETE request for the given +path+ with the given
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".freeze, params: params))
143
+ request(path, env.merge(method: "DELETE", params: params))
165
144
  end
166
145
 
167
- # Public: Issues a HEAD request for the given +path+ with the given
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
- # Public: Issues a OPTIONS request for the given +path+ with the given
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".freeze, params: params))
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 InstanceMethods
172
+ include Methods
200
173
  end
201
174
  end
@@ -1,8 +1,5 @@
1
- class Tynn # :nodoc: all
2
- VERSION = [
3
- MAJOR_VERSION = 1,
4
- MINOR_VERSION = 4,
5
- PATCH_VERSION = 0,
6
- PRE_VERSION = nil
7
- ].compact.join(".")
1
+ # frozen_string_literal: true
2
+
3
+ class Tynn
4
+ VERSION = "2.0.0.alpha" # :nodoc:
8
5
  end
@@ -1,14 +1,26 @@
1
- test "default headers" do
2
- Tynn.set(:default_headers, "Content-Type" => "text/plain")
1
+ # frozen_string_literal: true
3
2
 
4
- Tynn.define do
5
- root do
6
- res.write("hei")
7
- end
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
- app = Tynn::Test.new(Tynn)
11
- app.get("/")
16
+ def test_respond_with_headers
17
+ @app.set(:default_headers, "Content-Type" => "text/plain")
12
18
 
13
- assert_equal "text/plain", app.res.headers["Content-Type"]
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
@@ -1,27 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helper"
1
4
  require_relative "../lib/tynn/environment"
2
5
 
3
- test "use RACK_ENV by default" do
4
- begin
5
- old, ENV["RACK_ENV"] = ENV["RACK_ENV"], "production"
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
- Tynn.plugin(Tynn::Environment)
14
+ @app.plugin(Tynn::Environment)
15
+
16
+ assert_equal :development, @app.environment
17
+ ensure
18
+ ENV.replace(env)
19
+ end
8
20
 
9
- assert_equal(:production, Tynn.environment)
21
+ def test_with_rack_env
22
+ env, ENV["RACK_ENV"] = ENV.to_h, "test"
10
23
 
11
- assert !Tynn.development?
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["RACK_ENV"] = old
28
+ ENV.replace(env)
17
29
  end
18
- end
19
30
 
20
- test "use custom value" do
21
- Tynn.plugin(Tynn::Environment, env: "development")
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
- assert_equal(:development, Tynn.environment)
24
- assert Tynn.development?
25
- assert !Tynn.test?
26
- assert !Tynn.production?
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
@@ -1,9 +1,7 @@
1
- $VERBOSE = true
1
+ # frozen_string_literal: true
2
2
 
3
- require "cutest"
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
@@ -1,19 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helper"
1
4
  require_relative "../lib/tynn/json"
2
5
 
3
- test "json" do
4
- Tynn.plugin(Tynn::JSON)
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
- Tynn.define do
7
- root do
8
- json(name: "tynn")
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
- app = Tynn::Test.new
13
- app.get("/")
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
- json = JSON.parse(app.res.body)
37
+ ts = Tynn::Test.new(@app)
38
+ ts.get("/")
16
39
 
17
- assert_equal "tynn", json["name"]
18
- assert_equal "application/json", app.res.headers["Content-Type"]
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