Commit e95d89af by andrew morton

Incorporate the document's basePath in requests

parent 0b9e6ea2
...@@ -5,10 +5,6 @@ module RSpec ...@@ -5,10 +5,6 @@ module RSpec
class Formatter < RSpec::Core::Formatters::BaseTextFormatter class Formatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self, :example_finished, :close RSpec::Core::Formatters.register self, :example_finished, :close
def initialize(output)
super
end
def documents def documents
# We don't try to load the docs in `initalize` because when running # We don't try to load the docs in `initalize` because when running
# `rspec -f RSpec::Swagger::Formatter` RSpec initalized this class # `rspec -f RSpec::Swagger::Formatter` RSpec initalized this class
...@@ -17,13 +13,13 @@ module RSpec ...@@ -17,13 +13,13 @@ module RSpec
end end
def example_finished(notification) def example_finished(notification)
return unless notification.example.metadata[:swagger_object] == :response metadata = notification.example.metadata
return unless metadata[:swagger_object] == :response
notification.example.metadata.each do |k, v| metadata.each do |k, v|
puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger") puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger")
end end
metadata = notification.example.metadata
document = document_for(metadata[:swagger_document]) document = document_for(metadata[:swagger_document])
path_item = path_item_for(document, metadata[:swagger_path_item]) path_item = path_item_for(document, metadata[:swagger_path_item])
operation = operation_for(path_item, metadata[:swagger_operation]) operation = operation_for(path_item, metadata[:swagger_operation])
......
...@@ -73,6 +73,9 @@ module RSpec ...@@ -73,6 +73,9 @@ module RSpec
attributes.symbolize_keys! attributes.symbolize_keys!
validate_location! attributes[:in] validate_location! attributes[:in]
# TODO validate there is only be one body param
# TODO validate there are not both body and formData params
if attributes[:in] == :body if attributes[:in] == :body
unless attributes[:schema].present? unless attributes[:schema].present?
raise ArgumentError, "Parameter is missing required 'schema' value." raise ArgumentError, "Parameter is missing required 'schema' value."
...@@ -94,7 +97,7 @@ module RSpec ...@@ -94,7 +97,7 @@ module RSpec
params = object_data[:parameters] ||= {} params = object_data[:parameters] ||= {}
param = { name: name.to_s }.merge(attributes) param = { name: name.to_s }.merge(attributes)
# Params should be unique based on the 'name' and 'in' values. # This key ensures uniqueness based on the 'name' and 'in' values.
param_key = "#{param[:in]}&#{param[:name]}" param_key = "#{param[:in]}&#{param[:name]}"
params[param_key] = param params[param_key] = param
end end
...@@ -148,7 +151,7 @@ module RSpec ...@@ -148,7 +151,7 @@ module RSpec
before do |example| before do |example|
method = example.metadata[:swagger_operation][:method] method = example.metadata[:swagger_operation][:method]
path = resolve_path(example.metadata[:swagger_path_item][:path], self) path = resolve_path(example.metadata, self)
headers = resolve_headers(example.metadata) headers = resolve_headers(example.metadata)
# Run the request # Run the request
...@@ -193,7 +196,12 @@ module RSpec ...@@ -193,7 +196,12 @@ module RSpec
end end
module Resolver module Resolver
def resolve_prodces metadata def resolve_document metadata
name = metadata[:swagger_document]
Document.new(RSpec.configuration.swagger_docs[name])
end
def resolve_produces metadata
metadata[:swagger_operation][:produces] metadata[:swagger_operation][:produces]
end end
...@@ -204,7 +212,7 @@ module RSpec ...@@ -204,7 +212,7 @@ module RSpec
def resolve_headers metadata def resolve_headers metadata
headers = {} headers = {}
# Match the names that Rails uses internally # Match the names that Rails uses internally
if produces = resolve_prodces(metadata) if produces = resolve_produces(metadata)
headers['HTTP_ACCEPT'] = produces.join(';') headers['HTTP_ACCEPT'] = produces.join(';')
end end
if consumes = resolve_consumes(metadata) if consumes = resolve_consumes(metadata)
...@@ -219,17 +227,22 @@ module RSpec ...@@ -219,17 +227,22 @@ module RSpec
params = path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {})) params = path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {}))
# TODO resolve $refs # TODO resolve $refs
# TODO there should only be one body param
# TODO there should not be both body and formData params
params.values.map do |p| params.values.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 metadata, group_instance
# Should check that the parameter is actually defined before trying document = resolve_document metadata
# fetch a value? base_path = document[:basePath] || ''
template.gsub(/(\{.*?\})/){|match| group_instance.send(match[1...-1]) } # Find params in the path and replace them with values defined in
# in the example group.
path = metadata[:swagger_path_item][:path].gsub(/(\{.*?\})/) do |match|
# QUESTION: Should check that the parameter is actually defined in
# `metadata[:swagger_*][:parameters]` before fetch a value?
group_instance.send(match[1...-1])
end
base_path + path
end end
end end
end end
......
...@@ -197,24 +197,39 @@ RSpec.describe RSpec::Swagger::Helpers::Resolver do ...@@ -197,24 +197,39 @@ RSpec.describe RSpec::Swagger::Helpers::Resolver do
end end
describe "#resolve_path", :swagger_object do describe "#resolve_path", :swagger_object do
let(:metadata) do
{
swagger_document: 'example.json',
swagger_path_item: {path: template},
}
end
let(:document) { { } }
before { expect(self).to receive(:resolve_document) { document } }
describe "with a missing value" do describe "with a missing value" do
let(:template) { '/sites/{site_id}' }
it "raises an error" do it "raises an error" do
expect{ resolve_path('/sites/{site_id}', self) }.to raise_exception(NoMethodError) expect{ resolve_path(metadata, self) }.to raise_exception(NoMethodError)
end end
end end
describe "with values" do describe "with values" do
let(:template) { '/sites/{site_id}/accounts/{accountId}' }
let(:site_id) { 1001 } let(:site_id) { 1001 }
let(:accountId) { "pickles" } let(:accountId) { "pickles" }
it "substitutes them into the path" do it "substitutes them into the path" do
expect(resolve_path('/sites/{site_id}/accounts/{accountId}', self)).to eq('/sites/1001/accounts/pickles') expect(resolve_path(metadata, self)).to eq('/sites/1001/accounts/pickles')
end end
end end
describe "with a base path" do describe "with a base path" do
xit "prefixes the path" do let(:document) { { basePath: '/base' } }
let(:template) { '/sites/' }
it "prefixes the path" do
expect(resolve_path(metadata, self)).to eq('/base/sites/')
end end
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