Move rackify headers to RequestFactory + other minor refactors

This commit is contained in:
domaindrivendev 2017-07-21 13:49:01 -07:00
parent 6ee53b08ae
commit e18a001e9b
3 changed files with 66 additions and 57 deletions

View File

@ -12,15 +12,15 @@ module Rswag
send( send(
request[:verb], request[:verb],
request[:path], request[:path],
request[:body], request[:payload],
rackify_headers(request[:headers]) # Rails test infrastructure requires Rack headers request[:headers]
) )
else else
send( send(
request[:verb], request[:verb],
request[:path], request[:path],
{ {
params: request[:body], params: request[:payload],
headers: request[:headers] headers: request[:headers]
} }
) )
@ -30,24 +30,6 @@ module Rswag
def assert_response_matches_metadata(metadata, &block) def assert_response_matches_metadata(metadata, &block)
ResponseValidator.new.validate!(metadata, response, &block) ResponseValidator.new.validate!(metadata, response, &block)
end end
private
def rackify_headers(headers)
name_value_pairs = headers.map do |name, value|
[
case name
when 'Accept' then 'HTTP_ACCEPT'
when 'Content-Type' then 'CONTENT_TYPE'
when 'Authorization' then 'HTTP_AUTHORIZATION'
else name
end,
value
]
end
Hash[ name_value_pairs ]
end
end end
end end
end end

View File

@ -1,4 +1,5 @@
require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/conversions'
require 'json' require 'json'
module Rswag module Rswag
@ -16,8 +17,8 @@ module Rswag
{}.tap do |request| {}.tap do |request|
add_verb(request, metadata) add_verb(request, metadata)
add_path(request, metadata, swagger_doc, parameters, example) add_path(request, metadata, swagger_doc, parameters, example)
add_headers(request, parameters, example) add_headers(request, metadata, swagger_doc, parameters, example)
add_content(request, metadata, swagger_doc, parameters, example) add_payload(request, parameters, example)
end end
end end
@ -91,34 +92,60 @@ module Rswag
end end
end end
def add_headers(request, parameters, example) def add_headers(request, metadata, swagger_doc, parameters, example)
name_value_pairs = parameters tuples = parameters
.select { |p| p[:in] == :header } .select { |p| p[:in] == :header }
.map { |p| [ p[:name], example.send(p[:name]).to_s ] } .map { |p| [ p[:name], example.send(p[:name]).to_s ] }
request[:headers] = Hash[ name_value_pairs ]
end
def add_content(request, metadata, swagger_doc, parameters, example)
# Accept header # Accept header
produces = metadata[:operation][:produces] || swagger_doc[:produces] produces = metadata[:operation][:produces] || swagger_doc[:produces]
if produces if produces
accept = example.respond_to?(:'Accept') ? example.send(:'Accept') : produces.first accept = example.respond_to?(:'Accept') ? example.send(:'Accept') : produces.first
request[:headers]['Accept'] = accept tuples << [ 'Accept', accept ]
end end
# Content-Type and body # Content-Type header
consumes = metadata[:operation][:consumes] || swagger_doc[:consumes] consumes = metadata[:operation][:consumes] || swagger_doc[:consumes]
return if consumes.nil? if consumes
content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first
request[:headers]['Content-Type'] = content_type tuples << [ 'Content-Type', content_type ]
end
if content_type.include?('json') # Rails test infrastructure requires rackified headers
rackified_tuples = tuples.map do |pair|
[
case pair[0]
when 'Accept' then 'HTTP_ACCEPT'
when 'Content-Type' then 'CONTENT_TYPE'
when 'Authorization' then 'HTTP_AUTHORIZATION'
else pair[0]
end,
pair[1]
]
end
request[:headers] = Hash[ rackified_tuples ]
end
def add_payload(request, parameters, example)
content_type = request[:headers]['CONTENT_TYPE']
return if content_type.nil?
if [ 'application/x-www-form-urlencoded', 'multipart/form-data' ].include?(content_type)
request[:payload] = build_form_payload(parameters, example)
else
request[:payload] = build_json_payload(parameters, example)
end
end
def build_json_payload(parameters, example)
body_param = parameters.select { |p| p[:in] == :body }.first body_param = parameters.select { |p| p[:in] == :body }.first
return if body_param.nil? body_param ? example.send(body_param[:name]).to_json : nil
request[:body] = example.send(body_param[:name]).to_json end
end
def build_form_payload(parameters, example)
nil
# TODO:
end end
end end
end end

