Commit e95d89af by andrew morton

Incorporate the document's basePath in requests

parent 0b9e6ea2
......@@ -5,10 +5,6 @@ module RSpec
class Formatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self, :example_finished, :close
def initialize(output)
super
end
def documents
# We don't try to load the docs in `initalize` because when running
# `rspec -f RSpec::Swagger::Formatter` RSpec initalized this class
......@@ -17,13 +13,13 @@ module RSpec
end
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")
end
metadata = notification.example.metadata
document = document_for(metadata[:swagger_document])
path_item = path_item_for(document, metadata[:swagger_path_item])
operation = operation_for(path_item, metadata[:swagger_operation])
......
......@@ -73,6 +73,9 @@ module RSpec
attributes.symbolize_keys!
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
unless attributes[:schema].present?
raise ArgumentError, "Parameter is missing required 'schema' value."
......@@ -94,7 +97,7 @@ module RSpec
params = object_data[:parameters] ||= {}
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]}"
params[param_key] = param
end
......@@ -148,7 +151,7 @@ module RSpec
before do |example|
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)
# Run the request
......@@ -193,7 +196,12 @@ module RSpec
end
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]
end
......@@ -204,7 +212,7 @@ module RSpec
def resolve_headers metadata
headers = {}
# Match the names that Rails uses internally
if produces = resolve_prodces(metadata)
if produces = resolve_produces(metadata)
headers['HTTP_ACCEPT'] = produces.join(';')
end
if consumes = resolve_consumes(metadata)
......@@ -219,17 +227,22 @@ module RSpec
params = path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {}))
# 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|
p.slice(:name, :in).merge(value: group_instance.send(p[:name]))
end
end
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]) }
def resolve_path metadata, group_instance
document = resolve_document metadata
base_path = document[:basePath] || ''
# 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
......
......@@ -197,24 +197,39 @@ RSpec.describe RSpec::Swagger::Helpers::Resolver do
end
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
let(:template) { '/sites/{site_id}' }
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
describe "with values" do
let(:template) { '/sites/{site_id}/accounts/{accountId}' }
let(:site_id) { 1001 }
let(:accountId) { "pickles" }
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
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
......
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