Commit f37dda8c by andrew morton

Start adding some validations to the helpers

parent dae6fea2
......@@ -63,22 +63,40 @@ paths: (Paths)
end
module Parameters
def parameter name, args = {}
# TODO these should be unique by (name, in) so we should use a hash
# to store them instead of an array.
param_key = "#{metadata[:swagger_object]}_params".to_s
params = metadata[:swagger_data][param_key] ||= []
params << { name: name }.merge(args)
def parameter name, attributes = {}
attributes.symbolize_keys!
raise ArgumentError, "Missing 'in' parameter" unless attributes[:in]
locations = [:query, :header, :path, :formData, :body]
unless locations.include? attributes[:in]
raise ArgumentError, "Invalid 'in' parameter, must be one of #{locations}"
end
# if name.respond_to?(:has_key?)
# param = { '$ref' => name.delete(:ref) || name.delete('ref') }
# end
params_key = "#{metadata[:swagger_object]}_params".to_sym
params = metadata[:swagger_data][params_key] ||= {}
param = { name: name.to_s }.merge(attributes)
# Params should be unique based on the 'name' and 'in' values.
param_key = "#{param[:in]}&#{param[:name]}"
params[param_key] = param
end
end
module Operation
def response code, desc, params = {}, headers = {}, &block
def response status_code, desc, params = {}, headers = {}, &block
unless status_code == :default || (100..599).cover?(status_code)
raise ArgumentError, "status_code must be between 100 and 599 or :default"
end
meta = {
swagger_object: :status_code,
swagger_data: metadata[:swagger_data].merge(status_code: code, response_description: desc)
swagger_data: metadata[:swagger_data].merge(status_code: status_code, response_description: desc)
}
describe("#{code}", meta) do
describe("#{status_code}", meta) do
self.module_exec(&block) if block_given?
method = metadata[:swagger_data][:operation]
......@@ -105,7 +123,7 @@ paths: (Paths)
example.merge!( body: response.body, content_type: response.content_type.to_s)
end
expect(response).to have_http_status(code)
expect(response).to have_http_status(status_code)
end
end
end
......
......@@ -9,7 +9,13 @@ Gem::Specification.new do |s|
s.description = "Inspired by swagger_rails"
s.author = "andrew morton"
s.email = 'drewish@katherinehouse.com'
s.files = ['lib/rspec/swagger.rb', 'lib/rspec/swagger/version.rb']
s.files = [
'lib/rspec/swagger.rb',
'lib/rspec/swagger/configuration.rb',
'lib/rspec/swagger/formatter.rb',
'lib/rspec/swagger/helpers.rb',
'lib/rspec/swagger/version.rb',
]
s.homepage = 'https://github.com/drewish/rspec-swagger'
s.required_ruby_version = '~> 2.0'
......
......@@ -19,7 +19,7 @@ RSpec.describe "Requestsing", type: :request do
parameter "path-param", {in: :path}
operation "GET", "fetch item" do
before { Post.new.save }
parameter "op-param"
parameter "op-param", {in: :query}
response(200, "success", {}, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'}) do
capture_example
end
......
require "spec_helper"
RSpec.describe RSpec::Swagger::Helpers::Parameters do
let(:klass) do
Class.new do
include RSpec::Swagger::Helpers::Parameters
attr_accessor :metadata
end
end
subject { klass.new }
describe "#parameter" do
before { subject.metadata = {swagger_object: :path_item, swagger_data: {}} }
it "requires 'in' parameter" do
expect{ subject.parameter "name", foo: :bar }.to raise_exception(ArgumentError)
end
it "validates 'in' parameter" do
expect{ subject.parameter "name", in: :form_data }.to raise_exception(ArgumentError)
expect{ subject.parameter "name", in: "formData" }.to raise_exception(ArgumentError)
expect{ subject.parameter "name", in: :formData }.not_to raise_exception
end
context "on a path_item" do
before { subject.metadata = {swagger_object: :path_item, swagger_data: {}} }
it "keeps parameters unique by name and in" do
subject.parameter('foo', in: :path)
subject.parameter('foo', in: :path)
subject.parameter('bar', in: :query)
subject.parameter('baz', in: :query)
expect(subject.metadata[:swagger_data][:path_item_params].length).to eq 3
end
end
context "on an operation" do
before { subject.metadata = {swagger_object: :operation, swagger_data: {}} }
it "keeps parameters unique by name and in" do
subject.parameter('foo', in: :path)
subject.parameter('foo', in: :path)
subject.parameter('bar', in: :query)
subject.parameter('baz', in: :query)
expect(subject.metadata[:swagger_data][:operation_params].length).to eq 3
end
end
end
end
RSpec.describe RSpec::Swagger::Helpers::Operation do
let(:klass) do
Class.new do
include RSpec::Swagger::Helpers::Operation
attr_accessor :metadata
end
end
subject { klass.new }
describe "#response" do
before { subject.metadata = {swagger_object: :operation, swagger_data: {}} }
it "requires code be an integer 100...600 or :default" do
expect{ subject.response 1, "description" }.to raise_exception(ArgumentError)
expect{ subject.response 99, "description" }.to raise_exception(ArgumentError)
expect{ subject.response 600, "description" }.to raise_exception(ArgumentError)
expect{ subject.response '404', "description" }.to raise_exception(ArgumentError)
expect{ subject.response 'default', "description" }.to raise_exception(ArgumentError)
# expect{ subject.response 100, "description" }.not_to raise_exception
# expect{ subject.response 599, "description" }.not_to raise_exception
# expect{ subject.response :default, "description" }.not_to raise_exception
end
end
end
require 'rspec/swagger'
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
......
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