-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrequest_spec.rb
186 lines (153 loc) · 5.38 KB
/
request_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true
describe Grape::Request do
let(:default_method) { Rack::GET }
let(:default_params) { {} }
let(:default_options) do
{
method: method,
params: params
}
end
let(:default_env) do
Rack::MockRequest.env_for('/', options)
end
let(:method) { default_method }
let(:params) { default_params }
let(:options) { default_options }
let(:env) { default_env }
let(:request) do
described_class.new(env)
end
describe '#params' do
let(:params) do
{
a: '123',
b: 'xyz'
}
end
it 'by default returns stringified parameter keys' do
expect(request.params).to eq(ActiveSupport::HashWithIndifferentAccess.new('a' => '123', 'b' => 'xyz'))
end
context 'when build_params_with: Grape::Extensions::Hash::ParamBuilder is specified' do
let(:request) do
described_class.new(env, build_params_with: :hash)
end
it 'returns symbolized params' do
expect(request.params).to eq(a: '123', b: 'xyz')
end
end
describe 'with grape.routing_args' do
let(:options) do
default_options.merge('grape.routing_args' => routing_args)
end
let(:routing_args) do
{
version: '123',
route_info: '456',
c: 'ccc'
}
end
it 'cuts version and route_info' do
expect(request.params).to eq(ActiveSupport::HashWithIndifferentAccess.new(a: '123', b: 'xyz', c: 'ccc'))
end
end
context 'when rack_params raises an EOF error' do
before do
allow(request).to receive(:rack_params).and_raise(EOFError)
end
let(:message) { Grape::Exceptions::EmptyMessageBody.new(nil).to_s }
it 'raises an Grape::Exceptions::EmptyMessageBody' do
expect { request.params }.to raise_error(Grape::Exceptions::EmptyMessageBody, message)
end
end
context 'when rack_params raises a Rack::Multipart::MultipartPartLimitError' do
before do
allow(request).to receive(:rack_params).and_raise(Rack::Multipart::MultipartPartLimitError)
end
let(:message) { Grape::Exceptions::TooManyMultipartFiles.new(Rack::Utils.multipart_part_limit).to_s }
it 'raises an Rack::Multipart::MultipartPartLimitError' do
expect { request.params }.to raise_error(Grape::Exceptions::TooManyMultipartFiles, message)
end
end
context 'when rack_params raises a Rack::Multipart::MultipartTotalPartLimitError' do
before do
allow(request).to receive(:rack_params).and_raise(Rack::Multipart::MultipartTotalPartLimitError)
end
let(:message) { Grape::Exceptions::TooManyMultipartFiles.new(Rack::Utils.multipart_part_limit).to_s }
it 'raises an Rack::Multipart::MultipartPartLimitError' do
expect { request.params }.to raise_error(Grape::Exceptions::TooManyMultipartFiles, message)
end
end
context 'when rack_params raises a Rack::QueryParser::ParamsTooDeepError' do
before do
allow(request).to receive(:rack_params).and_raise(Rack::QueryParser::ParamsTooDeepError)
end
let(:message) { Grape::Exceptions::TooDeepParameters.new(Rack::Utils.param_depth_limit).to_s }
it 'raises a Grape::Exceptions::TooDeepParameters' do
expect { request.params }.to raise_error(Grape::Exceptions::TooDeepParameters, message)
end
end
context 'when rack_params raises a Rack::Utils::ParameterTypeError' do
before do
allow(request).to receive(:rack_params).and_raise(Rack::Utils::ParameterTypeError)
end
let(:message) { Grape::Exceptions::ConflictingTypes.new.to_s }
it 'raises a Grape::Exceptions::ConflictingTypes' do
expect { request.params }.to raise_error(Grape::Exceptions::ConflictingTypes, message)
end
end
context 'when rack_params raises a Rack::Utils::InvalidParameterError' do
before do
allow(request).to receive(:rack_params).and_raise(Rack::Utils::InvalidParameterError)
end
let(:message) { Grape::Exceptions::InvalidParameters.new.to_s }
it 'raises an Rack::Multipart::MultipartPartLimitError' do
expect { request.params }.to raise_error(Grape::Exceptions::InvalidParameters, message)
end
end
end
describe '#headers' do
let(:options) do
default_options.merge(request_headers)
end
describe 'with http headers in env' do
let(:request_headers) do
{
'HTTP_X_GRAPE_IS_COOL' => 'yeah'
}
end
let(:x_grape_is_cool_header) do
'x-grape-is-cool'
end
it 'cuts HTTP_ prefix and capitalizes header name words' do
expect(request.headers).to eq(x_grape_is_cool_header => 'yeah')
end
end
describe 'with non-HTTP_* stuff in env' do
let(:request_headers) do
{
'HTP_X_GRAPE_ENTITY_TOO' => 'but now we are testing Grape'
}
end
it 'does not include them' do
expect(request.headers).to eq({})
end
end
describe 'with symbolic header names' do
let(:request_headers) do
{
HTTP_GRAPE_LIKES_SYMBOLIC: 'it is true'
}
end
let(:env) do
default_env.merge(request_headers)
end
let(:grape_likes_symbolic_header) do
'grape-likes-symbolic'
end
it 'converts them to string' do
expect(request.headers).to eq(grape_likes_symbolic_header => 'it is true')
end
end
end
end