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
0b9e6ea2
Commit
0b9e6ea2
authored
Sep 22, 2016
by
andrew morton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix the formatter, make description parameters more consistent
parent
7c6501cd
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
50 deletions
+85
-50
helpers.rb
lib/rspec/swagger/helpers.rb
+50
-23
request_spec.rb
spec/requests/request_spec.rb
+10
-4
formatter_spec.rb
spec/rspec/swagger/formatter_spec.rb
+10
-14
helpers_spec.rb
spec/rspec/swagger/helpers_spec.rb
+15
-9
No files found.
lib/rspec/swagger/helpers.rb
View file @
0b9e6ea2
...
...
@@ -33,7 +33,7 @@ module RSpec
config
.
extend
Parameters
,
swagger_object: :path_item
config
.
extend
Operation
,
swagger_object: :operation
config
.
extend
Parameters
,
swagger_object: :operation
config
.
extend
Response
,
swagger_object: :
status_cod
e
config
.
extend
Response
,
swagger_object: :
respons
e
config
.
include
Resolver
,
:swagger_object
end
...
...
@@ -72,26 +72,13 @@ module RSpec
def
parameter
name
,
attributes
=
{}
attributes
.
symbolize_keys!
raise
ArgumentError
,
"Parameter is missing required 'in' value."
unless
attributes
[
:in
]
locations
=
[
:query
,
:header
,
:path
,
:formData
,
:body
]
unless
locations
.
include?
attributes
[
:in
]
raise
ArgumentError
,
"Parameter has an invalid 'in' value. Try:
#{
locations
}
."
end
validate_location!
attributes
[
:in
]
if
attributes
[
:in
]
==
:body
unless
attributes
[
:schema
].
present?
raise
ArgumentError
,
"Parameter is missing required 'schema' value."
end
else
unless
attributes
[
:type
].
present?
raise
ArgumentError
,
"Parameter is missing required 'type' value."
end
types
=
%i(string number integer boolean array file)
unless
types
.
include?
(
attributes
[
:type
])
raise
ArgumentError
,
"Parameter has an invalid 'type' value. Try:
#{
types
}
."
end
validate_type!
attributes
[
:type
]
end
# Path attributes are always required
...
...
@@ -111,6 +98,30 @@ module RSpec
param_key
=
"
#{
param
[
:in
]
}
&
#{
param
[
:name
]
}
"
params
[
param_key
]
=
param
end
private
def
validate_location!
location
unless
location
.
present?
raise
ArgumentError
,
"Parameter is missing required 'in' value."
end
locations
=
%i(query header path formData body)
unless
locations
.
include?
location
raise
ArgumentError
,
"Parameter has an invalid 'in' value. Try:
#{
locations
}
."
end
end
def
validate_type!
type
unless
type
.
present?
raise
ArgumentError
,
"Parameter is missing required 'type' value."
end
types
=
%i(string number integer boolean array file)
unless
types
.
include?
(
type
)
raise
ArgumentError
,
"Parameter has an invalid 'type' value. Try:
#{
types
}
."
end
end
end
module
Operation
...
...
@@ -122,13 +133,15 @@ module RSpec
metadata
[
:swagger_operation
][
:produces
]
=
mime_types
end
def
response
status_code
,
desc
,
params
=
{},
&
block
unless
status_code
==
:default
||
(
100
..
599
).
cover?
(
status_code
)
raise
ArgumentError
,
"status_code must be an integer 100 to 599, or :default"
end
def
response
status_code
,
attributes
=
{},
params
=
{},
&
block
attributes
.
symbolize_keys!
validate_status_code!
status_code
validate_description!
attributes
[
:description
]
meta
=
{
swagger_object: :
status_cod
e
,
swagger_response:
{
status_code:
status_code
,
description:
desc
}
swagger_object: :
respons
e
,
swagger_response:
attributes
.
merge
(
status_code:
status_code
)
}
describe
(
status_code
,
meta
)
do
self
.
module_exec
(
&
block
)
if
block_given?
...
...
@@ -152,11 +165,25 @@ module RSpec
end
end
it
(
"returns the correct status code"
,
{
swagger_object: :response
}
)
do
it
(
"returns the correct status code"
)
do
expect
(
response
).
to
have_http_status
(
status_code
)
end
end
end
private
def
validate_status_code!
status_code
unless
status_code
==
:default
||
(
100
..
599
).
cover?
(
status_code
)
raise
ArgumentError
,
"status_code must be an integer 100 to 599, or :default"
end
end
def
validate_description!
description
unless
description
.
present?
raise
ArgumentError
,
"Response is missing required 'description' value."
end
end
end
module
Response
...
...
spec/requests/request_spec.rb
View file @
0b9e6ea2
require
'swagger_helper'
RSpec
.
describe
"Requestsing"
,
type: :request
do
# path "/ping" do
# operation :put do
# response(200, {description: 'OK'})
# puts "in here"
# end
# end
path
'/posts'
do
operation
"GET"
,
summary
:"fetch list"
do
produces
'application/json'
# params
response
(
200
,
"successful"
,
{})
response
(
200
,
{
description:
"successful"
},
{})
end
operation
"POST"
,
summary:
"create"
do
...
...
@@ -17,7 +23,7 @@ RSpec.describe "Requestsing", type: :request do
let
(
:body
)
{
{
post:
{
title:
'asdf'
,
body:
"blah"
}
}
}
# TODO: it should pull the body from the params
response
(
201
,
"successfully created"
,
{
post:
{
title:
'asdf'
,
body:
"blah"
}
}.
to_json
)
do
response
(
201
,
{
description:
"successfully created"
}
,
{
post:
{
title:
'asdf'
,
body:
"blah"
}
}.
to_json
)
do
it
"uses the body we passed in"
do
post
=
JSON
.
parse
(
response
.
body
)
expect
(
post
[
"title"
]).
to
eq
(
'asdf'
)
...
...
@@ -37,7 +43,7 @@ RSpec.describe "Requestsing", type: :request do
before
{
Post
.
new
.
save
}
parameter
"op-param"
,
{
in: :query
,
type: :string
}
response
(
200
,
"success"
,
{})
do
response
(
200
,
{
description:
"success"
}
,
{})
do
capture_example
end
end
...
...
spec/rspec/swagger/formatter_spec.rb
View file @
0b9e6ea2
...
...
@@ -27,13 +27,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let
(
:metadata
)
do
{
swagger_object: :response
,
swagger_data:
{
path:
"/ping"
,
operation: :put
,
status_code:
200
,
response_description:
'OK'
,
example:
nil
}
swagger_path_item:
{
path:
"/ping"
},
swagger_operation:
{
method: :put
},
swagger_response:
{
status_code:
200
,
description:
"OK"
},
}
end
...
...
@@ -55,12 +51,10 @@ RSpec.describe RSpec::Swagger::Formatter do
let
(
:metadata
)
do
{
swagger_object: :response
,
swagger_data:
{
document:
'doc2.json'
,
path:
"/ping"
,
operation: :put
,
status_code:
200
}
swagger_document:
'doc2.json'
,
swagger_path_item:
{
path:
"/ping"
},
swagger_operation:
{
method: :put
},
swagger_response:
{
status_code:
200
,
description:
"OK"
},
}
end
...
...
@@ -88,7 +82,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let
(
:metadata
)
do
{
swagger_object: :response
,
swagger_data:
{
path:
'/ping'
,
operation: :get
,
status_code:
200
,
response_description:
'all good'
}
swagger_path_item:
{
path:
"/ping"
},
swagger_operation:
{
method: :get
},
swagger_response:
{
status_code:
200
,
description:
'all good'
},
}
end
...
...
spec/rspec/swagger/helpers_spec.rb
View file @
0b9e6ea2
...
...
@@ -83,6 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Parameters do
Class
.
new
do
include
RSpec
::
Swagger
::
Helpers
::
Parameters
attr_accessor
:metadata
def
describe
*
args
;
end
end
end
subject
{
klass
.
new
}
...
...
@@ -142,6 +143,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
Class
.
new
do
include
RSpec
::
Swagger
::
Helpers
::
Operation
attr_accessor
:metadata
def
describe
*
args
;
end
end
end
subject
{
klass
.
new
}
...
...
@@ -150,15 +152,19 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
before
{
subject
.
metadata
=
{
swagger_object: :operation
}
}
it
"requires code be an integer 100...600 or :default"
do
expect
{
subject
.
response
1
,
"description"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
99
,
"description"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
600
,
"description"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
'404'
,
"description"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
'default'
,
"description"
}.
to
raise_exception
(
ArgumentError
)
# expect{ subject.response 100, "description" }.not_to raise_exception
# expect{ subject.response 599, "description" }.not_to raise_exception
# expect{ subject.response :default, "description" }.not_to raise_exception
expect
{
subject
.
response
99
,
description:
"too low"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
600
,
description:
"too high"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
'404'
,
description:
"string"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
'default'
,
description:
"string"
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
100
,
description:
"low"
}.
not_to
raise_exception
expect
{
subject
.
response
599
,
description:
"high"
}.
not_to
raise_exception
expect
{
subject
.
response
:default
,
description:
"symbol"
}.
not_to
raise_exception
end
it
"requires a description"
do
expect
{
subject
.
response
100
}.
to
raise_exception
(
ArgumentError
)
expect
{
subject
.
response
100
,
description:
"low"
}.
not_to
raise_exception
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