Commit c4b5175f by andrew morton

Add get, post, put, etc helpers for setting up operations

parent 75fa3329
......@@ -53,17 +53,33 @@ module RSpec
end
module PathItem
def operation verb, attributes = {}, &block
METHODS = %w(get put post delete options head patch).freeze
def operation method, attributes = {}, &block
attributes.symbolize_keys!
# TODO, check verbs against a whitelist
method = method.to_s.downcase
validate_method! method
verb = verb.to_s.downcase
meta = {
swagger_object: :operation,
swagger_operation: attributes.merge(method: verb.to_sym).reject{ |v| v.nil? }
swagger_operation: attributes.merge(method: method.to_sym).reject{ |v| v.nil? }
}
describe(verb.to_s, meta, &block)
describe(method.to_s, meta, &block)
end
METHODS.each do |method|
define_method(method) do |attributes = {}, &block|
operation(method, attributes, &block)
end
end
private
def validate_method! method
unless METHODS.include? method.to_s
raise ArgumentError, "Operation has an invalid 'method' value. Try: #{METHODS}."
end
end
end
......@@ -130,7 +146,7 @@ module RSpec
raise ArgumentError, "Parameter is missing required 'in' value."
end
locations = %q(query header path formData body)
locations = %w(query header path formData body)
unless locations.include? location.to_s
raise ArgumentError, "Parameter has an invalid 'in' value. Try: #{locations}."
end
......@@ -141,7 +157,7 @@ module RSpec
raise ArgumentError, "Parameter is missing required 'type' value."
end
types = %q(string number integer boolean array file)
types = %w(string number integer boolean array file)
unless types.include? type.to_s
raise ArgumentError, "Parameter has an invalid 'type' value. Try: #{types}."
end
......@@ -170,7 +186,14 @@ module RSpec
describe(status_code, meta) do
self.module_exec(&block) if block_given?
# TODO: describe the wacky ness to get the metadata and access to let() defined values...
# To make a request we need:
# - the details we've collected in the metadata
# - parameter values defined using let()
# RSpec tries to limit access to metadata inside of it() / before()
# / after() blocks but that scope is the only place you can access
# the let() values. The solution the swagger_rails dev came up with
# is to use the example.metadata passed into the block with the
# block's scope which has access to the let() values.
before do |example|
builder = RequestBuilder.new(example.metadata, self)
method = builder.method
......@@ -185,12 +208,14 @@ module RSpec
self.send(method, path, body, headers)
end
if example.metadata[:capture_example]
if example.metadata[:capture_examples]
examples = example.metadata[:swagger_response][:examples] ||= {}
examples[response.content_type.to_s] = response.body
end
end
# TODO: see if we can get the caller to show up in the error
# backtrace for this test.
it("returns the correct status code") do
expect(response).to have_http_status(status_code)
end
......@@ -214,7 +239,7 @@ module RSpec
module Response
def capture_example
metadata[:capture_example] = true
metadata[:capture_examples] = true
end
end
end
......
......@@ -15,7 +15,7 @@ RSpec.describe "Requestsing", type: :request do
response(200, {description: "successful"}, {})
end
operation "POST",summary: "create" do
post summary: "create" do
produces 'application/json'
consumes 'application/json'
......@@ -38,7 +38,7 @@ RSpec.describe "Requestsing", type: :request do
parameter "post_id", {in: :path, type: :integer}
let(:post_id) { 1 }
operation "GET", summary: "fetch item" do
get summary: "fetch item" do
produces 'application/json'
before { Post.new.save }
......
......@@ -48,7 +48,7 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
describe "#operation" do
it "requires only an HTTP verb" do
it "requires a verb" do
expect(subject).to receive(:describe).with('get', {
swagger_object: :operation,
swagger_operation: {method: :get}
......@@ -57,7 +57,15 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
subject.operation('GET')
end
it "accepts other options" do
it 'validates the verb' do
expect{ subject.operation('foo') }.to raise_exception(ArgumentError)
expect{ subject.operation(:foo) }.to raise_exception(ArgumentError)
expect{ subject.operation(:head) }.not_to raise_exception
expect{ subject.operation('patch') }.not_to raise_exception
end
it 'accepts additional options' do
expect(subject).to receive(:describe).with('head', {
swagger_object: :operation,
swagger_operation: {
......@@ -74,7 +82,19 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
operationId: 'updatePetWithForm'
)
end
end
described_class::METHODS.each do |method|
describe "##{method}" do
it 'calls #operation' do
expect(subject).to receive(:describe).with(method.to_s, {
swagger_object: :operation,
swagger_operation: { method: method.to_sym }
})
subject.send(method)
end
end
end
end
......
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