Refactor request_factory & response_validator

This commit is contained in:
domaindrivendev
2017-07-13 16:00:39 -07:00
parent f7c29267fe
commit 97c2a39cfa
13 changed files with 454 additions and 434 deletions

View File

@@ -4,4 +4,9 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session
wrap_parameters format: [ :json ]
respond_to :json
rescue_from 'ActionController::UnknownFormat' do |ex|
head :not_acceptable
end
end

View File

@@ -1,6 +1,4 @@
class AuthTestsController < ApplicationController
wrap_parameters Blog
respond_to :json
# POST /auth-tests/basic
def basic

View File

@@ -1,6 +1,4 @@
class BlogsController < ApplicationController
wrap_parameters Blog
respond_to :json
# POST /blogs
def create

View File

@@ -1,5 +1,5 @@
TestApp::Application.routes.draw do
resources :blogs, defaults: { :format => :json }
resources :blogs
post 'auth-tests/basic', to: 'auth_tests#basic'

View File

@@ -9,10 +9,12 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
description 'Creates a new blog from provided data'
operationId 'createBlog'
consumes 'application/json'
produces 'application/json'
parameter name: :blog, :in => :body, schema: { '$ref' => '#/definitions/blog' }
let(:blog) { { title: 'foo', content: 'bar' } }
response '201', 'blog created' do
let(:blog) { { title: 'foo', content: 'bar' } }
run_test!
end
@@ -22,6 +24,11 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
let(:blog) { { title: 'foo' } }
run_test!
end
response '406', 'unsupported accept header' do
let(:'Accept') { 'application/foo' }
run_test!
end
end
get 'Searches blogs' do
@@ -31,10 +38,15 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
produces 'application/json'
parameter name: :keywords, in: :query, type: 'string'
let(:keywords) { 'foo bar' }
response '200', 'success' do
schema type: 'array', items: { '$ref' => '#/definitions/blog' }
run_test!
end
let(:keywords) { 'foo bar' }
response '406', 'unsupported accept header' do
let(:'Accept') { 'application/foo' }
run_test!
end
end
@@ -43,6 +55,9 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
path '/blogs/{id}' do
parameter name: :id, :in => :path, :type => :string
let(:id) { blog.id }
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
get 'Retrieves a blog' do
tags 'Blogs'
description 'Retrieves a specific blog by id'
@@ -62,8 +77,6 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
content: 'Hello world and hello universe. Thank you all very much!!!'
}
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
let(:id) { blog.id }
run_test!
end
@@ -71,6 +84,11 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
let(:id) { 'invalid' }
run_test!
end
response '406', 'unsupported accept header' do
let(:'Accept') { 'application/foo' }
run_test!
end
end
end
end

View File

@@ -40,6 +40,9 @@
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "blog",
@@ -58,6 +61,9 @@
"schema": {
"$ref": "#/definitions/errors_object"
}
},
"406": {
"description": "unsupported accept header"
}
}
},
@@ -87,6 +93,9 @@
"$ref": "#/definitions/blog"
}
}
},
"406": {
"description": "unsupported accept header"
}
}
}
@@ -137,6 +146,9 @@
},
"404": {
"description": "blog not found"
},
"406": {
"description": "unsupported accept header"
}
}
}