/
request_response_spec.rb
207 lines (175 loc) · 8.38 KB
/
request_response_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# frozen_string_literal: true
require 'spec_helper'
module Grape
module DSL
module RequestResponseSpec
class Dummy
include Grape::DSL::RequestResponse
def self.set(key, value)
settings[key.to_sym] = value
end
def self.imbue(key, value)
settings.imbue(key, value)
end
end
end
describe RequestResponse do
subject { Class.new(RequestResponseSpec::Dummy) }
let(:c_type) { 'application/json' }
let(:format) { 'txt' }
describe '.default_format' do
it 'sets the default format' do
expect(subject).to receive(:namespace_inheritable).with(:default_format, :format)
subject.default_format :format
end
it 'returns the format without paramter' do
subject.default_format :format
expect(subject.default_format).to eq :format
end
end
describe '.format' do
it 'sets a new format' do
expect(subject).to receive(:namespace_inheritable).with(:format, format.to_sym)
expect(subject).to receive(:namespace_inheritable).with(:default_error_formatter, Grape::ErrorFormatter::Txt)
subject.format format
end
end
describe '.formatter' do
it 'sets the formatter for a content type' do
expect(subject).to receive(:namespace_stackable).with(:formatters, c_type.to_sym => :formatter)
subject.formatter c_type, :formatter
end
end
describe '.parser' do
it 'sets a parser for a content type' do
expect(subject).to receive(:namespace_stackable).with(:parsers, c_type.to_sym => :parser)
subject.parser c_type, :parser
end
end
describe '.default_error_formatter' do
it 'sets a new error formatter' do
expect(subject).to receive(:namespace_inheritable).with(:default_error_formatter, Grape::ErrorFormatter::Json)
subject.default_error_formatter :json
end
end
describe '.error_formatter' do
it 'sets a error_formatter' do
format = 'txt'
expect(subject).to receive(:namespace_stackable).with(:error_formatters, format.to_sym => :error_formatter)
subject.error_formatter format, :error_formatter
end
it 'understands syntactic sugar' do
expect(subject).to receive(:namespace_stackable).with(:error_formatters, format.to_sym => :error_formatter)
subject.error_formatter format, with: :error_formatter
end
end
describe '.content_type' do
it 'sets a content type for a format' do
expect(subject).to receive(:namespace_stackable).with(:content_types, format.to_sym => c_type)
subject.content_type format, c_type
end
end
describe '.content_types' do
it 'returns all content types' do
expect(subject.content_types).to eq(xml: 'application/xml',
serializable_hash: 'application/json',
json: 'application/json',
txt: 'text/plain',
binary: 'application/octet-stream')
end
end
describe '.default_error_status' do
it 'sets a default error status' do
expect(subject).to receive(:namespace_inheritable).with(:default_error_status, 500)
subject.default_error_status 500
end
end
describe '.rescue_from' do
describe ':all' do
it 'sets rescue all to true' do
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, nil)
subject.rescue_from :all
end
it 'sets given proc as rescue handler' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, rescue_handler_proc)
subject.rescue_from :all, rescue_handler_proc
end
it 'sets given block as rescue handler' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, rescue_handler_proc)
subject.rescue_from :all, &rescue_handler_proc
end
it 'sets a rescue handler declared through :with option' do
with_block = -> { 'hello' }
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:all_rescue_handler, an_instance_of(Proc))
subject.rescue_from :all, with: with_block
end
it 'abort if :with option value is not Symbol, String or Proc' do
expect { subject.rescue_from :all, with: 1234 }.to raise_error(ArgumentError, "with: #{integer_class_name}, expected Symbol, String or Proc")
end
it 'abort if both :with option and block are passed' do
expect do
subject.rescue_from :all, with: -> { 'hello' } do
error!('bye')
end
end.to raise_error(ArgumentError, 'both :with option and block cannot be passed')
end
end
describe ':grape_exceptions' do
it 'sets rescue all to true' do
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
subject.rescue_from :grape_exceptions
end
it 'sets rescue_grape_exceptions to true' do
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
subject.rescue_from :grape_exceptions
end
end
describe 'list of exceptions is passed' do
it 'sets hash of exceptions as rescue handlers' do
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => nil)
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
subject.rescue_from StandardError
end
it 'rescues only base handlers if rescue_subclasses: false option is passed' do
expect(subject).to receive(:namespace_reverse_stackable).with(:base_only_rescue_handlers, StandardError => nil)
expect(subject).to receive(:namespace_stackable).with(:rescue_options, rescue_subclasses: false)
subject.rescue_from StandardError, rescue_subclasses: false
end
it 'sets given proc as rescue handler for each key in hash' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => rescue_handler_proc)
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
subject.rescue_from StandardError, rescue_handler_proc
end
it 'sets given block as rescue handler for each key in hash' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => rescue_handler_proc)
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
subject.rescue_from StandardError, &rescue_handler_proc
end
it 'sets a rescue handler declared through :with option for each key in hash' do
with_block = -> { 'hello' }
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => an_instance_of(Proc))
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
subject.rescue_from StandardError, with: with_block
end
end
end
describe '.represent' do
it 'sets a presenter for a class' do
presenter = Class.new
expect(subject).to receive(:namespace_stackable).with(:representations, ThisClass: presenter)
subject.represent :ThisClass, with: presenter
end
end
end
end
end