Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rspec-rails-swagger
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
rspec-rails-swagger
Commits
27841825
Commit
27841825
authored
Sep 25, 2016
by
andrew morton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set headers for parameters in: :header
parent
c4b5175f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
26 deletions
+40
-26
request_builder.rb
lib/rspec/swagger/request_builder.rb
+24
-11
helpers_spec.rb
spec/rspec/swagger/helpers_spec.rb
+0
-1
request_builder_spec.rb
spec/rspec/swagger/request_builder_spec.rb
+16
-14
No files found.
lib/rspec/swagger/request_builder.rb
View file @
27841825
...
...
@@ -26,10 +26,25 @@ module RSpec
metadata
[
:swagger_operation
][
:consumes
]
||
document
[
:consumes
]
end
def
parameters
def
parameters
location
=
nil
path_item
=
metadata
[
:swagger_path_item
]
||
{}
operation
=
metadata
[
:swagger_operation
]
||
{}
path_item
.
fetch
(
:parameters
,
{}).
merge
(
operation
.
fetch
(
:parameters
,
{}))
params
=
path_item
.
fetch
(
:parameters
,
{}).
merge
(
operation
.
fetch
(
:parameters
,
{}))
if
location
.
present?
params
.
select
{
|
k
,
_
|
k
.
starts_with?
"
#{
location
}
&"
}
else
params
end
end
def
parameter_values
location
# Don't bother looking at the full parameter bodies since all we need
# are location and name which are in the key.
values
=
parameters
(
location
)
.
keys
.
map
{
|
k
|
k
.
split
(
'&'
).
last
}
.
map
{
|
name
|
[
name
,
instance
.
send
(
name
)]
}
Hash
[
values
]
end
def
headers
...
...
@@ -39,7 +54,10 @@ module RSpec
headers
[
'HTTP_ACCEPT'
]
=
produces
.
join
(
';'
)
if
produces
.
present?
headers
[
'CONTENT_TYPE'
]
=
consumes
.
first
if
consumes
.
present?
# TODO needs to pull in parameters with in: :header set.
# TODO: do we need to do some capitalization to match the rack
# conventions?
parameter_values
(
:header
).
each
{
|
k
,
v
|
headers
[
k
]
=
v
}
headers
end
...
...
@@ -55,19 +73,14 @@ module RSpec
end
def
query
# Don't bother looking at the full parameter bodies since all we need
# are location and name which are the key.
query_params
=
parameters
.
keys
.
map
{
|
k
|
k
.
split
(
'&'
)}
.
select
{
|
location
,
name
|
location
==
'query'
}
.
map
{
|
location
,
name
|
[
name
,
instance
.
send
(
name
)]
}
'?'
+
Hash
[
query_params
].
to_query
unless
query_params
.
empty?
query_params
=
parameter_values
(
:query
).
to_query
"?
#{
query_params
}
"
unless
query_params
.
blank?
end
def
body
# And here all we need is the first half of the key to find the body
# parameter and its name to fetch a value.
if
key
=
parameters
.
keys
.
find
{
|
k
|
k
.
starts_with?
'body&'
}
if
key
=
parameters
(
:body
).
keys
.
first
instance
.
send
(
key
.
split
(
'&'
).
last
).
to_json
end
end
...
...
spec/rspec/swagger/helpers_spec.rb
View file @
27841825
...
...
@@ -47,7 +47,6 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
subject
{
klass
.
new
}
describe
"#operation"
do
it
"requires a verb"
do
expect
(
subject
).
to
receive
(
:describe
).
with
(
'get'
,
{
swagger_object: :operation
,
...
...
spec/rspec/swagger/request_builder_spec.rb
View file @
27841825
...
...
@@ -101,12 +101,14 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
end
describe
'#headers'
do
subject
{
described_class
.
new
(
double
(
'metadata'
),
double
(
'instance'
))
}
subject
{
described_class
.
new
(
double
(
'metadata'
),
instance
)
}
let
(
:instance
)
{
double
(
'instance'
)
}
let
(
:produces
)
{
}
let
(
:consumes
)
{
}
before
do
allow
(
subject
).
to
receive
(
:produces
)
{
produces
}
allow
(
subject
).
to
receive
(
:consumes
)
{
consumes
}
allow
(
subject
).
to
receive
(
:parameters
).
with
(
:header
)
{
{}
}
end
context
'when produces is present'
do
...
...
@@ -135,7 +137,15 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
end
end
xit
"includes parameters with in: :header"
do
context
'with header params'
do
it
'returns them in a string'
do
expect
(
subject
).
to
receive
(
:parameters
).
with
(
:header
)
{
{
'header&X-Magic'
=>
{
same: :here
}
}
}
expect
(
instance
).
to
receive
(
'X-Magic'
.
to_sym
)
{
:pickles
}
expect
(
subject
.
headers
).
to
include
(
'X-Magic'
=>
:pickles
)
end
end
end
...
...
@@ -172,10 +182,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context
'with no query params'
do
it
'returns nil'
do
expect
(
subject
).
to
receive
(
:parameters
)
{
{
'path&petId'
=>
{
this_is: :ignored
},
'body&site'
=>
{
same: :here
}
}
}
expect
(
subject
).
to
receive
(
:parameters
).
with
(
:query
)
{
{}
}
expect
(
subject
.
query
).
to
be_nil
end
...
...
@@ -183,8 +190,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context
'with query params'
do
it
'returns them in a string'
do
expect
(
subject
).
to
receive
(
:parameters
)
{
{
'path&petId'
=>
{
this_is: :ignored
},
expect
(
subject
).
to
receive
(
:parameters
).
with
(
:query
)
{
{
'query&site'
=>
{
same: :here
}
}
}
expect
(
instance
).
to
receive
(
:site
)
{
:pickles
}
...
...
@@ -200,10 +206,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context
'with no body param'
do
it
'returns nil'
do
expect
(
subject
).
to
receive
(
:parameters
)
{
{
'path&petId'
=>
{
this_is: :ignored
},
'query&site'
=>
{
same: :here
}
}
}
expect
(
subject
).
to
receive
(
:parameters
).
with
(
:body
)
{
{}
}
expect
(
subject
.
body
).
to
be_nil
end
...
...
@@ -211,8 +214,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context
'with a body param'
do
it
'returns a serialized JSON string'
do
expect
(
subject
).
to
receive
(
:parameters
)
{
{
'path&petId'
=>
{
this_is: :ignored
},
expect
(
subject
).
to
receive
(
:parameters
).
with
(
:body
)
{
{
'body&site'
=>
{
same: :here
}
}
}
expect
(
instance
).
to
receive
(
:site
)
{
{
name: :pickles
,
team: :cowboys
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment