jellyfish 1.3.1 → 1.5.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/.gitlab-ci.yml +35 -0
- data/.gitmodules +1 -1
- data/CHANGES.md +27 -0
- data/Gemfile +5 -5
- data/README.md +75 -62
- data/config.ru +1 -2
- data/jellyfish.gemspec +5 -6
- data/lib/jellyfish/version.rb +1 -1
- data/lib/jellyfish.rb +42 -33
- data/task/README.md +4 -4
- data/task/gemgem.rb +3 -1
- data/test/rack/test_builder.rb +6 -6
- data/test/rack/test_urlmap.rb +86 -86
- data/test/sinatra/test_base.rb +6 -6
- data/test/sinatra/test_routing.rb +3 -3
- data/test/test_from_readme.rb +1 -1
- data/test/test_log.rb +5 -3
- metadata +71 -74
- data/.travis.yml +0 -18
- data/lib/jellyfish/public/302.html +0 -10
data/lib/jellyfish.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
|
|
2
3
|
module Jellyfish
|
|
3
4
|
autoload :VERSION , 'jellyfish/version'
|
|
@@ -10,16 +11,22 @@ module Jellyfish
|
|
|
10
11
|
autoload :ChunkedBody, 'jellyfish/chunked_body'
|
|
11
12
|
autoload :WebSocket , 'jellyfish/websocket'
|
|
12
13
|
|
|
13
|
-
Cascade = Object.new
|
|
14
|
-
GetValue = Object.new
|
|
14
|
+
Cascade = Object.new.freeze
|
|
15
|
+
GetValue = Object.new.freeze
|
|
15
16
|
|
|
16
17
|
class Response < RuntimeError
|
|
18
|
+
def self.contents status
|
|
19
|
+
(@contents ||= Hash.new do |hash, key|
|
|
20
|
+
hash[key] = File.read("#{Jellyfish.public_root}/#{status}.html")
|
|
21
|
+
end)[status]
|
|
22
|
+
end
|
|
23
|
+
|
|
17
24
|
def headers
|
|
18
|
-
@headers ||= {'
|
|
25
|
+
@headers ||= {'content-type' => 'text/html'}
|
|
19
26
|
end
|
|
20
27
|
|
|
21
28
|
def body
|
|
22
|
-
@body ||= [
|
|
29
|
+
@body ||= [self.class.contents(status)]
|
|
23
30
|
end
|
|
24
31
|
end
|
|
25
32
|
|
|
@@ -29,9 +36,10 @@ module Jellyfish
|
|
|
29
36
|
attr_reader :url
|
|
30
37
|
def initialize url; @url = url ; end
|
|
31
38
|
def status ; 302 ; end
|
|
32
|
-
def headers ; super.merge('
|
|
33
|
-
def body ;
|
|
39
|
+
def headers ; super.merge('location' => url) ; end
|
|
40
|
+
def body ; [] ; end
|
|
34
41
|
end
|
|
42
|
+
class Moved < Found ; def status; 301; end; end
|
|
35
43
|
|
|
36
44
|
# -----------------------------------------------------------------
|
|
37
45
|
|
|
@@ -55,14 +63,14 @@ module Jellyfish
|
|
|
55
63
|
raise
|
|
56
64
|
end
|
|
57
65
|
|
|
58
|
-
def log message; jellyfish.log( message, env
|
|
59
|
-
def log_error error; jellyfish.log_error(error, env
|
|
66
|
+
def log message; jellyfish.log( message, env); end
|
|
67
|
+
def log_error error; jellyfish.log_error(error, env); end
|
|
60
68
|
def request ; @request ||= Rack::Request.new(env); end
|
|
61
69
|
def halt *args; throw(:halt, *args) ; end
|
|
62
70
|
def cascade ; halt(Jellyfish::Cascade) ; end
|
|
63
71
|
def not_found ; halt(Jellyfish::NotFound.new) ; end
|
|
64
72
|
def found url; halt(Jellyfish:: Found.new(url)); end
|
|
65
|
-
|
|
73
|
+
def moved url; halt(Jellyfish:: Moved.new(url)); end
|
|
66
74
|
|
|
67
75
|
def path_info ; env['PATH_INFO'] || '/' ; end
|
|
68
76
|
def request_method; env['REQUEST_METHOD'] || 'GET'; end
|
|
@@ -190,7 +198,7 @@ module Jellyfish
|
|
|
190
198
|
when Cascade
|
|
191
199
|
cascade(ctrl, env)
|
|
192
200
|
when Response
|
|
193
|
-
handle(ctrl, res, env
|
|
201
|
+
handle(ctrl, res, env)
|
|
194
202
|
when Array
|
|
195
203
|
res
|
|
196
204
|
when NilClass # make sure we return rack triple
|
|
@@ -199,47 +207,48 @@ module Jellyfish
|
|
|
199
207
|
raise TypeError.new("Expect the response to be a Jellyfish::Response" \
|
|
200
208
|
" or Rack triple (Array), but got: #{res.inspect}")
|
|
201
209
|
end
|
|
202
|
-
rescue =>
|
|
203
|
-
handle(ctrl,
|
|
210
|
+
rescue => error
|
|
211
|
+
handle(ctrl, error, env)
|
|
204
212
|
end
|
|
205
213
|
|
|
206
|
-
def log_error
|
|
207
|
-
|
|
208
|
-
|
|
214
|
+
def log_error error, env
|
|
215
|
+
env['rack.errors']&.
|
|
216
|
+
puts("[#{self.class.name}] #{error.inspect}" \
|
|
217
|
+
" for #{env['PATH_INFO'] || '/'} #{error.backtrace}")
|
|
209
218
|
end
|
|
210
219
|
|
|
211
|
-
def log msg,
|
|
212
|
-
|
|
213
|
-
|
|
220
|
+
def log msg, env
|
|
221
|
+
env['rack.errors']&.
|
|
222
|
+
puts("[#{self.class.name}] #{msg}")
|
|
214
223
|
end
|
|
215
224
|
|
|
216
225
|
private
|
|
217
226
|
def cascade ctrl, env
|
|
218
227
|
app.call(env)
|
|
219
|
-
rescue =>
|
|
220
|
-
handle(ctrl,
|
|
228
|
+
rescue => error
|
|
229
|
+
handle(ctrl, error, env)
|
|
221
230
|
end
|
|
222
231
|
|
|
223
|
-
def handle ctrl,
|
|
224
|
-
if handler = best_handler(
|
|
225
|
-
ctrl.block_call(
|
|
232
|
+
def handle ctrl, error, env
|
|
233
|
+
if handler = best_handler(error)
|
|
234
|
+
ctrl.block_call(error, handler)
|
|
226
235
|
elsif !self.class.handle_exceptions
|
|
227
|
-
raise
|
|
228
|
-
elsif
|
|
229
|
-
[
|
|
236
|
+
raise error
|
|
237
|
+
elsif error.kind_of?(Response) # InternalError ends up here if no handlers
|
|
238
|
+
[error.status, error.headers, error.body]
|
|
230
239
|
else # fallback and see if there's any InternalError handler
|
|
231
|
-
log_error(
|
|
232
|
-
handle(ctrl, InternalError.new)
|
|
240
|
+
log_error(error, env)
|
|
241
|
+
handle(ctrl, InternalError.new, env)
|
|
233
242
|
end
|
|
234
243
|
end
|
|
235
244
|
|
|
236
|
-
def best_handler
|
|
245
|
+
def best_handler error
|
|
237
246
|
handlers = self.class.handlers
|
|
238
|
-
if handlers.key?(
|
|
239
|
-
handlers[
|
|
247
|
+
if handlers.key?(error.class)
|
|
248
|
+
handlers[error.class]
|
|
240
249
|
else # or find the nearest match and cache it
|
|
241
|
-
ancestors
|
|
242
|
-
handlers[
|
|
250
|
+
ancestors = error.class.ancestors
|
|
251
|
+
handlers[error.class] = handlers.dup. # thread safe iteration
|
|
243
252
|
inject([nil, Float::INFINITY]){ |(handler, val), (klass, block)|
|
|
244
253
|
idx = ancestors.index(klass) || Float::INFINITY # lower is better
|
|
245
254
|
if idx < val then [block, idx] else [handler, val] end }.first
|
data/task/README.md
CHANGED
|
@@ -13,11 +13,11 @@ Provided tasks:
|
|
|
13
13
|
|
|
14
14
|
## REQUIREMENTS:
|
|
15
15
|
|
|
16
|
-
* Tested with MRI (official CRuby)
|
|
16
|
+
* Tested with MRI (official CRuby) and JRuby.
|
|
17
17
|
|
|
18
18
|
## INSTALLATION:
|
|
19
19
|
|
|
20
|
-
git submodule add
|
|
20
|
+
git submodule add https://github.com/godfat/gemgem.git task
|
|
21
21
|
|
|
22
22
|
And in Rakefile:
|
|
23
23
|
|
|
@@ -39,13 +39,13 @@ end
|
|
|
39
39
|
|
|
40
40
|
Apache License 2.0 (Apache-2.0)
|
|
41
41
|
|
|
42
|
-
Copyright (c) 2011-
|
|
42
|
+
Copyright (c) 2011-2023, Lin Jen-Shin (godfat)
|
|
43
43
|
|
|
44
44
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
45
45
|
you may not use this file except in compliance with the License.
|
|
46
46
|
You may obtain a copy of the License at
|
|
47
47
|
|
|
48
|
-
<
|
|
48
|
+
<https://www.apache.org/licenses/LICENSE-2.0>
|
|
49
49
|
|
|
50
50
|
Unless required by applicable law or agreed to in writing, software
|
|
51
51
|
distributed under the License is distributed on an "AS IS" BASIS,
|
data/task/gemgem.rb
CHANGED
|
@@ -42,6 +42,7 @@ module Gemgem
|
|
|
42
42
|
|
|
43
43
|
def gem_install
|
|
44
44
|
require 'rubygems/commands/install_command'
|
|
45
|
+
require 'rubygems/package'
|
|
45
46
|
# read ~/.gemrc
|
|
46
47
|
Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
|
|
47
48
|
Gem::Command.extra_args = Gem.configuration[:gem]
|
|
@@ -51,7 +52,8 @@ module Gemgem
|
|
|
51
52
|
cmd.handle_options([])
|
|
52
53
|
|
|
53
54
|
# install
|
|
54
|
-
|
|
55
|
+
gem_package = Gem::Package.new(gem_path)
|
|
56
|
+
install = Gem::Installer.new(gem_package, cmd.options)
|
|
55
57
|
install.install
|
|
56
58
|
puts "\e[35mGem installed: \e[33m#{strip_path(install.gem_dir)}\e[0m"
|
|
57
59
|
end
|
data/test/rack/test_builder.rb
CHANGED
|
@@ -28,10 +28,10 @@ describe Jellyfish::Builder do
|
|
|
28
28
|
would "supports mapping" do
|
|
29
29
|
app = builder_to_app do
|
|
30
30
|
map '/' do |outer_env|
|
|
31
|
-
run lambda { |inner_env| [200, {"
|
|
31
|
+
run lambda { |inner_env| [200, {"content-type" => "text/plain"}, ['root']] }
|
|
32
32
|
end
|
|
33
33
|
map '/sub' do
|
|
34
|
-
run lambda { |inner_env| [200, {"
|
|
34
|
+
run lambda { |inner_env| [200, {"content-type" => "text/plain"}, ['sub']] }
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
Rack::MockRequest.new(app).get("/").body.to_s.should.eq 'root'
|
|
@@ -56,7 +56,7 @@ describe Jellyfish::Builder do
|
|
|
56
56
|
'secret' == password
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
-
run lambda { |env| [200, {"
|
|
59
|
+
run lambda { |env| [200, {"content-type" => "text/plain"}, ['Hi Boss']] }
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
response = Rack::MockRequest.new(app).get("/")
|
|
@@ -84,9 +84,9 @@ describe Jellyfish::Builder do
|
|
|
84
84
|
would "can mix map and run for endpoints" do
|
|
85
85
|
app = builder_to_app do
|
|
86
86
|
map '/sub' do
|
|
87
|
-
run lambda { |inner_env| [200, {"
|
|
87
|
+
run lambda { |inner_env| [200, {"content-type" => "text/plain"}, ['sub']] }
|
|
88
88
|
end
|
|
89
|
-
run lambda { |inner_env| [200, {"
|
|
89
|
+
run lambda { |inner_env| [200, {"content-type" => "text/plain"}, ['root']] }
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
Rack::MockRequest.new(app).get("/").body.to_s.should.eq 'root'
|
|
@@ -123,7 +123,7 @@ describe Jellyfish::Builder do
|
|
|
123
123
|
def call(env)
|
|
124
124
|
raise "bzzzt" if @called > 0
|
|
125
125
|
@called += 1
|
|
126
|
-
[200, {'
|
|
126
|
+
[200, {'content-type' => 'text/plain'}, ['OK']]
|
|
127
127
|
end
|
|
128
128
|
end
|
|
129
129
|
|
data/test/rack/test_urlmap.rb
CHANGED
|
@@ -8,9 +8,9 @@ describe Jellyfish::URLMap do
|
|
|
8
8
|
would "dispatches paths correctly" do
|
|
9
9
|
app = lambda { |env|
|
|
10
10
|
[200, {
|
|
11
|
-
'
|
|
12
|
-
'
|
|
13
|
-
'
|
|
11
|
+
'x-scriptname' => env['SCRIPT_NAME'],
|
|
12
|
+
'x-pathinfo' => env['PATH_INFO'],
|
|
13
|
+
'content-type' => 'text/plain'
|
|
14
14
|
}, [""]]
|
|
15
15
|
}
|
|
16
16
|
map = Rack::Lint.new(Jellyfish::URLMap.new({
|
|
@@ -27,102 +27,102 @@ describe Jellyfish::URLMap do
|
|
|
27
27
|
|
|
28
28
|
res = Rack::MockRequest.new(map).get("/foo")
|
|
29
29
|
res.should.ok?
|
|
30
|
-
res["
|
|
31
|
-
res["
|
|
30
|
+
res["x-scriptname"].should.eq "/foo"
|
|
31
|
+
res["x-pathinfo"].should.eq ""
|
|
32
32
|
|
|
33
33
|
res = Rack::MockRequest.new(map).get("/foo/")
|
|
34
34
|
res.should.ok?
|
|
35
|
-
res["
|
|
36
|
-
res["
|
|
35
|
+
res["x-scriptname"].should.eq "/foo"
|
|
36
|
+
res["x-pathinfo"].should.eq "/"
|
|
37
37
|
|
|
38
38
|
res = Rack::MockRequest.new(map).get("/foo/bar")
|
|
39
39
|
res.should.ok?
|
|
40
|
-
res["
|
|
41
|
-
res["
|
|
40
|
+
res["x-scriptname"].should.eq "/foo/bar"
|
|
41
|
+
res["x-pathinfo"].should.eq ""
|
|
42
42
|
|
|
43
43
|
res = Rack::MockRequest.new(map).get("/foo/bar/")
|
|
44
44
|
res.should.ok?
|
|
45
|
-
res["
|
|
46
|
-
res["
|
|
45
|
+
res["x-scriptname"].should.eq "/foo/bar"
|
|
46
|
+
res["x-pathinfo"].should.eq "/"
|
|
47
47
|
|
|
48
48
|
res = Rack::MockRequest.new(map).get("/foo///bar//quux")
|
|
49
49
|
res.status.should.eq 200
|
|
50
50
|
res.should.ok?
|
|
51
|
-
res["
|
|
52
|
-
res["
|
|
51
|
+
res["x-scriptname"].should.eq "/foo/bar"
|
|
52
|
+
res["x-pathinfo"].should.eq "//quux"
|
|
53
53
|
|
|
54
54
|
res = Rack::MockRequest.new(map).get("/foo/quux", "SCRIPT_NAME" => "/bleh")
|
|
55
55
|
res.should.ok?
|
|
56
|
-
res["
|
|
57
|
-
res["
|
|
56
|
+
res["x-scriptname"].should.eq "/bleh/foo"
|
|
57
|
+
res["x-pathinfo"].should.eq "/quux"
|
|
58
58
|
|
|
59
59
|
res = Rack::MockRequest.new(map).get("/bar", 'HTTP_HOST' => 'foo.org')
|
|
60
60
|
res.should.ok?
|
|
61
|
-
res["
|
|
62
|
-
res["
|
|
61
|
+
res["x-scriptname"].should.eq "/bar"
|
|
62
|
+
res["x-pathinfo"].should.empty?
|
|
63
63
|
|
|
64
64
|
res = Rack::MockRequest.new(map).get("/bar/", 'HTTP_HOST' => 'foo.org')
|
|
65
65
|
res.should.ok?
|
|
66
|
-
res["
|
|
67
|
-
res["
|
|
66
|
+
res["x-scriptname"].should.eq "/bar"
|
|
67
|
+
res["x-pathinfo"].should.eq '/'
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
would "dispatches hosts correctly" do
|
|
71
71
|
map = Rack::Lint.new(Jellyfish::URLMap.new("http://foo.org/" => lambda { |env|
|
|
72
72
|
[200,
|
|
73
|
-
{ "
|
|
74
|
-
"
|
|
75
|
-
"
|
|
73
|
+
{ "content-type" => "text/plain",
|
|
74
|
+
"x-position" => "foo.org",
|
|
75
|
+
"x-host" => env["HTTP_HOST"] || env["SERVER_NAME"],
|
|
76
76
|
}, [""]]},
|
|
77
77
|
"http://subdomain.foo.org/" => lambda { |env|
|
|
78
78
|
[200,
|
|
79
|
-
{ "
|
|
80
|
-
"
|
|
81
|
-
"
|
|
79
|
+
{ "content-type" => "text/plain",
|
|
80
|
+
"x-position" => "subdomain.foo.org",
|
|
81
|
+
"x-host" => env["HTTP_HOST"] || env["SERVER_NAME"],
|
|
82
82
|
}, [""]]},
|
|
83
83
|
"http://bar.org/" => lambda { |env|
|
|
84
84
|
[200,
|
|
85
|
-
{ "
|
|
86
|
-
"
|
|
87
|
-
"
|
|
85
|
+
{ "content-type" => "text/plain",
|
|
86
|
+
"x-position" => "bar.org",
|
|
87
|
+
"x-host" => env["HTTP_HOST"] || env["SERVER_NAME"],
|
|
88
88
|
}, [""]]},
|
|
89
89
|
"/" => lambda { |env|
|
|
90
90
|
[200,
|
|
91
|
-
{ "
|
|
92
|
-
"
|
|
93
|
-
"
|
|
91
|
+
{ "content-type" => "text/plain",
|
|
92
|
+
"x-position" => "default.org",
|
|
93
|
+
"x-host" => env["HTTP_HOST"] || env["SERVER_NAME"],
|
|
94
94
|
}, [""]]}
|
|
95
95
|
))
|
|
96
96
|
|
|
97
97
|
res = Rack::MockRequest.new(map).get("/")
|
|
98
98
|
res.should.ok?
|
|
99
|
-
res["
|
|
99
|
+
res["x-position"].should.eq "default.org"
|
|
100
100
|
|
|
101
101
|
res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "bar.org")
|
|
102
102
|
res.should.ok?
|
|
103
|
-
res["
|
|
103
|
+
res["x-position"].should.eq "bar.org"
|
|
104
104
|
|
|
105
105
|
res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "foo.org")
|
|
106
106
|
res.should.ok?
|
|
107
|
-
res["
|
|
107
|
+
res["x-position"].should.eq "foo.org"
|
|
108
108
|
|
|
109
109
|
res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "subdomain.foo.org", "SERVER_NAME" => "foo.org")
|
|
110
110
|
res.should.ok?
|
|
111
|
-
res["
|
|
111
|
+
res["x-position"].should.eq "subdomain.foo.org"
|
|
112
112
|
|
|
113
113
|
res = Rack::MockRequest.new(map).get("http://foo.org/")
|
|
114
114
|
res.should.ok?
|
|
115
|
-
res["
|
|
115
|
+
res["x-position"].should.eq "foo.org"
|
|
116
116
|
|
|
117
117
|
res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "example.org")
|
|
118
118
|
res.should.ok?
|
|
119
|
-
res["
|
|
119
|
+
res["x-position"].should.eq "default.org"
|
|
120
120
|
|
|
121
121
|
res = Rack::MockRequest.new(map).get("/",
|
|
122
122
|
"HTTP_HOST" => "example.org:9292",
|
|
123
123
|
"SERVER_PORT" => "9292")
|
|
124
124
|
res.should.ok?
|
|
125
|
-
res["
|
|
125
|
+
res["x-position"].should.eq "default.org"
|
|
126
126
|
end
|
|
127
127
|
|
|
128
128
|
would "be nestable" do
|
|
@@ -130,10 +130,10 @@ describe Jellyfish::URLMap do
|
|
|
130
130
|
Jellyfish::URLMap.new("/bar" =>
|
|
131
131
|
Jellyfish::URLMap.new("/quux" => lambda { |env|
|
|
132
132
|
[200,
|
|
133
|
-
{ "
|
|
134
|
-
"
|
|
135
|
-
"
|
|
136
|
-
"
|
|
133
|
+
{ "content-type" => "text/plain",
|
|
134
|
+
"x-position" => "/foo/bar/quux",
|
|
135
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
136
|
+
"x-scriptname" => env["SCRIPT_NAME"],
|
|
137
137
|
}, [""]]}
|
|
138
138
|
))))
|
|
139
139
|
|
|
@@ -142,97 +142,97 @@ describe Jellyfish::URLMap do
|
|
|
142
142
|
|
|
143
143
|
res = Rack::MockRequest.new(map).get("/foo/bar/quux")
|
|
144
144
|
res.should.ok?
|
|
145
|
-
res["
|
|
146
|
-
res["
|
|
147
|
-
res["
|
|
145
|
+
res["x-position"].should.eq "/foo/bar/quux"
|
|
146
|
+
res["x-pathinfo"].should.eq ""
|
|
147
|
+
res["x-scriptname"].should.eq "/foo/bar/quux"
|
|
148
148
|
end
|
|
149
149
|
|
|
150
150
|
would "route root apps correctly" do
|
|
151
151
|
map = Rack::Lint.new(Jellyfish::URLMap.new("/" => lambda { |env|
|
|
152
152
|
[200,
|
|
153
|
-
{ "
|
|
154
|
-
"
|
|
155
|
-
"
|
|
156
|
-
"
|
|
153
|
+
{ "content-type" => "text/plain",
|
|
154
|
+
"x-position" => "root",
|
|
155
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
156
|
+
"x-scriptname" => env["SCRIPT_NAME"]
|
|
157
157
|
}, [""]]},
|
|
158
158
|
"/foo" => lambda { |env|
|
|
159
159
|
[200,
|
|
160
|
-
{ "
|
|
161
|
-
"
|
|
162
|
-
"
|
|
163
|
-
"
|
|
160
|
+
{ "content-type" => "text/plain",
|
|
161
|
+
"x-position" => "foo",
|
|
162
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
163
|
+
"x-scriptname" => env["SCRIPT_NAME"]
|
|
164
164
|
}, [""]]}
|
|
165
165
|
))
|
|
166
166
|
|
|
167
167
|
res = Rack::MockRequest.new(map).get("/foo/bar")
|
|
168
168
|
res.should.ok?
|
|
169
|
-
res["
|
|
170
|
-
res["
|
|
171
|
-
res["
|
|
169
|
+
res["x-position"].should.eq "foo"
|
|
170
|
+
res["x-pathinfo"].should.eq "/bar"
|
|
171
|
+
res["x-scriptname"].should.eq "/foo"
|
|
172
172
|
|
|
173
173
|
res = Rack::MockRequest.new(map).get("/foo")
|
|
174
174
|
res.should.ok?
|
|
175
|
-
res["
|
|
176
|
-
res["
|
|
177
|
-
res["
|
|
175
|
+
res["x-position"].should.eq "foo"
|
|
176
|
+
res["x-pathinfo"].should.eq ""
|
|
177
|
+
res["x-scriptname"].should.eq "/foo"
|
|
178
178
|
|
|
179
179
|
res = Rack::MockRequest.new(map).get("/bar")
|
|
180
180
|
res.should.ok?
|
|
181
|
-
res["
|
|
182
|
-
res["
|
|
183
|
-
res["
|
|
181
|
+
res["x-position"].should.eq "root"
|
|
182
|
+
res["x-pathinfo"].should.eq "/bar"
|
|
183
|
+
res["x-scriptname"].should.eq ""
|
|
184
184
|
|
|
185
185
|
res = Rack::MockRequest.new(map).get("")
|
|
186
186
|
res.should.ok?
|
|
187
|
-
res["
|
|
188
|
-
res["
|
|
189
|
-
res["
|
|
187
|
+
res["x-position"].should.eq "root"
|
|
188
|
+
res["x-pathinfo"].should.eq "/"
|
|
189
|
+
res["x-scriptname"].should.eq ""
|
|
190
190
|
end
|
|
191
191
|
|
|
192
192
|
would "not squeeze slashes" do
|
|
193
193
|
map = Rack::Lint.new(Jellyfish::URLMap.new("/" => lambda { |env|
|
|
194
194
|
[200,
|
|
195
|
-
{ "
|
|
196
|
-
"
|
|
197
|
-
"
|
|
198
|
-
"
|
|
195
|
+
{ "content-type" => "text/plain",
|
|
196
|
+
"x-position" => "root",
|
|
197
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
198
|
+
"x-scriptname" => env["SCRIPT_NAME"]
|
|
199
199
|
}, [""]]},
|
|
200
200
|
"/foo" => lambda { |env|
|
|
201
201
|
[200,
|
|
202
|
-
{ "
|
|
203
|
-
"
|
|
204
|
-
"
|
|
205
|
-
"
|
|
202
|
+
{ "content-type" => "text/plain",
|
|
203
|
+
"x-position" => "foo",
|
|
204
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
205
|
+
"x-scriptname" => env["SCRIPT_NAME"]
|
|
206
206
|
}, [""]]}
|
|
207
207
|
))
|
|
208
208
|
|
|
209
209
|
res = Rack::MockRequest.new(map).get("/http://example.org/bar")
|
|
210
210
|
res.should.ok?
|
|
211
|
-
res["
|
|
212
|
-
res["
|
|
213
|
-
res["
|
|
211
|
+
res["x-position"].should.eq "root"
|
|
212
|
+
res["x-pathinfo"].should.eq "/http://example.org/bar"
|
|
213
|
+
res["x-scriptname"].should.eq ""
|
|
214
214
|
end
|
|
215
215
|
|
|
216
216
|
would "not be case sensitive with hosts" do
|
|
217
217
|
map = Rack::Lint.new(Jellyfish::URLMap.new("http://example.org/" => lambda { |env|
|
|
218
218
|
[200,
|
|
219
|
-
{ "
|
|
220
|
-
"
|
|
221
|
-
"
|
|
222
|
-
"
|
|
219
|
+
{ "content-type" => "text/plain",
|
|
220
|
+
"x-position" => "root",
|
|
221
|
+
"x-pathinfo" => env["PATH_INFO"],
|
|
222
|
+
"x-scriptname" => env["SCRIPT_NAME"]
|
|
223
223
|
}, [""]]}
|
|
224
224
|
))
|
|
225
225
|
|
|
226
226
|
res = Rack::MockRequest.new(map).get("http://example.org/")
|
|
227
227
|
res.should.ok?
|
|
228
|
-
res["
|
|
229
|
-
res["
|
|
230
|
-
res["
|
|
228
|
+
res["x-position"].should.eq "root"
|
|
229
|
+
res["x-pathinfo"].should.eq "/"
|
|
230
|
+
res["x-scriptname"].should.eq ""
|
|
231
231
|
|
|
232
232
|
res = Rack::MockRequest.new(map).get("http://EXAMPLE.ORG/")
|
|
233
233
|
res.should.ok?
|
|
234
|
-
res["
|
|
235
|
-
res["
|
|
236
|
-
res["
|
|
234
|
+
res["x-position"].should.eq "root"
|
|
235
|
+
res["x-pathinfo"].should.eq "/"
|
|
236
|
+
res["x-scriptname"].should.eq ""
|
|
237
237
|
end
|
|
238
238
|
end
|
data/test/sinatra/test_base.rb
CHANGED
|
@@ -38,7 +38,7 @@ describe 'Sinatra base_test.rb' do
|
|
|
38
38
|
|
|
39
39
|
describe 'Jellyfish as a Rack middleware' do
|
|
40
40
|
inner_app ||= lambda{ |env|
|
|
41
|
-
[210, {'
|
|
41
|
+
[210, {'x-downstream' => 'true'}, ['Hello from downstream']]
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
app = Class.new{
|
|
@@ -55,7 +55,7 @@ describe 'Sinatra base_test.rb' do
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
get '/explicit-forward' do
|
|
58
|
-
headers_merge '
|
|
58
|
+
headers_merge 'x-middleware' => 'true'
|
|
59
59
|
status, headers, _ = jellyfish.app.call(env)
|
|
60
60
|
self.status status
|
|
61
61
|
self.headers headers
|
|
@@ -80,14 +80,14 @@ describe 'Sinatra base_test.rb' do
|
|
|
80
80
|
would 'forward requests downstream when no matching route found' do
|
|
81
81
|
status, headers, body = get('/missing', app)
|
|
82
82
|
status .should.eq 210
|
|
83
|
-
headers['
|
|
83
|
+
headers['x-downstream'].should.eq 'true'
|
|
84
84
|
body .should.eq ['Hello from downstream']
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
would 'call the downstream app directly and return result' do
|
|
88
88
|
status, headers, body = get('/low-level-forward', app)
|
|
89
89
|
status .should.eq 210
|
|
90
|
-
headers['
|
|
90
|
+
headers['x-downstream'].should.eq 'true'
|
|
91
91
|
body .should.eq ['Hello from downstream']
|
|
92
92
|
end
|
|
93
93
|
|
|
@@ -96,8 +96,8 @@ describe 'Sinatra base_test.rb' do
|
|
|
96
96
|
get('/explicit-forward', Rack::ContentLength.new(app))
|
|
97
97
|
|
|
98
98
|
status .should.eq 210
|
|
99
|
-
headers['
|
|
100
|
-
headers['
|
|
99
|
+
headers['x-downstream'] .should.eq 'true'
|
|
100
|
+
headers['content-length'].should.eq '28'
|
|
101
101
|
body.to_a .should.eq ['Hello after explicit forward']
|
|
102
102
|
end
|
|
103
103
|
end
|
|
@@ -70,7 +70,7 @@ describe 'Sinatra routing_test.rb' do
|
|
|
70
70
|
app = Class.new{
|
|
71
71
|
include Jellyfish
|
|
72
72
|
get{
|
|
73
|
-
self.headers '
|
|
73
|
+
self.headers 'content-type' => 'text/plain'
|
|
74
74
|
status, headers, body = jellyfish.app.call(env)
|
|
75
75
|
self.status status
|
|
76
76
|
self.body body
|
|
@@ -79,7 +79,7 @@ describe 'Sinatra routing_test.rb' do
|
|
|
79
79
|
}.new(Class.new{
|
|
80
80
|
include Jellyfish
|
|
81
81
|
handle Jellyfish::NotFound do
|
|
82
|
-
headers_merge '
|
|
82
|
+
headers_merge 'content-type' => 'text/html'
|
|
83
83
|
status 404
|
|
84
84
|
'<h1>Not Found</h1>'
|
|
85
85
|
end
|
|
@@ -87,7 +87,7 @@ describe 'Sinatra routing_test.rb' do
|
|
|
87
87
|
|
|
88
88
|
status, headers, body = get('/foo', app)
|
|
89
89
|
status .should.eq 404
|
|
90
|
-
headers['
|
|
90
|
+
headers['content-type'].should.eq 'text/html'
|
|
91
91
|
body .should.eq ['<h1>Not Found</h1>']
|
|
92
92
|
end
|
|
93
93
|
|
data/test/test_from_readme.rb
CHANGED
|
@@ -30,7 +30,7 @@ describe 'from README.md' do
|
|
|
30
30
|
sock = nil
|
|
31
31
|
status, headers, body = File.open(File::NULL) do |input|
|
|
32
32
|
app.call(
|
|
33
|
-
'
|
|
33
|
+
'SERVER_PROTOCOL'=> 'HTTP/1.1',
|
|
34
34
|
'REQUEST_METHOD' => method,
|
|
35
35
|
'HTTP_HOST' => host,
|
|
36
36
|
'PATH_INFO' => pinfo,
|