Commit 7e77096a by andrew morton

Cleaning up stuff around the params

parent db6e3635
...@@ -21,8 +21,10 @@ module RSpec ...@@ -21,8 +21,10 @@ module RSpec
data = notification.example.metadata[:swagger_data] data = notification.example.metadata[:swagger_data]
document = document_for(nil) document = document_for(nil)
path = path_for(document, data[:path]) path_item = path_item_for(document, data[:path])
operation = operation_for(path, data[:operation]) # TODO output path_item's parameters
operation = operation_for(path_item, data[:operation])
# TODO output operation's parameters
response = response_for(operation, data[:status_code]) response = response_for(operation, data[:status_code])
response[:description] = data[:response_description] if data[:response_description] response[:description] = data[:response_description] if data[:response_description]
response[:examples] = prepare_example(data[:example]) if data[:example] response[:examples] = prepare_example(data[:example]) if data[:example]
...@@ -37,7 +39,7 @@ module RSpec ...@@ -37,7 +39,7 @@ module RSpec
end end
def write_json(name, document) def write_json(name, document)
pp document puts JSON.pretty_generate(document)
end end
def document_for doc_name = nil def document_for doc_name = nil
...@@ -48,7 +50,7 @@ module RSpec ...@@ -48,7 +50,7 @@ module RSpec
end end
end end
def path_for document, path_name def path_item_for document, path_name
document[:paths] ||= {} document[:paths] ||= {}
document[:paths][path_name] ||= {} document[:paths][path_name] ||= {}
end end
......
...@@ -11,7 +11,7 @@ module RSpec ...@@ -11,7 +11,7 @@ module RSpec
config.extend Operation, swagger_object: :operation config.extend Operation, swagger_object: :operation
config.extend Parameters, swagger_object: :operation config.extend Parameters, swagger_object: :operation
config.extend Response, swagger_object: :status_code config.extend Response, swagger_object: :status_code
config.include Common, :swagger_object config.include Resolver, :swagger_object
end end
...@@ -105,11 +105,6 @@ paths: (Paths) ...@@ -105,11 +105,6 @@ paths: (Paths)
describe("#{status_code}", meta) do describe("#{status_code}", meta) do
self.module_exec(&block) if block_given? self.module_exec(&block) if block_given?
# TODO: this needs a better mechanism
if metadata[:capture_example]
example = metadata[:swagger_data][:example] = {}
end
before do |example| before do |example|
method = example.metadata[:swagger_data][:operation] method = example.metadata[:swagger_data][:operation]
path = resolve_path(example.metadata[:swagger_data][:path], self) path = resolve_path(example.metadata[:swagger_data][:path], self)
...@@ -119,15 +114,18 @@ paths: (Paths) ...@@ -119,15 +114,18 @@ paths: (Paths)
[path, params, headers] [path, params, headers]
end end
# Run the request
self.send(method, *args) self.send(method, *args)
# TODO fix the naming collision if example.metadata[:capture_example]
# if example example.metadata[:swagger_data][:example] = {
# example.merge!(body: response.body, content_type: response.content_type.to_s) body: response.body,
# end content_type: response.content_type.to_s
}
end
end end
it("matches", { swagger_object: :response }) do it("returns the correct status code", { swagger_object: :response }) do
expect(response).to have_http_status(status_code) expect(response).to have_http_status(status_code)
end end
end end
...@@ -140,17 +138,20 @@ paths: (Paths) ...@@ -140,17 +138,20 @@ paths: (Paths)
end end
end end
module Common module Resolver
def resolve_params swagger_data, group_instance def resolve_params swagger_data, group_instance
params = swagger_data[:params].values
# TODO resolve $refs # TODO resolve $refs
# TODO there should only be one body param # TODO there should only be one body param
# TODO there should not be both body and formData params # TODO there should not be both body and formData params
swagger_data[:params].values.map do |p| params.map do |p|
p.slice(:name, :in).merge(value: group_instance.send(p[:name])) p.slice(:name, :in).merge(value: group_instance.send(p[:name]))
end end
end end
def resolve_path template, group_instance def resolve_path template, group_instance
# Should check that the parameter is actually defined before trying
# fetch a value?
template.gsub(/(\{.*?\})/){|match| group_instance.send(match[1...-1]) } template.gsub(/(\{.*?\})/){|match| group_instance.send(match[1...-1]) }
end end
end end
......
...@@ -8,8 +8,16 @@ RSpec.describe "Requestsing", type: :request do ...@@ -8,8 +8,16 @@ RSpec.describe "Requestsing", type: :request do
end end
operation "POST", "create" do operation "POST", "create" do
# params parameter "body", in: :body
let(:body) { { post: { title: 'asdf', body: "blah" } } }
# TODO: it should pull the body from the params
response(201, "successfully created", { post: { title: 'asdf', body: "blah" } }.to_json, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'}) do response(201, "successfully created", { post: { title: 'asdf', body: "blah" } }.to_json, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'}) do
it "uses the body we passed in" do
post = JSON.parse(response.body)
expect(post["title"]).to eq('asdf')
expect(post["body"]).to eq('blah')
end
capture_example capture_example
end end
end end
......
...@@ -83,7 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do ...@@ -83,7 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
end end
RSpec.describe RSpec::Swagger::Helpers::Common do RSpec.describe RSpec::Swagger::Helpers::Resolver do
# Tthis helper is an include rather than an extend we can get it pulled into # Tthis helper is an include rather than an extend we can get it pulled into
# the test just by matching the filter metadata. # the test just by matching the filter metadata.
describe("#resolve_params", swagger_object: :something) do describe("#resolve_params", swagger_object: :something) do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment