syntropy 0.29.0 → 0.30.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +2 -2
- data/CHANGELOG.md +5 -0
- data/README.md +0 -2
- data/lib/syntropy/app.rb +8 -8
- data/lib/syntropy/errors.rb +13 -12
- data/lib/syntropy/http/connection.rb +396 -0
- data/lib/syntropy/http/server.rb +174 -0
- data/lib/syntropy/http/status.rb +76 -0
- data/lib/syntropy/http.rb +5 -0
- data/lib/syntropy/json_api.rb +2 -5
- data/lib/syntropy/mime_types.rb +37 -0
- data/lib/syntropy/request/mock_adapter.rb +58 -0
- data/lib/syntropy/request/request_info.rb +236 -0
- data/lib/syntropy/request/response.rb +206 -0
- data/lib/syntropy/{request_extensions.rb → request/validation.rb} +4 -173
- data/lib/syntropy/request.rb +99 -0
- data/lib/syntropy/utils.rb +1 -1
- data/lib/syntropy/version.rb +1 -1
- data/lib/syntropy.rb +3 -6
- data/syntropy.gemspec +2 -3
- data/test/app/about/_error.rb +1 -1
- data/test/app/api+.rb +1 -1
- data/test/app_custom/_site.rb +1 -1
- data/test/bm_router_proc.rb +3 -3
- data/test/helper.rb +6 -5
- data/test/test_app.rb +30 -30
- data/test/test_caching.rb +2 -2
- data/test/test_connection.rb +4 -4
- data/test/test_errors.rb +6 -6
- data/test/test_json_api.rb +10 -8
- data/test/test_mock_adapter.rb +59 -0
- data/test/test_request_info.rb +90 -0
- data/test/test_response.rb +112 -0
- data/test/test_server.rb +1 -1
- metadata +27 -17
- data/lib/syntropy/connection.rb +0 -402
- data/lib/syntropy/server.rb +0 -173
data/test/test_app.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require_relative 'helper'
|
|
4
4
|
|
|
5
5
|
class AppTest < Minitest::Test
|
|
6
|
-
|
|
6
|
+
HTTP = Syntropy::HTTP
|
|
7
7
|
|
|
8
8
|
APP_ROOT = File.join(__dir__, 'app')
|
|
9
9
|
|
|
@@ -29,27 +29,27 @@ class AppTest < Minitest::Test
|
|
|
29
29
|
|
|
30
30
|
def test_app_rendering
|
|
31
31
|
req = make_request(':method' => 'GET', ':path' => '/')
|
|
32
|
-
assert_equal
|
|
32
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
33
33
|
assert_equal 'Not found', req.response_body
|
|
34
34
|
|
|
35
35
|
req = make_request(':method' => 'HEAD', ':path' => '/')
|
|
36
|
-
assert_equal
|
|
36
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
37
37
|
assert_nil req.response_body
|
|
38
38
|
|
|
39
39
|
req = make_request(':method' => 'POST', ':path' => '/')
|
|
40
40
|
assert_equal 'Not found', req.response_body
|
|
41
|
-
assert_equal
|
|
41
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
42
42
|
|
|
43
43
|
req = make_request(':method' => 'GET', ':path' => '/test')
|
|
44
|
-
assert_equal
|
|
44
|
+
assert_equal HTTP::OK, req.response_status
|
|
45
45
|
assert_equal '<h1>Hello, world!</h1>', req.response_body
|
|
46
46
|
|
|
47
47
|
req = make_request(':method' => 'HEAD', ':path' => '/test')
|
|
48
|
-
assert_equal
|
|
48
|
+
assert_equal HTTP::OK, req.response_status
|
|
49
49
|
assert_nil req.response_body
|
|
50
50
|
|
|
51
51
|
req = make_request(':method' => 'POST', ':path' => '/test')
|
|
52
|
-
assert_equal
|
|
52
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
53
53
|
assert_equal "Method not allowed", req.response_body
|
|
54
54
|
|
|
55
55
|
req = make_request(':method' => 'GET', ':path' => '/test/assets/style.css')
|
|
@@ -57,13 +57,13 @@ class AppTest < Minitest::Test
|
|
|
57
57
|
assert_equal 'text/css', req.response_headers['Content-Type']
|
|
58
58
|
|
|
59
59
|
req = make_request(':method' => 'GET', ':path' => '/assets/style.css')
|
|
60
|
-
assert_equal
|
|
60
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
61
61
|
|
|
62
62
|
req = make_request(':method' => 'GET', ':path' => '/test/api?q=get')
|
|
63
63
|
assert_equal({ status: 'OK', response: 0 }, req.response_json)
|
|
64
64
|
|
|
65
65
|
req = make_request(':method' => 'POST', ':path' => '/test/api?q=get')
|
|
66
|
-
assert_equal
|
|
66
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
67
67
|
assert_equal({ status: 'Error', message: 'Method not allowed' }, req.response_json)
|
|
68
68
|
|
|
69
69
|
req = make_request(':method' => 'GET', ':path' => '/test/api/foo?q=get')
|
|
@@ -73,32 +73,32 @@ class AppTest < Minitest::Test
|
|
|
73
73
|
assert_equal({ status: 'OK', response: 1 }, req.response_json)
|
|
74
74
|
|
|
75
75
|
req = make_request(':method' => 'GET', ':path' => '/test/api?q=incr')
|
|
76
|
-
assert_equal
|
|
76
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
77
77
|
assert_equal({ status: 'Error', message: 'Method not allowed' }, req.response_json)
|
|
78
78
|
|
|
79
79
|
req = make_request(':method' => 'POST', ':path' => '/test/api/foo?q=incr')
|
|
80
80
|
assert_equal({ status: 'Error', message: 'Teapot' }, req.response_json)
|
|
81
|
-
assert_equal
|
|
81
|
+
assert_equal HTTP::TEAPOT, req.response_status
|
|
82
82
|
|
|
83
83
|
req = make_request(':method' => 'POST', ':path' => '/test/api/foo/bar?q=incr')
|
|
84
84
|
assert_equal({ status: 'Error', message: 'Teapot' }, req.response_json)
|
|
85
|
-
assert_equal
|
|
85
|
+
assert_equal HTTP::TEAPOT, req.response_status
|
|
86
86
|
|
|
87
87
|
req = make_request(':method' => 'GET', ':path' => '/test/bar')
|
|
88
88
|
assert_equal 'foobar', req.response_body
|
|
89
|
-
assert_equal
|
|
89
|
+
assert_equal HTTP::OK, req.response_status
|
|
90
90
|
|
|
91
91
|
req = make_request(':method' => 'POST', ':path' => '/test/bar')
|
|
92
92
|
assert_equal 'foobar', req.response_body
|
|
93
|
-
assert_equal
|
|
93
|
+
assert_equal HTTP::OK, req.response_status
|
|
94
94
|
|
|
95
95
|
req = make_request(':method' => 'GET', ':path' => '/test/baz')
|
|
96
96
|
assert_equal 'foobar', req.response_body
|
|
97
|
-
assert_equal
|
|
97
|
+
assert_equal HTTP::OK, req.response_status
|
|
98
98
|
|
|
99
99
|
req = make_request(':method' => 'POST', ':path' => '/test/baz')
|
|
100
100
|
assert_equal 'Method not allowed', req.response_body
|
|
101
|
-
assert_equal
|
|
101
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
102
102
|
|
|
103
103
|
req = make_request(':method' => 'GET', ':path' => '/test/about')
|
|
104
104
|
assert_equal 'About', req.response_body.chomp
|
|
@@ -110,7 +110,7 @@ class AppTest < Minitest::Test
|
|
|
110
110
|
assert_nil req.response_body
|
|
111
111
|
|
|
112
112
|
req = make_request(':method' => 'GET', ':path' => '/test/about/foo/bar')
|
|
113
|
-
assert_equal
|
|
113
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
114
114
|
|
|
115
115
|
req = make_request(':method' => 'GET', ':path' => '/test/params/abc')
|
|
116
116
|
assert_equal '/test/params/[foo]-abc', req.response_body.chomp
|
|
@@ -119,12 +119,12 @@ class AppTest < Minitest::Test
|
|
|
119
119
|
assert_equal '<link>foo</link>', req.response_body
|
|
120
120
|
|
|
121
121
|
req = make_request(':method' => 'GET', ':path' => '/test/bad_mod')
|
|
122
|
-
assert_equal
|
|
122
|
+
assert_equal HTTP::INTERNAL_SERVER_ERROR, req.response_status
|
|
123
123
|
end
|
|
124
124
|
|
|
125
125
|
def test_automatic_redirect_on_trailing_slash
|
|
126
126
|
req = make_request(':method' => 'GET', ':path' => '/test/rss/')
|
|
127
|
-
assert_equal
|
|
127
|
+
assert_equal HTTP::MOVED_PERMANENTLY, req.response_status
|
|
128
128
|
assert_equal '/test/rss', req.response_headers['Location']
|
|
129
129
|
end
|
|
130
130
|
|
|
@@ -146,29 +146,29 @@ class AppTest < Minitest::Test
|
|
|
146
146
|
|
|
147
147
|
def test_middleware
|
|
148
148
|
req = make_request(':method' => 'HEAD', ':path' => '/test?foo=42')
|
|
149
|
-
assert_equal
|
|
149
|
+
assert_equal HTTP::OK, req.response_status
|
|
150
150
|
assert_nil req.response_body
|
|
151
151
|
assert_equal '42', req.ctx[:foo]
|
|
152
152
|
|
|
153
153
|
req = make_request(':method' => 'HEAD', ':path' => '/test/about/raise?foo=43')
|
|
154
|
-
assert_equal
|
|
154
|
+
assert_equal HTTP::INTERNAL_SERVER_ERROR, req.response_status
|
|
155
155
|
assert_equal '<h1>Raised error</h1>', req.response_body
|
|
156
156
|
assert_equal '43', req.ctx[:foo]
|
|
157
157
|
end
|
|
158
158
|
|
|
159
159
|
def test_middleware_invocation_on_404
|
|
160
160
|
req = make_request(':method' => 'HEAD', ':path' => '/azerty?foo=bar')
|
|
161
|
-
assert_equal
|
|
161
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
162
162
|
assert_nil req.ctx[:foo]
|
|
163
163
|
|
|
164
164
|
req = make_request(':method' => 'HEAD', ':path' => '/test/azerty?foo=bar')
|
|
165
|
-
assert_equal
|
|
165
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
166
166
|
assert_equal 'bar', req.ctx[:foo]
|
|
167
167
|
end
|
|
168
168
|
end
|
|
169
169
|
|
|
170
170
|
class CustomAppTest < Minitest::Test
|
|
171
|
-
|
|
171
|
+
HTTP = Syntropy::HTTP
|
|
172
172
|
|
|
173
173
|
APP_ROOT = File.join(__dir__, 'app_custom')
|
|
174
174
|
|
|
@@ -190,12 +190,12 @@ class CustomAppTest < Minitest::Test
|
|
|
190
190
|
def test_app_with_site_rb_file
|
|
191
191
|
req = make_request(':method' => 'GET', ':path' => '/foo/bar')
|
|
192
192
|
assert_nil req.response_body
|
|
193
|
-
assert_equal
|
|
193
|
+
assert_equal HTTP::TEAPOT, req.response_status
|
|
194
194
|
end
|
|
195
195
|
end
|
|
196
196
|
|
|
197
197
|
class MultiSiteAppTest < Minitest::Test
|
|
198
|
-
|
|
198
|
+
HTTP = Syntropy::HTTP
|
|
199
199
|
|
|
200
200
|
APP_ROOT = File.join(__dir__, 'app_multi_site')
|
|
201
201
|
|
|
@@ -217,7 +217,7 @@ class MultiSiteAppTest < Minitest::Test
|
|
|
217
217
|
def test_route_by_host
|
|
218
218
|
req = make_request(':method' => 'GET', ':path' => '/', 'host' => 'blah')
|
|
219
219
|
assert_nil req.response_body
|
|
220
|
-
assert_equal
|
|
220
|
+
assert_equal HTTP::BAD_REQUEST, req.response_status
|
|
221
221
|
|
|
222
222
|
req = make_request(':method' => 'GET', ':path' => '/', 'host' => 'foo.bar')
|
|
223
223
|
assert_equal '<h1>foo.bar</h1>', req.response_body.chomp
|
|
@@ -228,7 +228,7 @@ class MultiSiteAppTest < Minitest::Test
|
|
|
228
228
|
end
|
|
229
229
|
|
|
230
230
|
class AppAPITest < Minitest::Test
|
|
231
|
-
|
|
231
|
+
HTTP = Syntropy::HTTP
|
|
232
232
|
|
|
233
233
|
APP_ROOT = File.join(__dir__, 'app')
|
|
234
234
|
|
|
@@ -292,7 +292,7 @@ class AppAPITest < Minitest::Test
|
|
|
292
292
|
end
|
|
293
293
|
|
|
294
294
|
class AppDependenciesTest < Minitest::Test
|
|
295
|
-
|
|
295
|
+
HTTP = Syntropy::HTTP
|
|
296
296
|
|
|
297
297
|
APP_ROOT = File.join(__dir__, 'app')
|
|
298
298
|
|
|
@@ -321,6 +321,6 @@ class AppDependenciesTest < Minitest::Test
|
|
|
321
321
|
|
|
322
322
|
req = make_request(':method' => 'GET', ':path' => '/test/bar')
|
|
323
323
|
assert_equal 'foobar', req.response_body
|
|
324
|
-
assert_equal
|
|
324
|
+
assert_equal HTTP::OK, req.response_status
|
|
325
325
|
end
|
|
326
326
|
end
|
data/test/test_caching.rb
CHANGED
|
@@ -4,7 +4,7 @@ require_relative 'helper'
|
|
|
4
4
|
require 'digest/sha1'
|
|
5
5
|
|
|
6
6
|
class CachingTest < Minitest::Test
|
|
7
|
-
|
|
7
|
+
HTTP = Syntropy::HTTP
|
|
8
8
|
|
|
9
9
|
APP_ROOT = File.join(__dir__, 'app')
|
|
10
10
|
|
|
@@ -40,7 +40,7 @@ class CachingTest < Minitest::Test
|
|
|
40
40
|
@app = Syntropy::App.new(**@env)
|
|
41
41
|
|
|
42
42
|
@c_fd, @s_fd = make_socket_pair
|
|
43
|
-
@adapter = Syntropy::Connection.new(nil, @machine, @s_fd, @env) { @app.(it) }
|
|
43
|
+
@adapter = Syntropy::HTTP::Connection.new(nil, @machine, @s_fd, @env) { @app.(it) }
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def teardown
|
data/test/test_connection.rb
CHANGED
|
@@ -29,7 +29,7 @@ class ConnectionTest < Minitest::Test
|
|
|
29
29
|
@hook = nil
|
|
30
30
|
@app = ->(req) { @reqs << req; @hook&.call(req) }
|
|
31
31
|
@env = {}
|
|
32
|
-
@adapter = Syntropy::Connection.new(nil, @machine, @s_fd, @env, &@app)
|
|
32
|
+
@adapter = Syntropy::HTTP::Connection.new(nil, @machine, @s_fd, @env, &@app)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def teardown
|
|
@@ -614,7 +614,7 @@ class ConnectionTest < Minitest::Test
|
|
|
614
614
|
response = read_client_side(65536)
|
|
615
615
|
expected = "HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\nSet-Cookie: foo=bar; HttpOnly\r\n\r\n3\r\nfoo\r\n0\r\n\r\n"
|
|
616
616
|
assert_equal expected, response
|
|
617
|
-
|
|
617
|
+
|
|
618
618
|
end
|
|
619
619
|
|
|
620
620
|
def test_set_cookie_multi1
|
|
@@ -628,7 +628,7 @@ class ConnectionTest < Minitest::Test
|
|
|
628
628
|
response = read_client_side(65536)
|
|
629
629
|
expected = "HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\nSet-Cookie: foo=bar; HttpOnly\r\nSet-Cookie: bar=baz\r\n\r\n3\r\nfoo\r\n0\r\n\r\n"
|
|
630
630
|
assert_equal expected, response
|
|
631
|
-
|
|
631
|
+
|
|
632
632
|
end
|
|
633
633
|
|
|
634
634
|
def test_set_cookie_multi2
|
|
@@ -644,6 +644,6 @@ class ConnectionTest < Minitest::Test
|
|
|
644
644
|
response = read_client_side(65536)
|
|
645
645
|
expected = "HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\nSet-Cookie: a=1\r\nSet-Cookie: b=2\r\nSet-Cookie: c=3\r\nSet-Cookie: d=4\r\nSet-Cookie: e=5\r\n\r\n3\r\nfoo\r\n0\r\n\r\n"
|
|
646
646
|
assert_equal expected, response
|
|
647
|
-
|
|
647
|
+
|
|
648
648
|
end
|
|
649
649
|
end
|
data/test/test_errors.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require_relative 'helper'
|
|
4
4
|
|
|
5
5
|
class ErrorsTest < Minitest::Test
|
|
6
|
-
ISE =
|
|
6
|
+
ISE = Syntropy::HTTP::INTERNAL_SERVER_ERROR
|
|
7
7
|
|
|
8
8
|
def test_error_http_status_class_method
|
|
9
9
|
e = RuntimeError.new
|
|
@@ -12,27 +12,27 @@ class ErrorsTest < Minitest::Test
|
|
|
12
12
|
e = Syntropy::Error.new
|
|
13
13
|
assert_equal ISE, Syntropy::Error.http_status(e)
|
|
14
14
|
|
|
15
|
-
e = Syntropy::Error.new("",
|
|
16
|
-
assert_equal
|
|
15
|
+
e = Syntropy::Error.new("", Syntropy::HTTP::UNAUTHORIZED)
|
|
16
|
+
assert_equal Syntropy::HTTP::UNAUTHORIZED, Syntropy::Error.http_status(e)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def test_method_not_allowed_error
|
|
20
20
|
e = Syntropy::Error.method_not_allowed('foo')
|
|
21
21
|
assert_kind_of Syntropy::Error, e
|
|
22
|
-
assert_equal
|
|
22
|
+
assert_equal Syntropy::HTTP::METHOD_NOT_ALLOWED, e.http_status
|
|
23
23
|
assert_equal 'foo', e.message
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def test_not_found_error
|
|
27
27
|
e = Syntropy::Error.not_found('bar')
|
|
28
28
|
assert_kind_of Syntropy::Error, e
|
|
29
|
-
assert_equal
|
|
29
|
+
assert_equal Syntropy::HTTP::NOT_FOUND, e.http_status
|
|
30
30
|
assert_equal 'bar', e.message
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def test_validation_error
|
|
34
34
|
e = Syntropy::ValidationError.new('baz')
|
|
35
|
-
assert_equal
|
|
35
|
+
assert_equal Syntropy::HTTP::BAD_REQUEST, e.http_status
|
|
36
36
|
assert_equal 'baz', e.message
|
|
37
37
|
end
|
|
38
38
|
end
|
data/test/test_json_api.rb
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require_relative 'helper'
|
|
4
4
|
|
|
5
5
|
class JSONAPITest < Minitest::Test
|
|
6
|
+
HTTP = Syntropy::HTTP
|
|
7
|
+
|
|
6
8
|
class TestAPI < Syntropy::JSONAPI
|
|
7
9
|
def foo(req)
|
|
8
10
|
@value
|
|
@@ -21,39 +23,39 @@ class JSONAPITest < Minitest::Test
|
|
|
21
23
|
def test_json_api
|
|
22
24
|
req = mock_req(':method' => 'GET', ':path' => '/')
|
|
23
25
|
@app.call(req)
|
|
24
|
-
assert_equal
|
|
26
|
+
assert_equal HTTP::BAD_REQUEST, req.response_status
|
|
25
27
|
|
|
26
28
|
req = mock_req(':method' => 'GET', ':path' => '/?q=foo')
|
|
27
29
|
@app.call(req)
|
|
28
|
-
assert_equal
|
|
30
|
+
assert_equal HTTP::OK, req.response_status
|
|
29
31
|
assert_equal({ status: 'OK', response: nil }, req.response_json)
|
|
30
32
|
|
|
31
33
|
req = mock_req(':method' => 'POST', ':path' => '/?q=foo')
|
|
32
34
|
@app.call(req)
|
|
33
|
-
assert_equal
|
|
35
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
34
36
|
|
|
35
37
|
|
|
36
38
|
req = mock_req(':method' => 'POST', ':path' => '/?q=bar&v=foo')
|
|
37
39
|
@app.call(req)
|
|
38
|
-
assert_equal
|
|
40
|
+
assert_equal HTTP::OK, req.response_status
|
|
39
41
|
assert_equal({ status: 'OK', response: true }, req.response_json)
|
|
40
42
|
|
|
41
43
|
req = mock_req(':method' => 'GET', ':path' => '/?q=bar&v=foo')
|
|
42
44
|
@app.call(req)
|
|
43
|
-
assert_equal
|
|
45
|
+
assert_equal HTTP::METHOD_NOT_ALLOWED, req.response_status
|
|
44
46
|
|
|
45
47
|
req = mock_req(':method' => 'GET', ':path' => '/?q=foo')
|
|
46
48
|
@app.call(req)
|
|
47
|
-
assert_equal
|
|
49
|
+
assert_equal HTTP::OK, req.response_status
|
|
48
50
|
assert_equal({ status: 'OK', response: 'foo' }, req.response_json)
|
|
49
51
|
|
|
50
52
|
req = mock_req(':method' => 'GET', ':path' => '/?q=foo')
|
|
51
53
|
@app.call(req)
|
|
52
|
-
assert_equal
|
|
54
|
+
assert_equal HTTP::OK, req.response_status
|
|
53
55
|
assert_equal({ status: 'OK', response: 'foo' }, req.response_json)
|
|
54
56
|
|
|
55
57
|
req = mock_req(':method' => 'GET', ':path' => '/?q=xxx')
|
|
56
58
|
@app.call(req)
|
|
57
|
-
assert_equal
|
|
59
|
+
assert_equal HTTP::NOT_FOUND, req.response_status
|
|
58
60
|
end
|
|
59
61
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'helper'
|
|
4
|
+
|
|
5
|
+
class MockAdapterTest < Minitest::Test
|
|
6
|
+
def test_mock_adapter
|
|
7
|
+
adapter = Syntropy::MockAdapter.new(nil)
|
|
8
|
+
req = Syntropy::Request.new({ ':path' => '/foo' }, adapter)
|
|
9
|
+
req.respond('bar', 'Content-Type' => 'baz')
|
|
10
|
+
|
|
11
|
+
assert_equal 'bar', adapter.response_body
|
|
12
|
+
assert_equal({'Content-Type' => 'baz'}, adapter.response_headers)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_mock_adapter_with_body
|
|
16
|
+
adapter = Syntropy::MockAdapter.new('barbaz')
|
|
17
|
+
req = Syntropy::Request.new({ ':path' => '/foo' }, adapter)
|
|
18
|
+
assert_equal false, req.complete?
|
|
19
|
+
|
|
20
|
+
body = req.read
|
|
21
|
+
assert_equal 'barbaz', body
|
|
22
|
+
assert_equal true, req.complete?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_mock_adapter_with_chunked_body
|
|
26
|
+
adapter = Syntropy::MockAdapter.new(['bar', 'baz'])
|
|
27
|
+
req = Syntropy::Request.new({ ':path' => '/foo' }, adapter)
|
|
28
|
+
assert_equal false, req.complete?
|
|
29
|
+
|
|
30
|
+
chunk = req.next_chunk
|
|
31
|
+
assert_equal 'bar', chunk
|
|
32
|
+
assert_equal false, req.complete?
|
|
33
|
+
|
|
34
|
+
chunk = req.next_chunk
|
|
35
|
+
assert_equal 'baz', chunk
|
|
36
|
+
assert_equal true, req.complete?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_mock_adapter_each_chunk
|
|
40
|
+
chunks = []
|
|
41
|
+
adapter = Syntropy::MockAdapter.new(['bar', 'baz'])
|
|
42
|
+
req = Syntropy::Request.new({ ':path' => '/foo' }, adapter)
|
|
43
|
+
assert_equal false, req.complete?
|
|
44
|
+
|
|
45
|
+
req.each_chunk { chunks << _1 }
|
|
46
|
+
assert_equal ['bar', 'baz'], chunks
|
|
47
|
+
assert_equal true, req.complete?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_set_response_headers
|
|
51
|
+
adapter = Syntropy::MockAdapter.new(nil)
|
|
52
|
+
req = Syntropy::Request.new({ ':path' => '/foo' }, adapter)
|
|
53
|
+
adapter.set_response_headers('Foo' => 'bar')
|
|
54
|
+
req.respond('hi', 'Bar' => 'baz')
|
|
55
|
+
|
|
56
|
+
assert_equal 'hi', adapter.response_body
|
|
57
|
+
assert_equal({'Foo' => 'bar', 'Bar' => 'baz'}, adapter.response_headers)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'helper'
|
|
4
|
+
|
|
5
|
+
class RequestInfoTest < Minitest::Test
|
|
6
|
+
def test_uri
|
|
7
|
+
r = Syntropy::MockAdapter.mock(':path' => '/test/path')
|
|
8
|
+
assert_equal '/test/path', r.path
|
|
9
|
+
assert_equal({}, r.query)
|
|
10
|
+
|
|
11
|
+
r = Syntropy::MockAdapter.mock(':path' => '/test/path?a=1&b=2&c=3%2f4')
|
|
12
|
+
assert_equal '/test/path', r.path
|
|
13
|
+
assert_equal({ a: '1', b: '2', c: '3/4' }, r.query)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_query
|
|
17
|
+
r = Syntropy::MockAdapter.mock(':path' => '/GponForm/diag_Form?images/')
|
|
18
|
+
assert_equal '/GponForm/diag_Form', r.path
|
|
19
|
+
assert_equal({:'images/' => true}, r.query)
|
|
20
|
+
|
|
21
|
+
r = Syntropy::MockAdapter.mock(':path' => '/?a=1&b=2')
|
|
22
|
+
assert_equal '/', r.path
|
|
23
|
+
assert_equal({a: '1', b: '2'}, r.query)
|
|
24
|
+
|
|
25
|
+
r = Syntropy::MockAdapter.mock(':path' => '/?l=a&t=&x=42')
|
|
26
|
+
assert_equal({l: 'a', t: '', x: '42'}, r.query)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_host
|
|
30
|
+
r = Syntropy::MockAdapter.mock(':path' => '/')
|
|
31
|
+
assert_nil r.host
|
|
32
|
+
assert_nil r.authority
|
|
33
|
+
|
|
34
|
+
r = Syntropy::MockAdapter.mock('host' => 'my.example.com')
|
|
35
|
+
assert_equal 'my.example.com', r.host
|
|
36
|
+
assert_equal 'my.example.com', r.authority
|
|
37
|
+
|
|
38
|
+
r = Syntropy::MockAdapter.mock(':authority' => 'my.foo.com')
|
|
39
|
+
assert_equal 'my.foo.com', r.host
|
|
40
|
+
assert_equal 'my.foo.com', r.authority
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_full_uri
|
|
44
|
+
r = Syntropy::MockAdapter.mock(
|
|
45
|
+
':scheme' => 'https',
|
|
46
|
+
'host' => 'foo.bar',
|
|
47
|
+
':path' => '/hey?a=b&c=d'
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
assert_equal 'https://foo.bar/hey?a=b&c=d', r.full_uri
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_cookies
|
|
54
|
+
r = Syntropy::MockAdapter.mock
|
|
55
|
+
|
|
56
|
+
assert_equal({}, r.cookies)
|
|
57
|
+
|
|
58
|
+
r = Syntropy::MockAdapter.mock(
|
|
59
|
+
'cookie' => 'uaid=a%2Fb; lastLocus=settings; signin_ref=/'
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
assert_equal({
|
|
63
|
+
'uaid' => 'a/b',
|
|
64
|
+
'lastLocus' => 'settings',
|
|
65
|
+
'signin_ref' => '/'
|
|
66
|
+
}, r.cookies)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_rewrite!
|
|
70
|
+
r = Syntropy::MockAdapter.mock(
|
|
71
|
+
':scheme' => 'https',
|
|
72
|
+
'host' => 'foo.bar',
|
|
73
|
+
':path' => '/hey/ho?a=b&c=d'
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
assert_equal '/hey/ho', r.path
|
|
77
|
+
assert_equal URI.parse('/hey/ho?a=b&c=d'), r.uri
|
|
78
|
+
assert_equal 'https://foo.bar/hey/ho?a=b&c=d', r.full_uri
|
|
79
|
+
|
|
80
|
+
r.rewrite!('/hhh', '/')
|
|
81
|
+
assert_equal '/hey/ho', r.path
|
|
82
|
+
assert_equal URI.parse('/hey/ho?a=b&c=d'), r.uri
|
|
83
|
+
assert_equal 'https://foo.bar/hey/ho?a=b&c=d', r.full_uri
|
|
84
|
+
|
|
85
|
+
r.rewrite!('/hey', '/')
|
|
86
|
+
assert_equal '/ho', r.path
|
|
87
|
+
assert_equal URI.parse('/ho?a=b&c=d'), r.uri
|
|
88
|
+
assert_equal 'https://foo.bar/ho?a=b&c=d', r.full_uri
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'helper'
|
|
4
|
+
|
|
5
|
+
class RedirectTest < Minitest::Test
|
|
6
|
+
def test_redirect
|
|
7
|
+
r = Syntropy::MockAdapter.mock
|
|
8
|
+
r.redirect('/foo')
|
|
9
|
+
|
|
10
|
+
assert_equal [
|
|
11
|
+
[:respond, r, nil, {":status"=>302, "Location"=>"/foo"}]
|
|
12
|
+
], r.adapter.calls
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_redirect_wirth_status
|
|
16
|
+
r = Syntropy::MockAdapter.mock
|
|
17
|
+
r.redirect('/bar', Syntropy::HTTP::MOVED_PERMANENTLY)
|
|
18
|
+
|
|
19
|
+
assert_equal [
|
|
20
|
+
[:respond, r, nil, {":status"=>301, "Location"=>"/bar"}]
|
|
21
|
+
], r.adapter.calls
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class StaticFileResponeTest < Minitest::Test
|
|
26
|
+
def setup
|
|
27
|
+
@path = File.join(__dir__, 'helper.rb')
|
|
28
|
+
@stat = File.stat(@path)
|
|
29
|
+
|
|
30
|
+
@etag = Syntropy::StaticFileCaching.file_stat_to_etag(@stat)
|
|
31
|
+
@last_modified = Syntropy::StaticFileCaching.file_stat_to_last_modified(@stat)
|
|
32
|
+
@content = IO.read(@path)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_serve_file_non_existent
|
|
36
|
+
r = Syntropy::MockAdapter.mock
|
|
37
|
+
r.serve_file('foo.rb', base_path: __dir__)
|
|
38
|
+
assert_equal [
|
|
39
|
+
[:respond, r, nil, { ':status' => Syntropy::HTTP::NOT_FOUND }]
|
|
40
|
+
], r.adapter.calls
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class UpgradeTest < Minitest::Test
|
|
45
|
+
def test_upgrade
|
|
46
|
+
r = Syntropy::MockAdapter.mock
|
|
47
|
+
r.upgrade('df')
|
|
48
|
+
|
|
49
|
+
assert_equal [
|
|
50
|
+
[
|
|
51
|
+
:respond,
|
|
52
|
+
r,
|
|
53
|
+
nil,
|
|
54
|
+
{
|
|
55
|
+
':status' => 101,
|
|
56
|
+
'Upgrade' => 'df',
|
|
57
|
+
'Connection' => 'upgrade'
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
:with_stream
|
|
62
|
+
]
|
|
63
|
+
], r.adapter.calls
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
r = Syntropy::MockAdapter.mock
|
|
67
|
+
r.upgrade('df', { 'foo' => 'bar' })
|
|
68
|
+
|
|
69
|
+
assert_equal [
|
|
70
|
+
[
|
|
71
|
+
:respond,
|
|
72
|
+
r,
|
|
73
|
+
nil,
|
|
74
|
+
{
|
|
75
|
+
':status' => 101,
|
|
76
|
+
'Upgrade' => 'df',
|
|
77
|
+
'Connection' => 'upgrade',
|
|
78
|
+
'foo' => 'bar'
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
:with_stream
|
|
83
|
+
]
|
|
84
|
+
], r.adapter.calls
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_websocket_upgrade
|
|
88
|
+
r = Syntropy::MockAdapter.mock(
|
|
89
|
+
'connection' => 'upgrade',
|
|
90
|
+
'upgrade' => 'websocket',
|
|
91
|
+
'sec-websocket-version' => '23',
|
|
92
|
+
'sec-websocket-key' => 'abcdefghij'
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
assert_equal 'websocket', r.upgrade_protocol
|
|
96
|
+
|
|
97
|
+
r.upgrade_to_websocket('foo' => 'baz')
|
|
98
|
+
accept = Digest::SHA1.base64digest('abcdefghij258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
|
|
99
|
+
|
|
100
|
+
assert_equal [
|
|
101
|
+
[:respond, r, nil, {
|
|
102
|
+
':status' => 101,
|
|
103
|
+
'Upgrade' => 'websocket',
|
|
104
|
+
'Connection' => 'upgrade',
|
|
105
|
+
'foo' => 'baz',
|
|
106
|
+
'Sec-WebSocket-Accept' => accept
|
|
107
|
+
}],
|
|
108
|
+
[:with_stream],
|
|
109
|
+
[:websocket_connection, r]
|
|
110
|
+
], r.adapter.calls
|
|
111
|
+
end
|
|
112
|
+
end
|
data/test/test_server.rb
CHANGED
|
@@ -26,7 +26,7 @@ class ServerTest < Minitest::Test
|
|
|
26
26
|
@machine = UM.new
|
|
27
27
|
@port = 10000 + rand(30000)
|
|
28
28
|
@env = { bind: "127.0.0.1:#{@port}" }.merge(opts)
|
|
29
|
-
@server = Syntropy::Server.new(@machine, @env) { @app&.call(it) }
|
|
29
|
+
@server = Syntropy::HTTP::Server.new(@machine, @env) { @app&.call(it) }
|
|
30
30
|
@f_server = @machine.spin { run_server }
|
|
31
31
|
|
|
32
32
|
# let server spin and listen to incoming connections
|