-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve mime type registration for Rails 5
- Loading branch information
Showing
5 changed files
with
194 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
require 'support/isolated_unit' | ||
require 'minitest/mock' | ||
require 'action_dispatch' | ||
require 'action_controller' | ||
|
||
class JsonApiRendererTest < ActionDispatch::IntegrationTest | ||
include ActiveSupport::Testing::Isolation | ||
|
||
class TestController < ActionController::Base | ||
class << self | ||
attr_accessor :last_request_parameters | ||
end | ||
|
||
def render_with_jsonapi_renderer | ||
author = Author.new(params[:data][:attributes]) | ||
render jsonapi: author | ||
end | ||
|
||
def parse | ||
self.class.last_request_parameters = request.request_parameters | ||
head :ok | ||
end | ||
end | ||
|
||
def teardown | ||
TestController.last_request_parameters = nil | ||
end | ||
|
||
def assert_parses(expected, actual, headers = {}) | ||
post '/parse', params: actual, headers: headers | ||
assert_response :ok | ||
assert_equal(expected, TestController.last_request_parameters) | ||
end | ||
|
||
class WithoutRenderer < JsonApiRendererTest | ||
setup do | ||
require 'rails' | ||
require 'active_record' | ||
require 'support/rails5_shims' | ||
require 'active_model_serializers' | ||
require 'fixtures/poro' | ||
|
||
make_basic_app | ||
|
||
Rails.application.routes.draw do | ||
ActiveSupport::Deprecation.silence do | ||
match ':action', :to => TestController, via: [:get, :post] | ||
end | ||
end | ||
end | ||
|
||
def test_jsonapi_parser_not_registered | ||
parsers = if Rails::VERSION::MAJOR >= 5 | ||
ActionDispatch::Request.parameter_parsers | ||
else | ||
ActionDispatch::ParamsParser::DEFAULT_PARSERS | ||
end | ||
mime_type = Mime::Type.lookup('application/vnd.api+json') | ||
assert_nil parsers[mime_type] | ||
end | ||
|
||
def test_jsonapi_renderer_not_registered | ||
expected = { | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
} | ||
} | ||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}' | ||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' } | ||
post '/render_with_jsonapi_renderer', params: payload, headers: headers | ||
assert expected, response.body | ||
end | ||
|
||
def test_jsonapi_parser | ||
assert_parses( | ||
{}, | ||
'', | ||
'CONTENT_TYPE' => 'application/vnd.api+json' | ||
) | ||
end | ||
end | ||
|
||
class WithRenderer < JsonApiRendererTest | ||
setup do | ||
require 'rails' | ||
require 'active_record' | ||
require 'support/rails5_shims' | ||
require 'active_model_serializers' | ||
require 'fixtures/poro' | ||
require 'active_model_serializers/register_jsonapi_renderer' | ||
|
||
make_basic_app | ||
|
||
Rails.application.routes.draw do | ||
ActiveSupport::Deprecation.silence do | ||
match ':action', :to => TestController, via: [:get, :post] | ||
end | ||
end | ||
end | ||
|
||
def test_jsonapi_parser_registered | ||
if Rails::VERSION::MAJOR >= 5 | ||
parsers = ActionDispatch::Request.parameter_parsers | ||
assert_equal Proc, parsers[:jsonapi].class | ||
else | ||
parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS | ||
mime_type = Mime::Type.lookup('application/vnd.api+json') | ||
assert_equal Proc, parsers[mime_type].class | ||
end | ||
end | ||
|
||
def test_jsonapi_renderer_registered | ||
expected = { | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
} | ||
} | ||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}' | ||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' } | ||
post '/render_with_jsonapi_renderer', params: payload, headers: headers | ||
assert expected, response.body | ||
end | ||
|
||
def test_jsonapi_parser | ||
assert_parses( | ||
{ | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'John Doe' | ||
}, | ||
'type' => 'users' | ||
} | ||
}, | ||
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}', | ||
'CONTENT_TYPE' => 'application/vnd.api+json' | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.