View File

@ -133,8 +133,8 @@ module Rswag
end end
context "no 'Content-Type' provided" do context "no 'Content-Type' provided" do
it "sets 'Content-Type' header to first in list" do it "sets 'CONTENT_TYPE' header to first in list" do
expect(request[:headers]).to eq('Content-Type' => 'application/json') expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/json')
end end
end end
@ -143,19 +143,19 @@ module Rswag
allow(example).to receive(:'Content-Type').and_return('application/xml') allow(example).to receive(:'Content-Type').and_return('application/xml')
end end
it "sets 'Content-Type' header to example value" do it "sets 'CONTENT_TYPE' header to example value" do
expect(request[:headers]).to eq('Content-Type' => 'application/xml') expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml')
end end
end end
context 'JSON body' do context 'JSON payload' do
before do before do
metadata[:operation][:parameters] = [ { name: 'comment', in: :body, schema: { type: 'object' } } ] metadata[:operation][:parameters] = [ { name: 'comment', in: :body, schema: { type: 'object' } } ]
allow(example).to receive(:comment).and_return(text: 'Some comment') allow(example).to receive(:comment).and_return(text: 'Some comment')
end end
it 'sets body to example value as JSON string' do it "serializes first 'body' parameter to JSON string" do
expect(request[:body]).to eq("{\"text\":\"Some comment\"}") expect(request[:payload]).to eq("{\"text\":\"Some comment\"}")
end end
end end
end end
@ -166,8 +166,8 @@ module Rswag
end end
context "no 'Accept' value provided" do context "no 'Accept' value provided" do
it "sets 'Accept' header to first in list" do it "sets 'HTTP_ACCEPT' header to first in list" do
expect(request[:headers]).to eq('Accept' => 'application/json') expect(request[:headers]).to eq('HTTP_ACCEPT' => 'application/json')
end end
end end
@ -176,8 +176,8 @@ module Rswag
allow(example).to receive(:'Accept').and_return('application/xml') allow(example).to receive(:'Accept').and_return('application/xml')
end end
it "sets 'Accept' header to example value" do it "sets 'HTTP_ACCEPT' header to example value" do
expect(request[:headers]).to eq('Accept' => 'application/xml') expect(request[:headers]).to eq('HTTP_ACCEPT' => 'application/xml')
end end
end end
end end
@ -213,8 +213,8 @@ module Rswag
allow(example).to receive(:Authorization).and_return('Basic foobar') allow(example).to receive(:Authorization).and_return('Basic foobar')
end end
it "sets 'Authorization' header to example value" do it "sets 'HTTP_AUTHORIZATION' header to example value" do
expect(request[:headers]).to eq('Authorization' => 'Basic foobar') expect(request[:headers]).to eq('HTTP_AUTHORIZATION' => 'Basic foobar')
end end
end end
@ -225,8 +225,8 @@ module Rswag
allow(example).to receive(:Authorization).and_return('Bearer foobar') allow(example).to receive(:Authorization).and_return('Bearer foobar')
end end
it "sets 'Authorization' header to example value" do it "sets 'HTTP_AUTHORIZATION' header to example value" do
expect(request[:headers]).to eq('Authorization' => 'Bearer foobar') expect(request[:headers]).to eq('HTTP_AUTHORIZATION' => 'Bearer foobar')
end end
end end
@ -266,8 +266,8 @@ module Rswag
context "global consumes" do context "global consumes" do
before { swagger_doc[:consumes] = [ 'application/xml' ] } before { swagger_doc[:consumes] = [ 'application/xml' ] }
it "defaults 'Content-Type' to global value(s)" do it "defaults 'CONTENT_TYPE' to global value(s)" do
expect(request[:headers]).to eq('Content-Type' => 'application/xml') expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml')
end end
end end