Commit c235162b by andrew morton

Few cleanups

parent 22adfbaa
...@@ -6,7 +6,14 @@ The design of this is heavily influenced by the awesome [swagger_rails](https:// ...@@ -6,7 +6,14 @@ The design of this is heavily influenced by the awesome [swagger_rails](https://
- install gem - install gem
- `rails generate rspec:install` - `rails generate rspec:install`
- create `spec/swagger_helper.rb` ... would be nice to be a generator - create `spec/swagger_helper.rb` (I'll try to get a generator to automate this)
- define your tests (I definitely need to make this step more explicit)
## Generate the docs
```
bundle exec rspec -f RSpec::Swagger::Formatter --order defined -t swagger_object
```
## Running tests ## Running tests
......
...@@ -174,15 +174,15 @@ module RSpec ...@@ -174,15 +174,15 @@ module RSpec
before do |example| before do |example|
builder = RequestBuilder.new(example.metadata, self) builder = RequestBuilder.new(example.metadata, self)
method = builder.method method = builder.method
path = builder.path + builder.query path = [builder.path, builder.query].join
headers = builder.headers headers = builder.headers
params = resolve_params(example.metadata, self) body = builder.body
# Run the request # Run the request
if ::Rails::VERSION::MAJOR >= 5 if ::Rails::VERSION::MAJOR >= 5
self.send(method, path, {params: params, headers: headers}) self.send(method, path, {params: body, headers: headers})
else else
self.send(method, path, params, headers) self.send(method, path, body, headers)
end end
if example.metadata[:capture_example] if example.metadata[:capture_example]
......
...@@ -49,22 +49,24 @@ module RSpec ...@@ -49,22 +49,24 @@ module RSpec
# in the example group. # in the example group.
path = base_path + metadata[:swagger_path_item][:path].gsub(/(\{.*?\})/) do |match| path = base_path + metadata[:swagger_path_item][:path].gsub(/(\{.*?\})/) do |match|
# QUESTION: Should check that the parameter is actually defined in # QUESTION: Should check that the parameter is actually defined in
# `metadata[:swagger_*][:parameters]` before fetch a value? # `parameters` before fetch a value?
instance.send(match[1...-1]) instance.send(match[1...-1])
end end
end end
def query def query
# Don't bother with the parameter bodies since all we need is location # Don't bother looking at the full parameter bodies since all we need
# and name which make up the key. # are location and name which are the key.
query_params = parameters.keys query_params = parameters.keys.map{ |k| k.split('&')}
.map{ |k| k.split('&')}
.select{ |location, name| location == 'query' } .select{ |location, name| location == 'query' }
.map{ |location, name| [name, instance.send(name)] } .map{ |location, name| [name, instance.send(name)] }
'?' + Hash[query_params].to_query unless query_params.empty? '?' + Hash[query_params].to_query unless query_params.empty?
end end
def body 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.keys.find{ |k| k.starts_with? 'body&' }
instance.send(key.split('&').last).to_json instance.send(key.split('&').last).to_json
end end
......
...@@ -42,7 +42,6 @@ RSpec.describe "Requestsing", type: :request do ...@@ -42,7 +42,6 @@ RSpec.describe "Requestsing", type: :request do
produces 'application/json' produces 'application/json'
before { Post.new.save } before { Post.new.save }
parameter "op-param", {in: :query, type: :string}
response(200, {description: "success"}, {}) do response(200, {description: "success"}, {}) do
capture_example capture_example
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