nyara 0.0.1.pre.5 → 0.0.1.pre.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/factorial.rb +19 -0
- data/ext/accept.c +2 -2
- data/ext/event.c +48 -23
- data/ext/extconf.rb +2 -0
- data/ext/hashes.c +28 -3
- data/ext/http-parser/http_parser.h +1 -0
- data/ext/nyara.c +20 -3
- data/ext/nyara.h +13 -2
- data/ext/request.c +90 -13
- data/ext/request.h +8 -2
- data/ext/request_parse.c +135 -6
- data/ext/route.cc +7 -10
- data/ext/test_response.c +155 -0
- data/ext/url_encoded.c +0 -5
- data/lib/nyara/config.rb +5 -0
- data/lib/nyara/controller.rb +91 -28
- data/lib/nyara/cookie.rb +7 -0
- data/lib/nyara/flash.rb +23 -0
- data/lib/nyara/hashes/header_hash.rb +2 -0
- data/lib/nyara/nyara.rb +14 -2
- data/lib/nyara/part.rb +156 -0
- data/lib/nyara/patches/array.rb +5 -0
- data/lib/nyara/patches/blank.rb +128 -0
- data/lib/nyara/patches/json.rb +15 -0
- data/lib/nyara/patches/mini_support.rb +6 -0
- data/lib/nyara/patches/string.rb +21 -0
- data/lib/nyara/patches/to_query.rb +113 -0
- data/lib/nyara/request.rb +13 -15
- data/lib/nyara/route.rb +15 -80
- data/lib/nyara/route_entry.rb +69 -2
- data/lib/nyara/session.rb +66 -21
- data/lib/nyara/test.rb +170 -0
- data/lib/nyara/view.rb +5 -6
- data/lib/nyara.rb +7 -6
- data/nyara.gemspec +2 -2
- data/rakefile +34 -4
- data/readme.md +8 -1
- data/spec/config_spec.rb +28 -0
- data/spec/cpu_counter_spec.rb +9 -0
- data/spec/evented_io_spec.rb +1 -0
- data/spec/flash_spec.rb +29 -0
- data/spec/hashes_spec.rb +8 -0
- data/spec/mini_support_spec.rb +54 -0
- data/spec/part_spec.rb +52 -0
- data/spec/path_helper_spec.rb +22 -14
- data/spec/request_delegate_spec.rb +19 -11
- data/spec/route_entry_spec.rb +55 -0
- data/spec/session_spec.rb +69 -7
- data/spec/spec_helper.rb +3 -0
- data/spec/test_spec.rb +58 -0
- data/tools/hello.rb +11 -3
- data/tools/memcheck.rb +33 -0
- data/tools/s.rb +11 -0
- metadata +23 -7
- data/example/design.rb +0 -62
- data/example/fib.rb +0 -15
- data/spec/route_spec.rb +0 -84
- /data/ext/inc/{status_codes.inc → status_codes.h} +0 -0
data/readme.md
CHANGED
@@ -2,6 +2,7 @@ Not Yet Another Ruby Async web framework and server.
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/luikore/nyara.png)](https://travis-ci.org/luikore/nyara)
|
4
4
|
|
5
|
+
- Fast
|
5
6
|
- Evented IO while API remains synchrony
|
6
7
|
- Prefork production server, mixing a bit blocking operations won't block other users
|
7
8
|
- Sinatra-like http method and scanf-like http path and path helper
|
@@ -39,6 +40,7 @@ ruby nyahaha.rb
|
|
39
40
|
|
40
41
|
# Documentation
|
41
42
|
|
43
|
+
- [Wiki](https://github.com/luikore/nyara/wiki/Home)
|
42
44
|
- [Manual](https://github.com/luikore/nyara/wiki/Manual)
|
43
45
|
- [API doc](http://rubydoc.info/github/luikore/nyara/master/frames)
|
44
46
|
- [Building from source](https://github.com/luikore/nyara/wiki/Building)
|
@@ -50,4 +52,9 @@ ruby nyahaha.rb
|
|
50
52
|
|
51
53
|
# License
|
52
54
|
|
53
|
-
BSD, see [copying](https://github.com/luikore/nyara/blob/master/copying)
|
55
|
+
BSD 3-Clause, see [copying](https://github.com/luikore/nyara/blob/master/copying)
|
56
|
+
|
57
|
+
# Contributors
|
58
|
+
|
59
|
+
luikore
|
60
|
+
hooopo
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
module Nyara
|
4
|
+
describe Config do
|
5
|
+
after :all do
|
6
|
+
Config.reset
|
7
|
+
end
|
8
|
+
|
9
|
+
it "#workers" do
|
10
|
+
Nyara.config.workers '3'
|
11
|
+
assert_equal 3, Config['workers']
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#port" do
|
15
|
+
Config.port '1000'
|
16
|
+
assert_equal 1000, Config['port']
|
17
|
+
end
|
18
|
+
|
19
|
+
it "env helpers" do
|
20
|
+
Config.set :env, 'test'
|
21
|
+
assert_equal true, Config.test?
|
22
|
+
assert_equal false, Config.development?
|
23
|
+
|
24
|
+
Config.set :env, 'production'
|
25
|
+
assert_equal false, Config.test?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/evented_io_spec.rb
CHANGED
data/spec/flash_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
module Nyara
|
4
|
+
describe Flash do
|
5
|
+
before :each do
|
6
|
+
@session = ParamHash.new
|
7
|
+
@flash = Flash.new @session
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#next" do
|
11
|
+
@flash[:foo] = 'foo'
|
12
|
+
assert_equal 'foo', @flash.next['foo']
|
13
|
+
assert_not_empty @session.values.first
|
14
|
+
|
15
|
+
@flash.next[:bar] = 'bar'
|
16
|
+
assert_nil @flash[:bar]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "#now" do
|
20
|
+
@flash.now['foo'] = 'foo'
|
21
|
+
assert_nil @flash.next['foo']
|
22
|
+
assert_empty @session.values.first
|
23
|
+
end
|
24
|
+
|
25
|
+
it "#now is from session, and #next is to session" do
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/hashes_spec.rb
CHANGED
@@ -26,6 +26,14 @@ module Nyara
|
|
26
26
|
assert_equal ['Content-Type', 'text/plain'], h.to_a.first
|
27
27
|
end
|
28
28
|
|
29
|
+
it "can serialize into an array" do
|
30
|
+
h = HeaderHash.new
|
31
|
+
h['Content-Length'] = 3
|
32
|
+
h['X-Weird'] = '奇怪'
|
33
|
+
arr = h.serialize
|
34
|
+
assert_equal ["Content-Length: 3\r\n", "X-Weird: 奇怪\r\n"], arr
|
35
|
+
end
|
36
|
+
|
29
37
|
class HaHash < HeaderHash
|
30
38
|
end
|
31
39
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
module Nyara
|
4
|
+
describe 'mini_support' do
|
5
|
+
it "Array#sum" do
|
6
|
+
assert_equal 0, [].sum
|
7
|
+
assert_equal 3, [1, 2].sum
|
8
|
+
end
|
9
|
+
|
10
|
+
# just test existence, no need to repeat activesupport tests
|
11
|
+
it "Object#blank?" do
|
12
|
+
assert [].respond_to? :blank?
|
13
|
+
end
|
14
|
+
|
15
|
+
# just test existence, no need to repeat activesupport tests
|
16
|
+
it "Object#to_query" do
|
17
|
+
assert ''.respond_to? :to_query
|
18
|
+
end
|
19
|
+
|
20
|
+
it "to_json works with inheritance" do
|
21
|
+
h = HeaderHash.new
|
22
|
+
h[:Accept] = 'en'
|
23
|
+
assert_equal('{"Accept":"en"}', h.to_json)
|
24
|
+
end
|
25
|
+
|
26
|
+
# just a class with .json_create(attrs)
|
27
|
+
class MyClass
|
28
|
+
def self.json_create attrs
|
29
|
+
1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
it "json load security" do
|
33
|
+
assert_equal({"json_class" => "MyClass", "length" => 1}, JSON.load('{"json_class":"MyClass","length":1}'))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "json dump security" do
|
37
|
+
h = {"a" => '</script>', "b" => true}
|
38
|
+
assert_not_include(h.to_json, '</script>')
|
39
|
+
assert_equal h, JSON.parse(h.to_json)
|
40
|
+
end
|
41
|
+
|
42
|
+
if RUBY_VERSION <= '2.0.0'
|
43
|
+
it "String#b" do
|
44
|
+
assert_equal 'ASCII-8BIT', "你".b.encoding.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
it "String#scrub" do
|
48
|
+
# from rdoc examples
|
49
|
+
assert_equal "abc\u3042\uFFFD", "abc\u3042\x81".scrub
|
50
|
+
assert_equal "abc\u3042*", "abc\u3042\x81".scrub("*")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/part_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
module Nyara
|
4
|
+
describe Part do
|
5
|
+
it "bottom case" do
|
6
|
+
p = Part.new({})
|
7
|
+
p.final
|
8
|
+
assert_equal '', p['data']
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses base64" do
|
12
|
+
p = Part.new\
|
13
|
+
'Content-Type' => 'image/jpeg; bla bla',
|
14
|
+
'Content-Disposition' => "inline; fiLename *= gbk'zh-CN'%C8%CB%B2%CE.jpg; namE= \"file\"",
|
15
|
+
'Content-Transfer-Encoding' => 'base64'
|
16
|
+
|
17
|
+
# total length a multiple to 8
|
18
|
+
data = [
|
19
|
+
"fNfaX7SRKfEfXMcYmHT/AHRXw94r0",
|
20
|
+
'G',
|
21
|
+
'xXxTqQFlaAC6l/5Yr/AHz7V6nAtam414yhf3',
|
22
|
+
'jhzqEoODUt0f/Z'
|
23
|
+
]
|
24
|
+
data.each do |datum|
|
25
|
+
p.update datum.dup
|
26
|
+
end
|
27
|
+
p.final
|
28
|
+
|
29
|
+
assert_equal 'base64', p['mechanism']
|
30
|
+
assert_equal 'image/jpeg', p['type']
|
31
|
+
assert_equal '人参.jpg', p['filename']
|
32
|
+
assert_equal 'file', p['name']
|
33
|
+
assert_equal data.join, Base64.strict_encode64(p['data'])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "parses 7bit data" do
|
37
|
+
p = Part.new\
|
38
|
+
'Content-Transfer-Encoding' => '7bit'
|
39
|
+
p.update 'foo bar'
|
40
|
+
assert_equal 'foo bar', p['data']
|
41
|
+
end
|
42
|
+
|
43
|
+
it "parses QP data" do
|
44
|
+
p = Part.new\
|
45
|
+
'Content-Transfer-Encoding' => 'quoted-PrintaBle'
|
46
|
+
p.update "a =3F ="
|
47
|
+
p.update "04"
|
48
|
+
p.final
|
49
|
+
assert_equal "a \x3F \x04", p['data']
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/path_helper_spec.rb
CHANGED
@@ -23,36 +23,39 @@ end
|
|
23
23
|
module Nyara
|
24
24
|
describe [Controller, Route] do
|
25
25
|
before :all do
|
26
|
-
Route.clear
|
27
26
|
Config.configure do
|
27
|
+
reset
|
28
28
|
set 'host', 'yavaeye.com'
|
29
29
|
map '/', 'foo'
|
30
30
|
map '/bar-prefix', 'foo_controller::bar'
|
31
31
|
map '/baz-prefix', 'foo_controller::baz'
|
32
32
|
end
|
33
|
-
|
33
|
+
Nyara.setup
|
34
34
|
end
|
35
35
|
|
36
|
-
context '#
|
36
|
+
context '#path_to' do
|
37
37
|
it "local query" do
|
38
38
|
c = FooController.new :stub_request
|
39
|
-
assert_equal '/12', c.
|
39
|
+
assert_equal '/12', c.path_to('#index', 12)
|
40
40
|
assert_raise ArgumentError do
|
41
|
-
c.
|
41
|
+
c.path_to '#index'
|
42
42
|
end
|
43
43
|
assert_raise ArgumentError do
|
44
|
-
c.
|
44
|
+
c.path_to('#index', 'a')
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
it "global query" do
|
49
49
|
c = FooController::BarController.new :stub_request
|
50
|
-
assert_equal '/bar-prefix/', c.
|
51
|
-
assert_equal '/baz-prefix/1', c.
|
50
|
+
assert_equal '/bar-prefix/', c.path_to('foo_controller::bar#index')
|
51
|
+
assert_equal '/baz-prefix/1', c.path_to('baz#index', 1)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "generates for nested query" do
|
55
|
-
|
55
|
+
c = FooController.new :stub_request
|
56
|
+
path = c.path_to('#index', 1, post: {array: [1, 2]})
|
57
|
+
items = URI.parse(path).query.split('&').map{|q| CGI.unescape q }
|
58
|
+
assert_equal ["post[array][]=1", "post[array][]=2"], items
|
56
59
|
end
|
57
60
|
|
58
61
|
it "perserves _method query" do
|
@@ -61,16 +64,21 @@ module Nyara
|
|
61
64
|
|
62
65
|
it "appends format and query" do
|
63
66
|
c = FooController.new :stub_request
|
64
|
-
generated = c.
|
67
|
+
generated = c.path_to '#index', 1, format: 'js', 'utm_source' => 'a spam'
|
65
68
|
assert_equal "/1.js?utm_source=a+spam", generated
|
66
69
|
end
|
67
70
|
end
|
68
71
|
|
69
|
-
context '#
|
72
|
+
context '#url_to' do
|
70
73
|
it "works" do
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
+
request = Object.new
|
75
|
+
class << request
|
76
|
+
attr_accessor :host_with_port
|
77
|
+
end
|
78
|
+
request.host_with_port = 'yavaeye.com'
|
79
|
+
c = FooController::BazController.new request
|
80
|
+
assert_equal '//yavaeye.com/baz-prefix/1', c.url_to('#index', 1)
|
81
|
+
assert_equal 'https://localhost:4567/1', c.url_to('foo#index', 1, scheme: 'https', host: 'localhost:4567')
|
74
82
|
end
|
75
83
|
end
|
76
84
|
end
|
@@ -5,15 +5,26 @@ module Nyara
|
|
5
5
|
class DelegateController < Controller
|
6
6
|
end
|
7
7
|
|
8
|
+
before :all do
|
9
|
+
Session.init
|
10
|
+
end
|
11
|
+
|
8
12
|
before :each do
|
9
13
|
@request = Ext.request_new
|
14
|
+
session = ParamHash.new
|
10
15
|
Ext.request_set_attrs @request, {
|
11
16
|
method_num: HTTP_METHODS['GET'],
|
12
17
|
path: '/search',
|
13
18
|
query: ParamHash.new.tap{|h| h['q'] = 'nyara' },
|
14
19
|
fiber: Fiber.new{},
|
15
20
|
scope: '/scope',
|
16
|
-
header: HeaderHash.new.tap{|h| h['Accept'] = 'en-US' }
|
21
|
+
header: HeaderHash.new.tap{|h| h['Accept'] = 'en-US' },
|
22
|
+
session: session,
|
23
|
+
flash: Flash.new(session),
|
24
|
+
cookie: ParamHash.new.tap{|h|
|
25
|
+
h['key1'] = 1
|
26
|
+
h['key2'] = 2
|
27
|
+
}
|
17
28
|
# ext: nil
|
18
29
|
# response_header:
|
19
30
|
# response_header_extra_lines:
|
@@ -38,7 +49,7 @@ module Nyara
|
|
38
49
|
assert_equal 'en-US', @c.header['accept']
|
39
50
|
end
|
40
51
|
|
41
|
-
context "
|
52
|
+
context "Emulate output IO" do
|
42
53
|
before :each do
|
43
54
|
@client, @server = Socket.pair :UNIX, :STREAM
|
44
55
|
Ext.set_nonblock @server.fileno
|
@@ -61,23 +72,20 @@ module Nyara
|
|
61
72
|
|
62
73
|
it "set cookie" do
|
63
74
|
@c.set_cookie 'set', 'set'
|
64
|
-
|
65
|
-
|
75
|
+
cookie_lines = receive_header.lines.grep(/Set-Cookie:/)
|
76
|
+
assert cookie_lines.grep(/set=set; HttpOnly/).first, cookie_lines.inspect
|
66
77
|
end
|
67
78
|
|
68
79
|
it "delete cookie" do
|
69
80
|
@c.delete_cookie 'del'
|
70
|
-
|
71
|
-
|
81
|
+
cookie_lines = receive_header.lines.grep(/Set-Cookie:/)
|
82
|
+
assert cookie_lines.grep(/del.*Expires/).first, cookie_lines.inspect
|
72
83
|
end
|
73
84
|
|
74
85
|
it "clear cookie" do
|
75
86
|
@c.clear_cookie
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
it "#session" do
|
80
|
-
pending
|
87
|
+
cookie_lines = receive_header.lines
|
88
|
+
assert_equal 2, cookie_lines.grep(/Set-Cookie: (key1|key2);/).size, cookie_lines.inspect
|
81
89
|
end
|
82
90
|
end
|
83
91
|
end
|
data/spec/route_entry_spec.rb
CHANGED
@@ -2,11 +2,66 @@ require_relative "spec_helper"
|
|
2
2
|
|
3
3
|
module Nyara
|
4
4
|
describe RouteEntry do
|
5
|
+
before :each do
|
6
|
+
@r = RouteEntry.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "#compile prefix, suffix and conv" do
|
10
|
+
@r.path = '/'
|
11
|
+
@r.compile :controller_stub, '/'
|
12
|
+
assert_equal '/', @r.prefix
|
13
|
+
assert_equal '', @r.suffix
|
14
|
+
assert_equal [], @r.conv
|
15
|
+
|
16
|
+
@r.path = '/'
|
17
|
+
@r.compile :controller_stub, '/scope'
|
18
|
+
assert_equal '/scope', @r.prefix
|
19
|
+
assert_equal '', @r.suffix
|
20
|
+
assert_equal [], @r.conv
|
21
|
+
|
22
|
+
@r.path = '/a/%d/b'
|
23
|
+
@r.compile :controller_stub, '/scope'
|
24
|
+
assert_equal "/scope/a/", @r.prefix
|
25
|
+
assert_equal "^(-?[0-9]+)/b$", @r.suffix
|
26
|
+
assert_equal [:to_i], @r.conv
|
27
|
+
end
|
28
|
+
|
5
29
|
it "#set_accept_exts" do
|
6
30
|
r = RouteEntry.new
|
7
31
|
r.set_accept_exts ['html', :js]
|
8
32
|
assert_equal [%w"text html html", %w"application javascript js"], r.accept_mimes
|
9
33
|
assert_equal ({'html'=>true, 'js'=>true}), r.accept_exts
|
10
34
|
end
|
35
|
+
|
36
|
+
it "#compile_re" do
|
37
|
+
re, conv = @r.compile_re '%s/%u/%d/%f/%x'
|
38
|
+
assert_equal [:to_s, :to_i, :to_i, :to_f, :hex], conv
|
39
|
+
s = '1/2/-3/4.5/F'
|
40
|
+
assert_equal [s, *s.split('/')], s.match(Regexp.new re).to_a
|
41
|
+
|
42
|
+
re, conv = @r.compile_re '/'
|
43
|
+
assert_equal '^/$', re
|
44
|
+
assert_equal [], conv
|
45
|
+
end
|
46
|
+
|
47
|
+
it "#compile_re with utf-8 chars" do
|
48
|
+
re, conv = @r.compile_re '/目录/%da/也可以'
|
49
|
+
assert_equal [:to_i], conv
|
50
|
+
s = "/目录/12a/也可以"
|
51
|
+
assert_equal [s, '12'], s.match(Regexp.new re).to_a
|
52
|
+
end
|
53
|
+
|
54
|
+
it "#analyse_path" do
|
55
|
+
r = @r.analyse_path '/hello/%d-world%u/%s/'
|
56
|
+
assert_equal ['/hello/', '%d-world%u/%s'], r
|
57
|
+
|
58
|
+
prefix, suffix = @r.analyse_path '/hello'
|
59
|
+
assert_equal '/hello', prefix
|
60
|
+
assert_equal nil, suffix
|
61
|
+
|
62
|
+
prefix, suffix = @r.analyse_path '/'
|
63
|
+
assert_equal '/', prefix
|
64
|
+
assert_equal nil, suffix
|
65
|
+
end
|
11
66
|
end
|
12
67
|
end
|
data/spec/session_spec.rb
CHANGED
@@ -2,18 +2,74 @@ require_relative "spec_helper"
|
|
2
2
|
|
3
3
|
module Nyara
|
4
4
|
describe Session do
|
5
|
+
context ".encode_set_cookie options" do
|
6
|
+
before :all do
|
7
|
+
@session = {'hello' => 'world'}
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is HttpOnly" do
|
11
|
+
Session.init
|
12
|
+
line = Session.encode_set_cookie @session, false
|
13
|
+
assert_includes line, '; HttpOnly'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "adds Secure" do
|
17
|
+
init_configure_with :session, :secure, true
|
18
|
+
line = Session.encode_set_cookie @session, false
|
19
|
+
assert_includes line, '; Secure'
|
20
|
+
|
21
|
+
init_configure_with :session, :secure, false
|
22
|
+
line = Session.encode_set_cookie @session, true
|
23
|
+
assert_not_includes line, 'Secure'
|
24
|
+
|
25
|
+
init_configure_with :session, :secure, nil
|
26
|
+
line = Session.encode_set_cookie @session, true
|
27
|
+
assert_includes line, '; Secure'
|
28
|
+
line = Session.encode_set_cookie @session, false
|
29
|
+
assert_not_includes line, 'Secure'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "adds Expires" do
|
33
|
+
init_configure_with :session, :expire, nil
|
34
|
+
line = Session.encode_set_cookie @session, false
|
35
|
+
assert_not_includes line, 'Expires'
|
36
|
+
|
37
|
+
init_configure_with :session, :expires, 30 * 60
|
38
|
+
line = Session.encode_set_cookie @session, false
|
39
|
+
assert_includes line, '; Expires='
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises for unkown keys" do
|
43
|
+
assert_raise RuntimeError do
|
44
|
+
init_configure_with :session, :ciphre_key, 'adsf'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def init_configure_with *options
|
49
|
+
Config.configure do
|
50
|
+
reset
|
51
|
+
set *options
|
52
|
+
end
|
53
|
+
Session.init
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
5
57
|
context "no cipher" do
|
6
58
|
before :all do
|
7
|
-
Config
|
59
|
+
Config.configure do
|
60
|
+
reset
|
61
|
+
set 'session', 'cipher_key', nil
|
62
|
+
end
|
8
63
|
Session.init
|
9
64
|
end
|
10
65
|
|
11
66
|
it "should encode and decode" do
|
12
67
|
cookie = {}
|
13
68
|
session = {'hello' => 'world'}
|
14
|
-
Session.
|
69
|
+
Session.encode_to_cookie session, cookie
|
15
70
|
|
16
|
-
|
71
|
+
session_data = cookie[Session.name].split('/')[1]
|
72
|
+
assert_includes Base64.urlsafe_decode64(session_data), 'world'
|
17
73
|
|
18
74
|
session2 = Session.decode cookie
|
19
75
|
assert_equal 'world', session2[:hello]
|
@@ -22,7 +78,7 @@ module Nyara
|
|
22
78
|
it "drops bad signature" do
|
23
79
|
cookie = {}
|
24
80
|
session = {'hello' => 'world'}
|
25
|
-
Session.
|
81
|
+
Session.encode_to_cookie session, cookie
|
26
82
|
|
27
83
|
cookie[Session.name].sub!(/\w/, &:swapcase)
|
28
84
|
|
@@ -33,16 +89,22 @@ module Nyara
|
|
33
89
|
|
34
90
|
context "with cipher" do
|
35
91
|
before :all do
|
36
|
-
Config
|
92
|
+
Config.configure do
|
93
|
+
reset
|
94
|
+
set 'session', 'cipher_key', "some cipher key"
|
95
|
+
end
|
37
96
|
Session.init
|
38
97
|
end
|
39
98
|
|
40
99
|
it "encode and decode" do
|
41
100
|
cookie = {}
|
42
101
|
session = {'hello' => 'world'}
|
43
|
-
Session.
|
102
|
+
Session.encode_to_cookie session, cookie
|
44
103
|
|
45
|
-
|
104
|
+
session_data = cookie[Session.name].split('/')[1]
|
105
|
+
if session_data
|
106
|
+
assert_not_includes Base64.urlsafe_decode64(session_data), 'world'
|
107
|
+
end
|
46
108
|
|
47
109
|
session2 = Session.decode cookie
|
48
110
|
assert_equal 'world', session2[:hello]
|
data/spec/spec_helper.rb
CHANGED
@@ -7,15 +7,18 @@ require "erb"
|
|
7
7
|
require "haml"
|
8
8
|
require "liquid"
|
9
9
|
require "open-uri"
|
10
|
+
require "base64"
|
10
11
|
require 'pp'
|
11
12
|
|
12
13
|
if ENV['COVERAGE']
|
13
14
|
require "simplecov"
|
14
15
|
SimpleCov.start do
|
16
|
+
coverage_dir '.coverage'
|
15
17
|
add_group 'lib', 'lib'
|
16
18
|
end
|
17
19
|
end
|
18
20
|
require_relative "../lib/nyara/nyara"
|
21
|
+
require_relative "../lib/nyara/test"
|
19
22
|
|
20
23
|
RSpec.configure do |config|
|
21
24
|
config.expect_with :stdlib
|
data/spec/test_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
class TestController < Nyara::Controller
|
4
|
+
meta '#index'
|
5
|
+
get '/' do
|
6
|
+
content_type 'txt'
|
7
|
+
send_string '初めまして from test'
|
8
|
+
end
|
9
|
+
|
10
|
+
meta '#create'
|
11
|
+
post '/create' do
|
12
|
+
redirect_to '#index'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class MyTest
|
17
|
+
include Nyara::Test
|
18
|
+
end
|
19
|
+
|
20
|
+
module Nyara
|
21
|
+
describe Nyara::Test do
|
22
|
+
before :all do
|
23
|
+
configure do
|
24
|
+
reset
|
25
|
+
set :env, 'test'
|
26
|
+
map '/', TestController
|
27
|
+
end
|
28
|
+
Nyara.setup
|
29
|
+
@test = MyTest.new
|
30
|
+
end
|
31
|
+
|
32
|
+
it "response" do
|
33
|
+
@test.get "/", {'Xample' => 'résumé'}
|
34
|
+
assert @test.response.success?
|
35
|
+
assert_equal 'résumé', @test.request.header['Xample']
|
36
|
+
assert_equal '初めまして from test', @test.response.body
|
37
|
+
assert_equal 'text/plain; charset=UTF-8', @test.response.header['Content-Type']
|
38
|
+
end
|
39
|
+
|
40
|
+
it "redirect" do
|
41
|
+
@test.post @test.path_to('test#create')
|
42
|
+
assert @test.response.success?
|
43
|
+
assert_equal 'http://localhost/', @test.redirect_location
|
44
|
+
@test.follow_redirect
|
45
|
+
assert_equal '/', @test.request.path
|
46
|
+
end
|
47
|
+
|
48
|
+
it "session continuation" do
|
49
|
+
@test.session['a'] = '3'
|
50
|
+
@test.get "/"
|
51
|
+
assert_equal '3', @test.session['a']
|
52
|
+
@test.session['b'] = '4'
|
53
|
+
@test.get "/"
|
54
|
+
assert_equal '4', @test.session['b']
|
55
|
+
assert_equal '3', @test.session['a']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/tools/hello.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require_relative "../lib/nyara"
|
2
|
-
require "open-uri"
|
3
|
-
require "pry"
|
2
|
+
# require "open-uri"
|
3
|
+
# require "pry"
|
4
4
|
|
5
5
|
configure do
|
6
6
|
# set :env, 'production'
|
@@ -8,8 +8,16 @@ configure do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
get '/' do
|
11
|
-
open 'http://baidu.com', &:read
|
11
|
+
# open 'http://baidu.com', &:read
|
12
12
|
send_string 'hello world'
|
13
13
|
end
|
14
14
|
|
15
|
+
get '/exit' do
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/redi' do
|
20
|
+
redirect '/'
|
21
|
+
end
|
22
|
+
|
15
23
|
# GC.stress = true
|