mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
First iteration of rspec driven swagger
This commit is contained in:
@@ -5,55 +5,56 @@ module SwaggerRails
|
||||
module Adapter
|
||||
|
||||
def path(path_template, &block)
|
||||
describe(path_template, path_template: path_template, &block)
|
||||
metadata = {
|
||||
path_template: path_template
|
||||
}
|
||||
describe(path_template, metadata, &block)
|
||||
end
|
||||
|
||||
def operation(method, summary=nil, &block)
|
||||
operation_metadata = {
|
||||
method: method,
|
||||
def operation(http_verb, summary=nil, &block)
|
||||
metadata = {
|
||||
http_verb: http_verb,
|
||||
summary: summary,
|
||||
parameters: []
|
||||
}
|
||||
describe(method, operation: operation_metadata, &block)
|
||||
describe(http_verb, metadata, &block)
|
||||
end
|
||||
|
||||
[ :get, :post, :patch, :put, :delete, :head ].each do |http_verb|
|
||||
define_method(http_verb) do |summary=nil, &block|
|
||||
operation(http_verb, summary, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def consumes(*mime_types)
|
||||
metadata[:operation][:consumes] = mime_types
|
||||
metadata[:consumes] = mime_types
|
||||
end
|
||||
|
||||
def produces(*mime_types)
|
||||
metadata[:operation][:produces] = mime_types
|
||||
metadata[:produces] = mime_types
|
||||
end
|
||||
|
||||
def header(name, attributes={})
|
||||
parameter(name, 'header', attributes)
|
||||
def parameter(name, attributes={})
|
||||
metadata[:parameters] << { name: name.to_s }.merge(attributes)
|
||||
end
|
||||
|
||||
def body(name, attributes={})
|
||||
parameter(name, 'body', attributes)
|
||||
end
|
||||
|
||||
def parameter(name, location, attributes={})
|
||||
parameter_metadata = { name: name.to_s, in: location }.merge(attributes)
|
||||
metadata[:operation][:parameters] << parameter_metadata
|
||||
end
|
||||
|
||||
def response(status, description, &block)
|
||||
response_metadata = { status: status, description: description }
|
||||
context(description, response: response_metadata, &block)
|
||||
def response(code, description, &block)
|
||||
metadata = {
|
||||
response_code: code,
|
||||
response: {
|
||||
description: description
|
||||
}
|
||||
}
|
||||
context(description, metadata, &block)
|
||||
end
|
||||
|
||||
def run_test!
|
||||
before do |example|
|
||||
SwaggerRails::TestVisitor.instance.act!(
|
||||
self, example.metadata[:path_template], example.metadata[:operation]
|
||||
)
|
||||
SwaggerRails::TestVisitor.instance.submit_request!(self, example.metadata)
|
||||
end
|
||||
|
||||
it "returns a #{metadata[:response][:status]} status" do |example|
|
||||
SwaggerRails::TestVisitor.instance.assert!(
|
||||
self, example.metadata[:response]
|
||||
)
|
||||
it "returns a #{metadata[:response_code]} status" do |example|
|
||||
SwaggerRails::TestVisitor.instance.assert_response!(self, example.metadata)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user