goliath 1.0.4 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of goliath might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +5 -4
- data/goliath.gemspec +11 -7
- data/lib/goliath/api.rb +1 -1
- data/lib/goliath/connection.rb +15 -11
- data/lib/goliath/constants.rb +1 -0
- data/lib/goliath/rack/default_response_format.rb +3 -9
- data/lib/goliath/rack/formatters/json.rb +1 -1
- data/lib/goliath/rack/jsonp.rb +7 -2
- data/lib/goliath/rack/params.rb +4 -3
- data/lib/goliath/rack/validator.rb +1 -1
- data/lib/goliath/request.rb +37 -20
- data/lib/goliath/response.rb +2 -0
- data/lib/goliath/runner.rb +4 -3
- data/lib/goliath/server.rb +17 -2
- data/lib/goliath/test_helper_sse.rb +76 -0
- data/lib/goliath/validation/error.rb +4 -1
- data/lib/goliath/version.rb +1 -1
- data/spec/integration/async_request_processing.rb +2 -2
- data/spec/integration/chunked_streaming_spec.rb +1 -1
- data/spec/integration/early_abort_spec.rb +6 -6
- data/spec/integration/echo_spec.rb +7 -7
- data/spec/integration/empty_body_spec.rb +2 -2
- data/spec/integration/event_stream_spec.rb +50 -0
- data/spec/integration/exception_handling_spec.rb +202 -0
- data/spec/integration/http_log_spec.rb +16 -16
- data/spec/integration/jsonp_spec.rb +61 -10
- data/spec/integration/keepalive_spec.rb +2 -2
- data/spec/integration/pipelining_spec.rb +3 -3
- data/spec/integration/reloader_spec.rb +3 -3
- data/spec/integration/template_spec.rb +7 -7
- data/spec/integration/test_helper_spec.rb +3 -3
- data/spec/integration/trace_spec.rb +2 -2
- data/spec/integration/valid_spec.rb +25 -5
- data/spec/integration/websocket_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -3
- data/spec/unit/api_spec.rb +1 -1
- data/spec/unit/connection_spec.rb +8 -8
- data/spec/unit/console_spec.rb +3 -3
- data/spec/unit/env_spec.rb +9 -9
- data/spec/unit/headers_spec.rb +8 -8
- data/spec/unit/rack/default_mime_type_spec.rb +3 -3
- data/spec/unit/rack/formatters/json_spec.rb +35 -13
- data/spec/unit/rack/formatters/plist_spec.rb +8 -8
- data/spec/unit/rack/formatters/xml_spec.rb +18 -18
- data/spec/unit/rack/formatters/yaml_spec.rb +13 -13
- data/spec/unit/rack/heartbeat_spec.rb +15 -15
- data/spec/unit/rack/params_spec.rb +99 -62
- data/spec/unit/rack/render_spec.rb +14 -14
- data/spec/unit/rack/validation/boolean_value_spec.rb +6 -6
- data/spec/unit/rack/validation/default_params_spec.rb +13 -13
- data/spec/unit/rack/validation/numeric_range_spec.rb +17 -17
- data/spec/unit/rack/validation/param_spec.rb +75 -75
- data/spec/unit/rack/validation/request_method_spec.rb +9 -9
- data/spec/unit/rack/validation/required_param_spec.rb +39 -39
- data/spec/unit/rack/validation/required_value_spec.rb +19 -19
- data/spec/unit/request_spec.rb +18 -18
- data/spec/unit/response_spec.rb +6 -6
- data/spec/unit/runner_spec.rb +31 -31
- data/spec/unit/server_spec.rb +21 -21
- data/spec/unit/validation/standard_http_errors_spec.rb +6 -6
- metadata +149 -94
- data/examples/around.rb +0 -38
- data/examples/clone.rb +0 -26
- data/examples/router.rb +0 -15
- data/examples/test.rb +0 -31
- data/examples/upload.rb +0 -17
@@ -3,7 +3,7 @@ require 'goliath/rack/params'
|
|
3
3
|
|
4
4
|
describe Goliath::Rack::Params do
|
5
5
|
it 'accepts an app' do
|
6
|
-
|
6
|
+
expect { Goliath::Rack::Params.new('my app') }.not_to raise_error
|
7
7
|
end
|
8
8
|
|
9
9
|
describe 'with middleware' do
|
@@ -21,19 +21,26 @@ describe Goliath::Rack::Params do
|
|
21
21
|
}.to raise_error(Goliath::Validation::BadRequestError)
|
22
22
|
end
|
23
23
|
|
24
|
+
it 'handles ambiguous query strings' do
|
25
|
+
@env['QUERY_STRING'] = 'ambiguous[]=&ambiguous[4]='
|
26
|
+
expect {
|
27
|
+
@params.retrieve_params(@env)
|
28
|
+
}.to raise_error(Goliath::Validation::Error)
|
29
|
+
end
|
30
|
+
|
24
31
|
it 'parses the query string' do
|
25
32
|
@env['QUERY_STRING'] = 'foo=bar&baz=bonkey'
|
26
33
|
|
27
34
|
ret = @params.retrieve_params(@env)
|
28
|
-
ret['foo'].
|
29
|
-
ret['baz'].
|
35
|
+
expect(ret['foo']).to eq('bar')
|
36
|
+
expect(ret['baz']).to eq('bonkey')
|
30
37
|
end
|
31
38
|
|
32
39
|
it 'parses the nested query string' do
|
33
40
|
@env['QUERY_STRING'] = 'foo[bar]=baz'
|
34
41
|
|
35
42
|
ret = @params.retrieve_params(@env)
|
36
|
-
ret['foo'].
|
43
|
+
expect(ret['foo']).to eq({'bar' => 'baz'})
|
37
44
|
end
|
38
45
|
|
39
46
|
it 'parses the post body' do
|
@@ -42,18 +49,18 @@ describe Goliath::Rack::Params do
|
|
42
49
|
@env['rack.input'].rewind
|
43
50
|
|
44
51
|
ret = @params.retrieve_params(@env)
|
45
|
-
ret['foo'].
|
46
|
-
ret['baz'].
|
47
|
-
ret['zonk'].
|
52
|
+
expect(ret['foo']).to eq('bar')
|
53
|
+
expect(ret['baz']).to eq('bonkey')
|
54
|
+
expect(ret['zonk']).to eq({'donk' => 'monk'})
|
48
55
|
end
|
49
56
|
|
50
57
|
it 'parses arrays of data' do
|
51
58
|
@env['QUERY_STRING'] = 'foo[]=bar&foo[]=baz&foo[]=foos'
|
52
59
|
|
53
60
|
ret = @params.retrieve_params(@env)
|
54
|
-
ret['foo'].is_a?(Array).
|
55
|
-
ret['foo'].length.
|
56
|
-
ret['foo'].
|
61
|
+
expect(ret['foo'].is_a?(Array)).to be true
|
62
|
+
expect(ret['foo'].length).to eq(3)
|
63
|
+
expect(ret['foo']).to eq(%w(bar baz foos))
|
57
64
|
end
|
58
65
|
|
59
66
|
it 'parses multipart data' do
|
@@ -73,8 +80,8 @@ Berry\r
|
|
73
80
|
@env[Goliath::Constants::CONTENT_LENGTH] = @env['rack.input'].length
|
74
81
|
|
75
82
|
ret = @params.retrieve_params(@env)
|
76
|
-
ret['submit-name'].
|
77
|
-
ret['submit-name-with-content'].
|
83
|
+
expect(ret['submit-name']).to eq('Larry')
|
84
|
+
expect(ret['submit-name-with-content']).to eq('Berry')
|
78
85
|
end
|
79
86
|
|
80
87
|
it 'combines query string and post body params' do
|
@@ -85,14 +92,14 @@ Berry\r
|
|
85
92
|
@env['rack.input'].rewind
|
86
93
|
|
87
94
|
ret = @params.retrieve_params(@env)
|
88
|
-
ret['baz'].
|
89
|
-
ret['foos'].
|
95
|
+
expect(ret['baz']).to eq('bar')
|
96
|
+
expect(ret['foos']).to eq('bonkey')
|
90
97
|
end
|
91
98
|
|
92
99
|
it 'handles empty query and post body' do
|
93
100
|
ret = @params.retrieve_params(@env)
|
94
|
-
ret.is_a?(Hash).
|
95
|
-
ret.
|
101
|
+
expect(ret.is_a?(Hash)).to be true
|
102
|
+
expect(ret).to be_empty
|
96
103
|
end
|
97
104
|
|
98
105
|
it 'prefers post body over query string' do
|
@@ -103,15 +110,12 @@ Berry\r
|
|
103
110
|
@env['rack.input'].rewind
|
104
111
|
|
105
112
|
ret = @params.retrieve_params(@env)
|
106
|
-
ret['foo'].
|
107
|
-
ret['baz'].
|
113
|
+
expect(ret['foo']).to eq('bar2')
|
114
|
+
expect(ret['baz']).to eq('bonkey')
|
108
115
|
end
|
109
116
|
|
110
117
|
it 'sets the params into the environment' do
|
111
|
-
@app.
|
112
|
-
app_env.has_key?('params').should be_true
|
113
|
-
app_env['params']['a'].should == 'b'
|
114
|
-
end
|
118
|
+
expect(@app).to receive(:call).with(hash_including("params"=>{"a"=>"b"}))
|
115
119
|
|
116
120
|
@env['QUERY_STRING'] = "a=b"
|
117
121
|
@params.call(@env)
|
@@ -120,60 +124,93 @@ Berry\r
|
|
120
124
|
it 'returns status, headers and body from the app' do
|
121
125
|
app_headers = {'Content-Type' => 'hash'}
|
122
126
|
app_body = {:a => 1, :b => 2}
|
123
|
-
@app.
|
127
|
+
expect(@app).to receive(:call).and_return([200, app_headers, app_body])
|
124
128
|
|
125
129
|
status, headers, body = @params.call(@env)
|
126
|
-
status.
|
127
|
-
headers.
|
128
|
-
body.
|
130
|
+
expect(status).to eq(200)
|
131
|
+
expect(headers).to eq(app_headers)
|
132
|
+
expect(body).to eq(app_body)
|
129
133
|
end
|
130
134
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
135
|
+
it 'returns a validation error if one is raised while parsing' do
|
136
|
+
expect(@app).to_not receive(:call)
|
137
|
+
params_exception = Goliath::Validation::Error.new(423, 'invalid')
|
138
|
+
expect(@params).to receive(:retrieve_params).and_raise(params_exception)
|
139
|
+
status, headers, body = @params.call(@env)
|
140
|
+
expect(status).to eq 423
|
141
|
+
expect(headers).to eq({})
|
142
|
+
expect(body).to eq(error: 'invalid')
|
143
|
+
end
|
137
144
|
|
138
|
-
|
139
|
-
|
140
|
-
end
|
145
|
+
it 'returns a 500 error if an unexpected error is raised while parsing' do
|
146
|
+
expect(@app).to_not receive(:call)
|
141
147
|
|
142
|
-
|
143
|
-
|
144
|
-
@env['rack.input'] = StringIO.new
|
145
|
-
@env['rack.input'] << %|{"foo":"bar"}|
|
146
|
-
@env['rack.input'].rewind
|
148
|
+
params_exception = Exception.new('uh oh')
|
149
|
+
expect(@params).to receive(:retrieve_params).and_raise(params_exception)
|
147
150
|
|
148
|
-
|
149
|
-
|
150
|
-
end
|
151
|
+
logger = double('logger').as_null_object
|
152
|
+
expect(@env).to receive(:logger).twice.and_return(logger)
|
151
153
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
154
|
+
status, headers, body = @params.call(@env)
|
155
|
+
expect(status).to eq 500
|
156
|
+
expect(headers).to eq({})
|
157
|
+
expect(body).to eq(error: 'uh oh')
|
158
|
+
end
|
157
159
|
|
158
|
-
|
159
|
-
|
160
|
-
|
160
|
+
it 'does not swallow exceptions from the app' do
|
161
|
+
app_exception = Class.new(Exception)
|
162
|
+
expect(@app).to receive(:call).and_raise(app_exception)
|
163
|
+
expect { @params.call(@env) }.to raise_error(app_exception)
|
164
|
+
end
|
161
165
|
|
162
|
-
|
163
|
-
|
166
|
+
context 'content type' do
|
167
|
+
it "parses application/x-www-form-urlencoded" do
|
168
|
+
@env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
164
169
|
@env['rack.input'] = StringIO.new
|
170
|
+
@env['rack.input'] << "foos=bonkey"
|
171
|
+
@env['rack.input'].rewind
|
165
172
|
|
166
173
|
ret = @params.retrieve_params(@env)
|
167
|
-
ret.
|
174
|
+
expect(ret['foos']).to eq('bonkey')
|
168
175
|
end
|
169
176
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
+
['application/json', 'application/vnd.api+json', 'application/javascript'].each do |content_type|
|
178
|
+
it "parses #{content_type}" do
|
179
|
+
@env['CONTENT_TYPE'] = content_type
|
180
|
+
@env['rack.input'] = StringIO.new
|
181
|
+
@env['rack.input'] << %|{"foo":"bar"}|
|
182
|
+
@env['rack.input'].rewind
|
183
|
+
|
184
|
+
ret = @params.retrieve_params(@env)
|
185
|
+
expect(ret['foo']).to eq('bar')
|
186
|
+
end
|
187
|
+
|
188
|
+
it "parses #{content_type} that does not evaluate to a hash" do
|
189
|
+
@env['CONTENT_TYPE'] = content_type
|
190
|
+
@env['rack.input'] = StringIO.new
|
191
|
+
@env['rack.input'] << %|["foo","bar"]|
|
192
|
+
@env['rack.input'].rewind
|
193
|
+
|
194
|
+
ret = @params.retrieve_params(@env)
|
195
|
+
expect(ret['_json']).to eq(['foo', 'bar'])
|
196
|
+
end
|
197
|
+
|
198
|
+
it "handles empty input gracefully on #{content_type} JSON" do
|
199
|
+
@env['CONTENT_TYPE'] = content_type
|
200
|
+
@env['rack.input'] = StringIO.new
|
201
|
+
|
202
|
+
ret = @params.retrieve_params(@env)
|
203
|
+
expect(ret).to be_empty
|
204
|
+
end
|
205
|
+
|
206
|
+
it "raises a BadRequestError on invalid #{content_type} JSON" do
|
207
|
+
@env['CONTENT_TYPE'] = content_type
|
208
|
+
@env['rack.input'] = StringIO.new
|
209
|
+
@env['rack.input'] << %|{"foo":"bar" BORKEN}|
|
210
|
+
@env['rack.input'].rewind
|
211
|
+
|
212
|
+
expect{ @params.retrieve_params(@env) }.to raise_error(Goliath::Validation::BadRequestError)
|
213
|
+
end
|
177
214
|
end
|
178
215
|
|
179
216
|
it "doesn't parse unknown content types" do
|
@@ -183,7 +220,7 @@ Berry\r
|
|
183
220
|
@env['rack.input'].rewind
|
184
221
|
|
185
222
|
ret = @params.retrieve_params(@env)
|
186
|
-
ret.
|
223
|
+
expect(ret).to eq({})
|
187
224
|
end
|
188
225
|
end
|
189
226
|
end
|
@@ -13,31 +13,31 @@ describe Goliath::Rack::Render do
|
|
13
13
|
let(:render) { Goliath::Rack::Render.new(app) }
|
14
14
|
|
15
15
|
it 'accepts an app' do
|
16
|
-
|
16
|
+
expect { Goliath::Rack::Render.new('my app') }.not_to raise_error
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'returns the status, body and app headers' do
|
20
20
|
app_body = {'c' => 'd'}
|
21
21
|
|
22
|
-
app.
|
22
|
+
expect(app).to receive(:call).and_return([200, {'a' => 'b'}, app_body])
|
23
23
|
status, headers, body = render.call(env)
|
24
24
|
|
25
|
-
status.
|
26
|
-
headers['a'].
|
27
|
-
body.
|
25
|
+
expect(status).to eq(200)
|
26
|
+
expect(headers['a']).to eq('b')
|
27
|
+
expect(body).to eq(app_body)
|
28
28
|
end
|
29
29
|
|
30
30
|
describe 'Vary' do
|
31
31
|
it 'adds Accept to provided Vary header' do
|
32
|
-
app.
|
32
|
+
expect(app).to receive(:call).and_return([200, {'Vary' => 'Cookie'}, {}])
|
33
33
|
status, headers, body = render.call(env)
|
34
|
-
headers['Vary'].
|
34
|
+
expect(headers['Vary']).to eq('Cookie,Accept')
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'sets Accept if there is no Vary header' do
|
38
|
-
app.
|
38
|
+
expect(app).to receive(:call).and_return([200, {}, {}])
|
39
39
|
status, headers, body = render.call(env)
|
40
|
-
headers['Vary'].
|
40
|
+
expect(headers['Vary']).to eq('Accept')
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -51,7 +51,7 @@ describe Goliath::Rack::Render do
|
|
51
51
|
|
52
52
|
describe 'Content-Type' do
|
53
53
|
before(:each) do
|
54
|
-
app.
|
54
|
+
expect(app).to receive(:call).and_return([200, {}, {}])
|
55
55
|
end
|
56
56
|
|
57
57
|
describe 'from header' do
|
@@ -59,7 +59,7 @@ describe Goliath::Rack::Render do
|
|
59
59
|
it "handles content type for #{type}" do
|
60
60
|
env['HTTP_ACCEPT'] = type
|
61
61
|
status, headers, body = render.call(env)
|
62
|
-
headers['Content-Type'].
|
62
|
+
expect(headers['Content-Type']).to match(/^#{Regexp.escape(type)}/)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
@@ -69,7 +69,7 @@ describe Goliath::Rack::Render do
|
|
69
69
|
it "converts #{format} to #{content_type}" do
|
70
70
|
env['params']['format'] = format
|
71
71
|
status, headers, body = render.call(env)
|
72
|
-
headers['Content-Type'].
|
72
|
+
expect(headers['Content-Type']).to match(/^#{Regexp.escape(content_type)}/)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -78,14 +78,14 @@ describe Goliath::Rack::Render do
|
|
78
78
|
env['HTTP_ACCEPT'] = 'application/xml'
|
79
79
|
env['params']['format'] = 'json'
|
80
80
|
status, headers, body = render.call(env)
|
81
|
-
headers['Content-Type'].
|
81
|
+
expect(headers['Content-Type']).to match(%r{^application/json})
|
82
82
|
end
|
83
83
|
|
84
84
|
describe 'charset' do
|
85
85
|
it 'is set if not present' do
|
86
86
|
env['params']['format'] = 'json'
|
87
87
|
status, headers, body = render.call(env)
|
88
|
-
headers['Content-Type'].
|
88
|
+
expect(headers['Content-Type']).to match(/; charset=utf-8$/)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
@@ -14,32 +14,32 @@ describe Goliath::Rack::Validation::BooleanValue do
|
|
14
14
|
|
15
15
|
it 'uses the default if the key is not present' do
|
16
16
|
@bv.call(@env)
|
17
|
-
@env['params']['id'].
|
17
|
+
expect(@env['params']['id']).to eq(true)
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'uses the default if the key is nil' do
|
21
21
|
@env['params']['id'] = nil
|
22
22
|
@bv.call(@env)
|
23
|
-
@env['params']['id'].
|
23
|
+
expect(@env['params']['id']).to eq(true)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'uses the default if the key is blank' do
|
27
27
|
@env['params']['id'] = ""
|
28
28
|
@bv.call(@env)
|
29
|
-
@env['params']['id'].
|
29
|
+
expect(@env['params']['id']).to eq(true)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'a random value is false' do
|
33
33
|
@env['params']['id'] = 'blarg'
|
34
34
|
@bv.call(@env)
|
35
|
-
@env['params']['id'].
|
35
|
+
expect(@env['params']['id']).to eq(false)
|
36
36
|
end
|
37
37
|
|
38
38
|
%w(t true TRUE T 1).each do |type|
|
39
39
|
it "considers #{type} true" do
|
40
40
|
@env['params']['id'] = type
|
41
41
|
@bv.call(@env)
|
42
|
-
@env['params']['id'].
|
42
|
+
expect(@env['params']['id']).to eq(true)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -47,7 +47,7 @@ describe Goliath::Rack::Validation::BooleanValue do
|
|
47
47
|
it "considers #{type} false" do
|
48
48
|
@env['params']['id'] = type
|
49
49
|
@bv.call(@env)
|
50
|
-
@env['params']['id'].
|
50
|
+
expect(@env['params']['id']).to eq(false)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -4,15 +4,15 @@ require 'goliath/rack/validation/default_params'
|
|
4
4
|
describe Goliath::Rack::Validation::DefaultParams do
|
5
5
|
it 'accepts an app' do
|
6
6
|
opts = {:defaults => ['title'], :key => 'fields'}
|
7
|
-
|
7
|
+
expect { Goliath::Rack::Validation::DefaultParams.new('my app', opts) }.not_to raise_error
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'requires defaults to be set' do
|
11
|
-
|
11
|
+
expect { Goliath::Rack::Validation::DefaultParams.new('my app', {:key => 'test'}) }.to raise_error('Must provide defaults to DefaultParams')
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'requires key to be set' do
|
15
|
-
|
15
|
+
expect { Goliath::Rack::Validation::DefaultParams.new('my app', {:defaults => 'test'}) }.to raise_error('must provide key to DefaultParams')
|
16
16
|
end
|
17
17
|
|
18
18
|
describe 'with middleware' do
|
@@ -25,47 +25,47 @@ describe Goliath::Rack::Validation::DefaultParams do
|
|
25
25
|
it 'passes through provided key if set' do
|
26
26
|
@env['params']['fl'] = ['pubdate', 'content']
|
27
27
|
@rf.call(@env)
|
28
|
-
@env['params']['fl'].
|
28
|
+
expect(@env['params']['fl']).to eq(['pubdate', 'content'])
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'sets defaults if no key set' do
|
32
32
|
@rf.call(@env)
|
33
|
-
@env['params']['fl'].
|
33
|
+
expect(@env['params']['fl']).to eq(['title', 'link'])
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'sets defaults if no key set' do
|
37
37
|
@env['params']['fl'] = nil
|
38
38
|
@rf.call(@env)
|
39
|
-
@env['params']['fl'].
|
39
|
+
expect(@env['params']['fl']).to eq(['title', 'link'])
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'sets defaults if no key is empty' do
|
43
43
|
@env['params']['fl'] = []
|
44
44
|
@rf.call(@env)
|
45
|
-
@env['params']['fl'].
|
45
|
+
expect(@env['params']['fl']).to eq(['title', 'link'])
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'handles a single item' do
|
49
49
|
@env['params']['fl'] = 'title'
|
50
50
|
@rf.call(@env)
|
51
|
-
@env['params']['fl'].
|
51
|
+
expect(@env['params']['fl']).to eq('title')
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'handles a blank string' do
|
55
55
|
@env['params']['fl'] = ''
|
56
56
|
@rf.call(@env)
|
57
|
-
@env['params']['fl'].
|
57
|
+
expect(@env['params']['fl']).to eq(['title', 'link'])
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'returns the app status, headers and body' do
|
61
61
|
app_headers = {'Content-Type' => 'asdf'}
|
62
62
|
app_body = {'a' => 'b'}
|
63
|
-
@app.
|
63
|
+
expect(@app).to receive(:call).and_return([200, app_headers, app_body])
|
64
64
|
|
65
65
|
status, headers, body = @rf.call(@env)
|
66
|
-
status.
|
67
|
-
headers.
|
68
|
-
body.
|
66
|
+
expect(status).to eq(200)
|
67
|
+
expect(headers).to eq(app_headers)
|
68
|
+
expect(body).to eq(app_body)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -9,7 +9,7 @@ describe Goliath::Rack::Validation::NumericRange do
|
|
9
9
|
|
10
10
|
it 'accepts options on create' do
|
11
11
|
opts = { :min => 1, :key => 2 }
|
12
|
-
|
12
|
+
expect { Goliath::Rack::Validation::NumericRange.new('my app', opts) }.not_to raise_error
|
13
13
|
end
|
14
14
|
|
15
15
|
describe 'with middleware' do
|
@@ -20,41 +20,41 @@ describe Goliath::Rack::Validation::NumericRange do
|
|
20
20
|
it 'uses the default if value is less then min' do
|
21
21
|
@env['params']['id'] = -10
|
22
22
|
@nr.call(@env)
|
23
|
-
@env['params']['id'].
|
23
|
+
expect(@env['params']['id']).to eq(15)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'uses the default if value is greater then max' do
|
27
27
|
@env['params']['id'] = 25
|
28
28
|
@nr.call(@env)
|
29
|
-
@env['params']['id'].
|
29
|
+
expect(@env['params']['id']).to eq(15)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'uses the first value, if the value is an array' do
|
33
33
|
@env['params']['id'] = [10, 11, 12]
|
34
34
|
@nr.call(@env)
|
35
|
-
@env['params']['id'].
|
35
|
+
expect(@env['params']['id']).to eq(10)
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'uses the default if value is not present' do
|
39
39
|
@nr.call(@env)
|
40
|
-
@env['params']['id'].
|
40
|
+
expect(@env['params']['id']).to eq(15)
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'uses the default if value is nil' do
|
44
44
|
@env['params']['id'] = nil
|
45
45
|
@nr.call(@env)
|
46
|
-
@env['params']['id'].
|
46
|
+
expect(@env['params']['id']).to eq(15)
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'returns the app status, headers and body' do
|
50
50
|
app_headers = {'Content-Type' => 'app'}
|
51
51
|
app_body = {'b' => 'c'}
|
52
|
-
@app.
|
52
|
+
expect(@app).to receive(:call).and_return([200, app_headers, app_body])
|
53
53
|
|
54
54
|
status, headers, body = @nr.call(@env)
|
55
|
-
status.
|
56
|
-
headers.
|
57
|
-
body.
|
55
|
+
expect(status).to eq(200)
|
56
|
+
expect(headers).to eq(app_headers)
|
57
|
+
expect(body).to eq(app_body)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -62,42 +62,42 @@ describe Goliath::Rack::Validation::NumericRange do
|
|
62
62
|
nr = Goliath::Rack::Validation::NumericRange.new(@app, {:key => 'id', :min => 1.1, :as => Float})
|
63
63
|
@env['params']['id'] = 1.5
|
64
64
|
nr.call(@env)
|
65
|
-
@env['params']['id'].
|
65
|
+
expect(@env['params']['id']).to eq(1.5)
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'raises error if key is not set' do
|
69
|
-
|
69
|
+
expect { Goliath::Rack::Validation::NumericRange.new('app', {:min => 5}) }.to raise_error('NumericRange key required')
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'raises error if neither min nor max set' do
|
73
|
-
|
73
|
+
expect { Goliath::Rack::Validation::NumericRange.new('app', {:key => 5}) }.to raise_error('NumericRange requires :min or :max')
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'uses min if default not set' do
|
77
77
|
nr = Goliath::Rack::Validation::NumericRange.new(@app, {:key => 'id', :min => 5, :max => 10})
|
78
78
|
@env['params']['id'] = 15
|
79
79
|
nr.call(@env)
|
80
|
-
@env['params']['id'].
|
80
|
+
expect(@env['params']['id']).to eq(5)
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'uses max if default and min not set' do
|
84
84
|
nr = Goliath::Rack::Validation::NumericRange.new(@app, {:key => 'id', :max => 10})
|
85
85
|
@env['params']['id'] = 15
|
86
86
|
nr.call(@env)
|
87
|
-
@env['params']['id'].
|
87
|
+
expect(@env['params']['id']).to eq(10)
|
88
88
|
end
|
89
89
|
|
90
90
|
it "doesn't require min" do
|
91
91
|
nr = Goliath::Rack::Validation::NumericRange.new(@app, {:key => 'id', :max => 10})
|
92
92
|
@env['params']['id'] = 8
|
93
93
|
nr.call(@env)
|
94
|
-
@env['params']['id'].
|
94
|
+
expect(@env['params']['id']).to eq(8)
|
95
95
|
end
|
96
96
|
|
97
97
|
it "doesn't require max" do
|
98
98
|
nr = Goliath::Rack::Validation::NumericRange.new(@app, {:key => 'id', :min => 1})
|
99
99
|
@env['params']['id'] = 15
|
100
100
|
nr.call(@env)
|
101
|
-
@env['params']['id'].
|
101
|
+
expect(@env['params']['id']).to eq(15)
|
102
102
|
end
|
103
103
|
end
|