Class: OpenapiRuby::Adapters::RSpec::ResponseProxy
- Inherits:
-
Object
- Object
- OpenapiRuby::Adapters::RSpec::ResponseProxy
show all
- Defined in:
- lib/openapi_ruby/adapters/rspec.rb
Instance Method Summary
collapse
Constructor Details
#initialize(example_group, response_context) ⇒ ResponseProxy
Returns a new instance of ResponseProxy.
117
118
119
120
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 117
def initialize(example_group, response_context)
@example_group = example_group
@response = response_context
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
Forward let/before/after/subject to RSpec
206
207
208
209
210
211
212
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 206
def method_missing(name, ...)
if @example_group.respond_to?(name)
@example_group.send(name, ...)
else
super
end
end
|
Instance Method Details
#example(content_type) ⇒ Object
130
131
132
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 130
def example(content_type, **)
@response.example(content_type, **)
end
|
126
127
128
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 126
def (name, attributes = {})
@response.(name, attributes)
end
|
#produces(*content_types) ⇒ Object
134
135
136
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 134
def produces(*content_types)
@response.produces(*content_types)
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
214
215
216
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 214
def respond_to_missing?(name, include_private = false)
@example_group.respond_to?(name, include_private) || super
end
|
#run_test!(description = nil, &block) ⇒ Object
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
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 138
def run_test!(description = nil, &block)
response_ctx = @response
@example_group.it(description || "returns #{response_ctx.status_code}") do |example|
example_metadata = example.metadata
path = resolve_path(example_metadata)
operation = find_operation(example_metadata)
params = resolve_let(:request_params) || {}
= resolve_let(:request_headers) || {}
body = resolve_let(:request_body)
operation&.parameters&.each do |param|
name = param["name"]
val = resolve_let(name.to_sym)
next unless val
case param["in"]
when "query"
params[name] = val
when "header"
[name] = val
when "path"
end
end
method = operation&.verb || example_metadata[:openapi_operation]&.verb || "get"
send_args = {params: body || params}
send_args[:headers] = if .any?
if body
content_type = ["Content-Type"] || operation&.instance_variable_get(:@consumes_list)&.first
if content_type&.include?("form-data") || content_type&.include?("x-www-form-urlencoded")
send_args[:params] = body
send_args[:headers] = ( || {}).merge("Content-Type" => content_type)
else
send_args[:params] = body.is_a?(String) ? body : body.to_json
send_args[:headers] = ( || {}).merge("Content-Type" => content_type || "application/json")
end
end
send(method.to_sym, path, **send_args)
if OpenapiRuby.configuration.validate_responses_in_tests && response_ctx.schema_definition
validator = Testing::ResponseValidator.new
errors = validator.validate(
response_body: parsed_response_body,
status_code: response.status,
response_context: response_ctx
)
expect(errors).to be_empty, "Response validation failed:\n#{errors.join("\n")}"
else
expect(response.status).to eq(response_ctx.status_code.to_i)
end
instance_eval(&block) if block
end
end
|
#schema(definition) ⇒ Object
122
123
124
|
# File 'lib/openapi_ruby/adapters/rspec.rb', line 122
def schema(definition)
@response.schema(definition)
end
|