utopia 0.12.6 → 1.0.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/.travis.yml +6 -2
- data/Gemfile +6 -0
- data/README.md +48 -14
- data/Rakefile +5 -0
- data/bin/utopia +132 -15
- data/lib/utopia.rb +13 -10
- data/lib/utopia/content.rb +140 -0
- data/lib/utopia/content/link.rb +124 -0
- data/lib/utopia/content/links.rb +228 -0
- data/lib/utopia/content/node.rb +387 -0
- data/lib/utopia/content/processor.rb +128 -0
- data/lib/utopia/content/tag.rb +102 -0
- data/lib/utopia/controller.rb +137 -0
- data/lib/utopia/controller/action.rb +112 -0
- data/lib/utopia/controller/base.rb +174 -0
- data/lib/utopia/{middleware/controller → controller}/variables.rb +36 -38
- data/lib/utopia/exception_handler.rb +79 -0
- data/lib/utopia/extensions/array.rb +2 -2
- data/lib/utopia/localization.rb +143 -0
- data/lib/utopia/mail_exceptions.rb +136 -0
- data/lib/utopia/middleware.rb +7 -22
- data/lib/utopia/path.rb +150 -60
- data/lib/utopia/redirector.rb +152 -0
- data/lib/utopia/{extensions/hash.rb → session.rb} +4 -6
- data/lib/utopia/session/encrypted_cookie.rb +46 -48
- data/lib/utopia/{middleware/directory_index.rb → session/lazy_hash.rb} +44 -27
- data/lib/utopia/static.rb +255 -0
- data/lib/utopia/tags/deferred.rb +12 -8
- data/lib/utopia/tags/environment.rb +18 -6
- data/lib/utopia/tags/node.rb +12 -8
- data/lib/utopia/tags/override.rb +12 -12
- data/lib/utopia/version.rb +1 -1
- data/setup/.bowerrc +3 -0
- data/{lib/utopia/setup → setup}/Gemfile +1 -1
- data/setup/Rakefile +4 -0
- data/{lib/utopia/setup → setup}/cache/head/readme.txt +0 -0
- data/{lib/utopia/setup → setup}/cache/meta/readme.txt +0 -0
- data/setup/config.ru +64 -0
- data/{lib/utopia/setup → setup}/lib/readme.txt +0 -0
- data/{lib/utopia/setup → setup}/pages/_heading.xnode +0 -0
- data/{lib/utopia/setup → setup}/pages/_page.xnode +1 -1
- data/{lib/utopia/setup → setup}/pages/_static/icon.png +0 -0
- data/setup/pages/_static/site.css +70 -0
- data/{lib/utopia/setup → setup}/pages/errors/exception.xnode +0 -0
- data/{lib/utopia/setup → setup}/pages/errors/file-not-found.xnode +0 -0
- data/{lib/utopia/setup → setup}/pages/links.yaml +0 -0
- data/setup/pages/welcome/index.xnode +17 -0
- data/{lib/utopia/setup → setup}/public/readme.txt +0 -0
- data/spec/utopia/content/link_spec.rb +108 -0
- data/spec/utopia/content/links/foo/index.xnode +0 -0
- data/spec/utopia/content/links/foo/links.yaml +2 -0
- data/spec/utopia/content/links/foo/test.de.xnode +0 -0
- data/spec/utopia/content/links/foo/test.en.xnode +0 -0
- data/spec/utopia/content/links/links.yaml +9 -0
- data/spec/utopia/content/links/welcome.xnode +0 -0
- data/spec/utopia/content/localized/five/index.en.xnode +0 -0
- data/spec/utopia/content/localized/four/index.en.xnode +0 -0
- data/spec/utopia/content/localized/four/index.zh.xnode +0 -0
- data/spec/utopia/content/localized/four/links.yaml +4 -0
- data/spec/utopia/content/localized/links.yaml +16 -0
- data/spec/utopia/content/localized/one.xnode +0 -0
- data/spec/utopia/content/localized/three/index.xnode +0 -0
- data/spec/utopia/content/localized/two.en.xnode +0 -0
- data/spec/utopia/content/localized/two.zh.xnode +0 -0
- data/spec/utopia/content/node/ordered/first.xnode +0 -0
- data/spec/utopia/content/node/ordered/index.xnode +0 -0
- data/spec/utopia/content/node/ordered/links.yaml +4 -0
- data/spec/utopia/content/node/ordered/second.xnode +0 -0
- data/spec/utopia/content/node/related/foo.en.xnode +0 -0
- data/spec/utopia/content/node/related/foo.ja.xnode +0 -0
- data/spec/utopia/content/node/related/links.yaml +4 -0
- data/spec/utopia/content/node_spec.rb +63 -0
- data/spec/utopia/{middleware/content_spec.rb → content/processor_spec.rb} +34 -23
- data/spec/utopia/content_spec.rb +87 -0
- data/spec/utopia/content_spec.ru +10 -0
- data/spec/utopia/{middleware/controller_spec.rb → controller_spec.rb} +61 -16
- data/spec/utopia/controller_spec.ru +4 -0
- data/spec/utopia/extensions_spec.rb +6 -17
- data/spec/utopia/localization_spec.rb +60 -0
- data/spec/utopia/localization_spec.ru +11 -0
- data/{lib/utopia/tags.rb → spec/utopia/middleware_spec.rb} +8 -14
- data/spec/utopia/{middleware/content_root → pages}/_heading.xnode +0 -0
- data/spec/utopia/pages/content/_show-value.xnode +1 -0
- data/spec/utopia/pages/content/test-partial.xnode +1 -0
- data/spec/utopia/pages/controller/controller.rb +28 -0
- data/spec/utopia/pages/controller/index.xnode +1 -0
- data/spec/utopia/pages/controller/nested/controller.rb +4 -0
- data/spec/utopia/{middleware/content_root → pages}/index.xnode +0 -0
- data/spec/utopia/pages/localized.de.txt +1 -0
- data/spec/utopia/pages/localized.en.txt +1 -0
- data/spec/utopia/pages/localized.jp.txt +1 -0
- data/spec/utopia/pages/node/index.xnode +1 -0
- data/spec/utopia/pages/test.txt +1 -0
- data/spec/utopia/path_spec.rb +109 -0
- data/spec/utopia/rack_spec.rb +2 -0
- data/spec/utopia/session_spec.rb +82 -0
- data/spec/utopia/session_spec.ru +20 -0
- data/spec/utopia/spec_helper.rb +16 -0
- data/{lib/utopia/extensions/string.rb → spec/utopia/static_spec.rb} +24 -15
- data/spec/utopia/static_spec.ru +4 -0
- data/utopia.gemspec +3 -3
- metadata +138 -54
- data/lib/utopia/extensions/regexp.rb +0 -33
- data/lib/utopia/link.rb +0 -288
- data/lib/utopia/middleware/all.rb +0 -33
- data/lib/utopia/middleware/content.rb +0 -157
- data/lib/utopia/middleware/content/node.rb +0 -386
- data/lib/utopia/middleware/content/processor.rb +0 -123
- data/lib/utopia/middleware/controller.rb +0 -130
- data/lib/utopia/middleware/controller/action.rb +0 -121
- data/lib/utopia/middleware/controller/base.rb +0 -184
- data/lib/utopia/middleware/exception_handler.rb +0 -80
- data/lib/utopia/middleware/localization.rb +0 -147
- data/lib/utopia/middleware/localization/name.rb +0 -69
- data/lib/utopia/middleware/mail_exceptions.rb +0 -138
- data/lib/utopia/middleware/redirector.rb +0 -146
- data/lib/utopia/middleware/requester.rb +0 -126
- data/lib/utopia/middleware/static.rb +0 -295
- data/lib/utopia/setup.rb +0 -60
- data/lib/utopia/setup/config.ru +0 -47
- data/lib/utopia/setup/pages/_static/background.png +0 -0
- data/lib/utopia/setup/pages/_static/site.css +0 -48
- data/lib/utopia/setup/pages/welcome/index.xnode +0 -7
- data/lib/utopia/tag.rb +0 -105
- data/lib/utopia/tags/all.rb +0 -34
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
19
|
# THE SOFTWARE.
|
|
20
20
|
|
|
21
|
+
require_relative 'spec_helper'
|
|
22
|
+
|
|
21
23
|
require 'utopia/extensions/array'
|
|
22
24
|
require 'utopia/extensions/date'
|
|
23
|
-
require 'utopia/extensions/hash'
|
|
24
|
-
require 'utopia/extensions/regexp'
|
|
25
25
|
|
|
26
26
|
module Utopia::ExtensionsSpec
|
|
27
27
|
describe Array do
|
|
@@ -34,6 +34,10 @@ module Utopia::ExtensionsSpec
|
|
|
34
34
|
expect(b).to be == 3
|
|
35
35
|
expect(c).to be == [4, 5]
|
|
36
36
|
end
|
|
37
|
+
|
|
38
|
+
it "should not split empty array" do
|
|
39
|
+
expect([].split_at('a')).to be == [[], nil, []]
|
|
40
|
+
end
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
describe Date do
|
|
@@ -66,19 +70,4 @@ module Utopia::ExtensionsSpec
|
|
|
66
70
|
expect(today.to_time <=> yesterday.to_time).to be == 1
|
|
67
71
|
end
|
|
68
72
|
end
|
|
69
|
-
|
|
70
|
-
describe Hash do
|
|
71
|
-
it "should symbolize keys" do
|
|
72
|
-
hash = {'a' => 1, 'b' => 2}
|
|
73
|
-
|
|
74
|
-
expect(hash.symbolize_keys).to be == {a: 1, b: 2}
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
describe Regexp do
|
|
79
|
-
it "should start with" do
|
|
80
|
-
expect(Regexp.starts_with("foobar")).to be =~ "foobarbaz"
|
|
81
|
-
expect(Regexp.starts_with("bar")).to_not be =~ "blubarbaz"
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
73
|
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env rspec
|
|
2
|
+
# Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
3
|
+
#
|
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
# furnished to do so, subject to the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be included in
|
|
12
|
+
# all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
# THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require_relative 'spec_helper'
|
|
23
|
+
|
|
24
|
+
require 'rack'
|
|
25
|
+
require 'rack/test'
|
|
26
|
+
|
|
27
|
+
require 'utopia/static'
|
|
28
|
+
require 'utopia/content'
|
|
29
|
+
require 'utopia/localization'
|
|
30
|
+
|
|
31
|
+
module Utopia::StaticSpec
|
|
32
|
+
describe Utopia::Static do
|
|
33
|
+
include Rack::Test::Methods
|
|
34
|
+
|
|
35
|
+
let(:app) {Rack::Builder.parse_file(File.expand_path('../localization_spec.ru', __FILE__)).first}
|
|
36
|
+
|
|
37
|
+
it "should respond with default localization" do
|
|
38
|
+
get '/localized.txt'
|
|
39
|
+
|
|
40
|
+
expect(last_response.body).to be == 'localized.en.txt'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "should respond with the requested localization" do
|
|
44
|
+
get '/en/localized.txt'
|
|
45
|
+
expect(last_response.body).to be == 'localized.en.txt'
|
|
46
|
+
|
|
47
|
+
get '/de/localized.txt'
|
|
48
|
+
expect(last_response.body).to be == 'localized.de.txt'
|
|
49
|
+
|
|
50
|
+
get '/jp/localized.txt'
|
|
51
|
+
expect(last_response.body).to be == 'localized.jp.txt'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "should respond with accepted language localization" do
|
|
55
|
+
get '/localized.txt', {}, 'HTTP_ACCEPT_LANGUAGE' => 'jp,en'
|
|
56
|
+
|
|
57
|
+
expect(last_response.body).to be == 'localized.jp.txt'
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -18,20 +18,14 @@
|
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
19
|
# THE SOFTWARE.
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def self.create(name, &block)
|
|
30
|
-
@@all[name] = block
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def self.all
|
|
34
|
-
@@all
|
|
21
|
+
require_relative 'spec_helper'
|
|
22
|
+
|
|
23
|
+
require 'utopia/middleware'
|
|
24
|
+
|
|
25
|
+
module Utopia::MiddlewareSpec
|
|
26
|
+
describe Utopia do
|
|
27
|
+
it "should give a default path relative to the cwd" do
|
|
28
|
+
expect(File).to exist(Utopia::default_root('pages', File.dirname(__FILE__)))
|
|
35
29
|
end
|
|
36
30
|
end
|
|
37
31
|
end
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#{attributes[:value]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#{partial 'show-value', value: 10}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
on 'flat' do
|
|
3
|
+
success! content: "flat"
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
on '**/hello-world' do
|
|
7
|
+
success! content: @hello_world
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
on '**' do
|
|
11
|
+
@hello_world = "Hello World"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
on 'ignore' do
|
|
15
|
+
ignore!
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
on 'redirect' do
|
|
19
|
+
redirect! 'bar'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
on 'rewrite' do
|
|
23
|
+
rewrite! 'index'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
on 'index' do
|
|
27
|
+
success! content: 'Hello World'
|
|
28
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Foobar
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
localized.de.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
localized.en.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
localized.jp.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#{local_path}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Hello World!
|
data/spec/utopia/path_spec.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env rspec
|
|
2
|
+
|
|
1
3
|
# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
4
|
#
|
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
@@ -18,10 +20,19 @@
|
|
|
18
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
21
|
# THE SOFTWARE.
|
|
20
22
|
|
|
23
|
+
require_relative 'spec_helper'
|
|
24
|
+
|
|
21
25
|
require 'utopia/path'
|
|
22
26
|
|
|
23
27
|
module Utopia::PathSpec
|
|
24
28
|
describe Utopia::Path do
|
|
29
|
+
it "should be root path" do
|
|
30
|
+
root = Utopia::Path["/"]
|
|
31
|
+
|
|
32
|
+
expect(root.components).to be == ['', '']
|
|
33
|
+
expect(root.to_local_path).to be == '/'
|
|
34
|
+
end
|
|
35
|
+
|
|
25
36
|
it "should concatenate absolute paths" do
|
|
26
37
|
root = Utopia::Path["/"]
|
|
27
38
|
|
|
@@ -42,5 +53,103 @@ module Utopia::PathSpec
|
|
|
42
53
|
|
|
43
54
|
expect(descendants.reverse).to be == ascendants
|
|
44
55
|
end
|
|
56
|
+
|
|
57
|
+
it "should be able to remove relative path entries" do
|
|
58
|
+
path = Utopia::Path["/foo/bar/../baz/."]
|
|
59
|
+
expect(path.simplify.components).to be == ['', 'foo', 'baz']
|
|
60
|
+
|
|
61
|
+
path = Utopia::Path["/foo/bar/../baz/./"]
|
|
62
|
+
expect(path.simplify.components).to be == ['', 'foo', 'baz', '']
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "should remove the extension from the basename" do
|
|
66
|
+
path = Utopia::Path["dir/foo.html"]
|
|
67
|
+
|
|
68
|
+
basename = path.basename("html")
|
|
69
|
+
|
|
70
|
+
expect(basename.name).to be == 'foo'
|
|
71
|
+
expect(basename.extension).to be == 'html'
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "should be able to convert into a directory" do
|
|
75
|
+
path = Utopia::Path["foo/bar"]
|
|
76
|
+
|
|
77
|
+
expect(path).to_not be_directory
|
|
78
|
+
|
|
79
|
+
expect(path.to_directory).to be_directory
|
|
80
|
+
|
|
81
|
+
dir_path = path.to_directory
|
|
82
|
+
expect(dir_path.to_directory).to be == dir_path
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "should start with the given path" do
|
|
86
|
+
path = Utopia::Path["/a/b/c/d/e"]
|
|
87
|
+
|
|
88
|
+
expect(path.start_with?(path.dirname)).to be true
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "should split at the specified point" do
|
|
92
|
+
path = Utopia::Path["/a/b/c/d/e"]
|
|
93
|
+
|
|
94
|
+
expect(path.split('c')).to be == [Utopia::Path['/a/b'], Utopia::Path['d/e']]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "shouldn't be able to modify frozen paths" do
|
|
98
|
+
path = Utopia::Path["dir/foo.html"]
|
|
99
|
+
|
|
100
|
+
path.freeze
|
|
101
|
+
|
|
102
|
+
expect(path.frozen?).to be true
|
|
103
|
+
|
|
104
|
+
expect{path[0] = 'bob'}.to raise_exception(RuntimeError)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should give the correct variant" do
|
|
108
|
+
path = Utopia::Path["foo.en"]
|
|
109
|
+
|
|
110
|
+
expect(path.basename.variant).to be == 'en'
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "should give no variant" do
|
|
114
|
+
path = Utopia::Path["foo"]
|
|
115
|
+
|
|
116
|
+
expect(path.basename.variant).to be == nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "should expand relative paths" do
|
|
120
|
+
root = Utopia::Path['/root']
|
|
121
|
+
path = Utopia::Path["dir/foo.html"]
|
|
122
|
+
|
|
123
|
+
expect(path.expand(root)).to be == (root + path)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it "shouldn't expand absolute paths" do
|
|
127
|
+
root = Utopia::Path['/root']
|
|
128
|
+
path = Utopia::Path["dir/foo.html"]
|
|
129
|
+
|
|
130
|
+
expect(root.expand(root)).to be == root
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it "should give the shortest path for outer paths" do
|
|
134
|
+
input = Utopia::Path.create("/a/b/c/index")
|
|
135
|
+
output = Utopia::Path.create("/a/b/c/d/e/")
|
|
136
|
+
|
|
137
|
+
short = input.shortest_path(output)
|
|
138
|
+
|
|
139
|
+
expect(short.components).to be == ["..", "..", "index"]
|
|
140
|
+
|
|
141
|
+
expect((output + short).simplify).to be == input
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "should give the shortest path for inner paths" do
|
|
145
|
+
input = Utopia::Path.create("/a/b/c/index")
|
|
146
|
+
output = Utopia::Path.create("/a/")
|
|
147
|
+
|
|
148
|
+
short = input.shortest_path(output)
|
|
149
|
+
|
|
150
|
+
expect(short.components).to be == ["b", "c", "index"]
|
|
151
|
+
|
|
152
|
+
expect((output + short).simplify).to be == input
|
|
153
|
+
end
|
|
45
154
|
end
|
|
46
155
|
end
|
data/spec/utopia/rack_spec.rb
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env rspec
|
|
2
|
+
# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
3
|
+
#
|
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
# furnished to do so, subject to the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be included in
|
|
12
|
+
# all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
# THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require_relative 'spec_helper'
|
|
23
|
+
|
|
24
|
+
require 'rack'
|
|
25
|
+
require 'rack/test'
|
|
26
|
+
|
|
27
|
+
require 'utopia/session'
|
|
28
|
+
require 'utopia/session/encrypted_cookie'
|
|
29
|
+
|
|
30
|
+
module Utopia::SessionSpec
|
|
31
|
+
describe Utopia::Session do
|
|
32
|
+
include Rack::Test::Methods
|
|
33
|
+
|
|
34
|
+
let(:app) {Rack::Builder.parse_file(File.expand_path('../session_spec.ru', __FILE__)).first}
|
|
35
|
+
|
|
36
|
+
it "shouldn't commit session values unless required" do
|
|
37
|
+
# This URL doesn't update the session:
|
|
38
|
+
get "/"
|
|
39
|
+
expect(last_response.header).to be == {}
|
|
40
|
+
|
|
41
|
+
# This URL updates the session:
|
|
42
|
+
get "/login"
|
|
43
|
+
expect(last_response.header).to_not be == {}
|
|
44
|
+
expect(last_response.header).to be_include 'Set-Cookie'
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "should set and get values correctly" do
|
|
48
|
+
get "/session-set?key=foo&value=bar"
|
|
49
|
+
expect(last_response.header).to be_include 'Set-Cookie'
|
|
50
|
+
|
|
51
|
+
get "/session-get?key=foo"
|
|
52
|
+
expect(last_response.body).to be == "bar"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe Utopia::Session::LazyHash do
|
|
57
|
+
it "should load hash when required" do
|
|
58
|
+
loaded = false
|
|
59
|
+
|
|
60
|
+
hash = Utopia::Session::LazyHash.new do
|
|
61
|
+
loaded = true
|
|
62
|
+
{a: 10, b: 20}
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
expect(loaded).to be false
|
|
66
|
+
|
|
67
|
+
expect(hash[:a]).to be 10
|
|
68
|
+
|
|
69
|
+
expect(loaded).to be true
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "should delete the specified item" do
|
|
73
|
+
hash = Utopia::Session::LazyHash.new do
|
|
74
|
+
{a: 10, b: 20}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
expect(hash.include?(:a)).to be true
|
|
78
|
+
expect(hash.delete(:a)).to be 10
|
|
79
|
+
expect(hash.include?(:a)).to be false
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
use Utopia::Session::EncryptedCookie, secret: "97111cabf4c1a5e85b8029cf7c61aa44424fc24a"
|
|
3
|
+
|
|
4
|
+
run lambda { |env|
|
|
5
|
+
request = Rack::Request.new(env)
|
|
6
|
+
|
|
7
|
+
if env['PATH_INFO'] =~ /login/
|
|
8
|
+
env['rack.session']['login'] = 'true'
|
|
9
|
+
|
|
10
|
+
[200, {}, []]
|
|
11
|
+
elsif env['PATH_INFO'] =~ /session-set/
|
|
12
|
+
env['rack.session'][request[:key]] = request[:value]
|
|
13
|
+
|
|
14
|
+
[200, {}, []]
|
|
15
|
+
elsif env['PATH_INFO'] =~ /session-get/
|
|
16
|
+
[200, {}, [env['rack.session'][request[:key]]]]
|
|
17
|
+
else
|
|
18
|
+
[404, {}, []]
|
|
19
|
+
end
|
|
20
|
+
}
|