Class: OpenapiRuby::Adapters::RSpec::ResponseProxy

Inherits:
Object
  • Object
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



199
200
201
202
203
204
205
# File 'lib/openapi_ruby/adapters/rspec.rb', line 199

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

#header(name, attributes = {}) ⇒ Object



126
127
128
# File 'lib/openapi_ruby/adapters/rspec.rb', line 126

def header(name, attributes = {})
  @response.header(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

Returns:

  • (Boolean)


207
208
209
# File 'lib/openapi_ruby/adapters/rspec.rb', line 207

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
# 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.

    # Resolve path parameters from let variables
    path = resolve_path()
    operation = find_operation()

    # Build params and headers from let variables
    params = resolve_let(:request_params) || {}
    headers = resolve_let(:request_headers) || {}
    body = resolve_let(:request_body)

    # Merge individual parameter let values
    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"
        headers[name] = val
      when "path"
        # Already handled in resolve_path
      end
    end

    # Execute the request
    method = operation&.verb || [:openapi_operation]&.verb || "get"
    send_args = {params: body || params}
    send_args[:headers] = headers if headers.any?

    if (body && headers["Content-Type"]&.include?("json")) || body.is_a?(Hash)
      send_args[:params] = body.is_a?(String) ? body : body.to_json
      send_args[:headers] = (headers || {}).merge("Content-Type" => "application/json")
    end

    send(method.to_sym, path, **send_args)

    # Validate response
    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

    # Execute additional assertions
    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