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
c4b5175f
Commit
c4b5175f
authored
Sep 25, 2016
by
andrew morton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add get, post, put, etc helpers for setting up operations
parent
75fa3329
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
14 deletions
+59
-14
helpers.rb
lib/rspec/swagger/helpers.rb
+35
-10
request_spec.rb
spec/requests/request_spec.rb
+2
-2
helpers_spec.rb
spec/rspec/swagger/helpers_spec.rb
+22
-2
No files found.
lib/rspec/swagger/helpers.rb
View file @
c4b5175f
...
@@ -53,17 +53,33 @@ module RSpec
...
@@ -53,17 +53,33 @@ module RSpec
end
end
module
PathItem
module
PathItem
def
operation
verb
,
attributes
=
{},
&
block
METHODS
=
%w(get put post delete options head patch)
.
freeze
def
operation
method
,
attributes
=
{},
&
block
attributes
.
symbolize_keys!
attributes
.
symbolize_keys!
# TODO, check verbs against a whitelist
method
=
method
.
to_s
.
downcase
validate_method!
method
verb
=
verb
.
to_s
.
downcase
meta
=
{
meta
=
{
swagger_object: :operation
,
swagger_object: :operation
,
swagger_operation:
attributes
.
merge
(
method:
verb
.
to_sym
).
reject
{
|
v
|
v
.
nil?
}
swagger_operation:
attributes
.
merge
(
method:
method
.
to_sym
).
reject
{
|
v
|
v
.
nil?
}
}
}
describe
(
verb
.
to_s
,
meta
,
&
block
)
describe
(
method
.
to_s
,
meta
,
&
block
)
end
METHODS
.
each
do
|
method
|
define_method
(
method
)
do
|
attributes
=
{},
&
block
|
operation
(
method
,
attributes
,
&
block
)
end
end
private
def
validate_method!
method
unless
METHODS
.
include?
method
.
to_s
raise
ArgumentError
,
"Operation has an invalid 'method' value. Try:
#{
METHODS
}
."
end
end
end
end
end
...
@@ -130,7 +146,7 @@ module RSpec
...
@@ -130,7 +146,7 @@ module RSpec
raise
ArgumentError
,
"Parameter is missing required 'in' value."
raise
ArgumentError
,
"Parameter is missing required 'in' value."
end
end
locations
=
%
q
(query header path formData body)
locations
=
%
w
(query header path formData body)
unless
locations
.
include?
location
.
to_s
unless
locations
.
include?
location
.
to_s
raise
ArgumentError
,
"Parameter has an invalid 'in' value. Try:
#{
locations
}
."
raise
ArgumentError
,
"Parameter has an invalid 'in' value. Try:
#{
locations
}
."
end
end
...
@@ -141,7 +157,7 @@ module RSpec
...
@@ -141,7 +157,7 @@ module RSpec
raise
ArgumentError
,
"Parameter is missing required 'type' value."
raise
ArgumentError
,
"Parameter is missing required 'type' value."
end
end
types
=
%
q
(string number integer boolean array file)
types
=
%
w
(string number integer boolean array file)
unless
types
.
include?
type
.
to_s
unless
types
.
include?
type
.
to_s
raise
ArgumentError
,
"Parameter has an invalid 'type' value. Try:
#{
types
}
."
raise
ArgumentError
,
"Parameter has an invalid 'type' value. Try:
#{
types
}
."
end
end
...
@@ -170,7 +186,14 @@ module RSpec
...
@@ -170,7 +186,14 @@ module RSpec
describe
(
status_code
,
meta
)
do
describe
(
status_code
,
meta
)
do
self
.
module_exec
(
&
block
)
if
block_given?
self
.
module_exec
(
&
block
)
if
block_given?
# TODO: describe the wacky ness to get the metadata and access to let() defined values...
# To make a request we need:
# - the details we've collected in the metadata
# - parameter values defined using let()
# RSpec tries to limit access to metadata inside of it() / before()
# / after() blocks but that scope is the only place you can access
# the let() values. The solution the swagger_rails dev came up with
# is to use the example.metadata passed into the block with the
# block's scope which has access to the let() values.
before
do
|
example
|
before
do
|
example
|
builder
=
RequestBuilder
.
new
(
example
.
metadata
,
self
)
builder
=
RequestBuilder
.
new
(
example
.
metadata
,
self
)
method
=
builder
.
method
method
=
builder
.
method
...
@@ -185,12 +208,14 @@ module RSpec
...
@@ -185,12 +208,14 @@ module RSpec
self
.
send
(
method
,
path
,
body
,
headers
)
self
.
send
(
method
,
path
,
body
,
headers
)
end
end
if
example
.
metadata
[
:capture_example
]
if
example
.
metadata
[
:capture_example
s
]
examples
=
example
.
metadata
[
:swagger_response
][
:examples
]
||=
{}
examples
=
example
.
metadata
[
:swagger_response
][
:examples
]
||=
{}
examples
[
response
.
content_type
.
to_s
]
=
response
.
body
examples
[
response
.
content_type
.
to_s
]
=
response
.
body
end
end
end
end
# TODO: see if we can get the caller to show up in the error
# backtrace for this test.
it
(
"returns the correct status code"
)
do
it
(
"returns the correct status code"
)
do
expect
(
response
).
to
have_http_status
(
status_code
)
expect
(
response
).
to
have_http_status
(
status_code
)
end
end
...
@@ -214,7 +239,7 @@ module RSpec
...
@@ -214,7 +239,7 @@ module RSpec
module
Response
module
Response
def
capture_example
def
capture_example
metadata
[
:capture_example
]
=
true
metadata
[
:capture_example
s
]
=
true
end
end
end
end
end
end
...
...
spec/requests/request_spec.rb
View file @
c4b5175f
...
@@ -15,7 +15,7 @@ RSpec.describe "Requestsing", type: :request do
...
@@ -15,7 +15,7 @@ RSpec.describe "Requestsing", type: :request do
response
(
200
,
{
description:
"successful"
},
{})
response
(
200
,
{
description:
"successful"
},
{})
end
end
operation
"POST"
,
summary:
"create"
do
post
summary:
"create"
do
produces
'application/json'
produces
'application/json'
consumes
'application/json'
consumes
'application/json'
...
@@ -38,7 +38,7 @@ RSpec.describe "Requestsing", type: :request do
...
@@ -38,7 +38,7 @@ RSpec.describe "Requestsing", type: :request do
parameter
"post_id"
,
{
in: :path
,
type: :integer
}
parameter
"post_id"
,
{
in: :path
,
type: :integer
}
let
(
:post_id
)
{
1
}
let
(
:post_id
)
{
1
}
operation
"GET"
,
summary:
"fetch item"
do
get
summary:
"fetch item"
do
produces
'application/json'
produces
'application/json'
before
{
Post
.
new
.
save
}
before
{
Post
.
new
.
save
}
...
...
spec/rspec/swagger/helpers_spec.rb
View file @
c4b5175f
...
@@ -48,7 +48,7 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
...
@@ -48,7 +48,7 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
describe
"#operation"
do
describe
"#operation"
do
it
"requires
only an HTTP
verb"
do
it
"requires
a
verb"
do
expect
(
subject
).
to
receive
(
:describe
).
with
(
'get'
,
{
expect
(
subject
).
to
receive
(
:describe
).
with
(
'get'
,
{
swagger_object: :operation
,
swagger_object: :operation
,
swagger_operation:
{
method: :get
}
swagger_operation:
{
method: :get
}
...
@@ -57,7 +57,15 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
...
@@ -57,7 +57,15 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
subject
.
operation
(
'GET'
)
subject
.
operation
(
'GET'
)
end
end
it
"accepts other options"
do
it
'validates the verb'
do
expect
{
subject
.
operation
(
'foo'
)
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
operation
(
:foo
)
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
operation
(
:head
)
}.
not_to
raise_exception
expect
{
subject
.
operation
(
'patch'
)
}.
not_to
raise_exception
end
it
'accepts additional options'
do
expect
(
subject
).
to
receive
(
:describe
).
with
(
'head'
,
{
expect
(
subject
).
to
receive
(
:describe
).
with
(
'head'
,
{
swagger_object: :operation
,
swagger_object: :operation
,
swagger_operation:
{
swagger_operation:
{
...
@@ -74,7 +82,19 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
...
@@ -74,7 +82,19 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
operationId:
'updatePetWithForm'
operationId:
'updatePetWithForm'
)
)
end
end
end
described_class
::
METHODS
.
each
do
|
method
|
describe
"#
#{
method
}
"
do
it
'calls #operation'
do
expect
(
subject
).
to
receive
(
:describe
).
with
(
method
.
to_s
,
{
swagger_object: :operation
,
swagger_operation:
{
method:
method
.
to_sym
}
})
subject
.
send
(
method
)
end
end
end
end
end
end
...
...
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