spec/integration/rails/camel_caser_spec.rb in cp-sparrow-0.0.14 vs spec/integration/rails/camel_caser_spec.rb in cp-sparrow-0.0.15
- old
+ new
@@ -8,22 +8,22 @@
last_name: 'smith',
bar: { lordFüü: 12 },
"DE" => 'german',
}
end
- let(:json) { MultiJson.dump(json_object) }
+ let(:json) { JSON.generate(json_object) }
context "accept header is given" do
context 'path not excluded' do
before do
post '/posts', json, { 'request-json-format' => 'underscore',
'response-json-format' => 'underscore',
'CONTENT-TYPE' => 'application/json' }
end
- subject { MultiJson.load(last_response.body) }
+ subject { JSON.parse(last_response.body) }
it "converts lower camelcase to underscore params" do
expect(last_response).to be_successful
expect(subject).to have_key("keys")
expect(subject["keys"]).to include("user_name")
@@ -38,11 +38,11 @@
get '/ignore', json_object, { 'CONTENT-TYPE' => 'application/json',
'request-json-format' => 'underscore',
'response-json-format' => 'underscore' }
end
- subject { MultiJson.load(last_response.body) }
+ subject { JSON.parse(last_response.body) }
it 'should not touch the input keys and the response' do
expect(subject).to have_key('camelCase')
expect(subject).to have_key('snake_case')
expect(subject).to have_key('keys')
@@ -159,11 +159,11 @@
{
'request-json-format' => 'underscore',
'CONTENT-TYPE' => 'application/json'
}
end
- subject { MultiJson.load(last_response.body) }
+ subject { JSON.parse(last_response.body) }
it 'should return an array as root element' do
expect(subject).to_not have_key("user_options")
expect(subject).to have_key("userOptions")
expect(subject["userOptions"]).to have_key("firstName")
@@ -178,22 +178,23 @@
before do
post '/posts', json, { "CONTENT_TYPE" => 'text/x-json' }
end
subject do
- MultiJson.load(last_response.body)
+ JSON.parse(last_response.body)
end
- it "did not convert lower camelcase to underscore params" do
+ it "converts incoming request parameters to snake_case" do
expect(subject).to have_key("keys")
- expect(subject["keys"]).to include("userName")
+ expect(subject["keys"]).to include("user_name")
end
- it "did not convert all UPPERCASE words to underscore params" do
+ # not touching UPPERCASE params only works for camelizing strategies
+ it "converts UPPERCASE params when using underscore strategy" do
expect(subject).to have_key("keys")
- expect(subject["keys"]).to include("DE")
+ expect(subject["keys"]).to include("de")
end
end
context 'convert response output keys' do
@@ -202,11 +203,11 @@
'request-json-format' => 'camelize',
'response-json-format' => 'underscore' }
end
subject do
- MultiJson.load(last_response.body)
+ JSON.parse(last_response.body)
end
it 'underscores the response' do
expect(subject).to_not have_key('fakeKey')
expect(subject).to have_key('fake_key')
@@ -218,41 +219,50 @@
before do
get '/array_of_elements', nil, {
'CONTENT-TYPE' => 'application/json; charset=utf-8'
}
end
- subject { MultiJson.load(last_response.body) }
+ subject { JSON.parse(last_response.body) }
it 'should return an array as root element' do
expect(subject.class).to eq Array
expect(subject.first).to_not have_key("fake_key")
expect(subject.first).to have_key("fakeKey")
end
end
- context 'reaction on error responses' do
+ describe 'the reaction on error responses' do
require 'action_controller/metal/exceptions'
it 'lets Rails do its RoutingError when the url is not found' do
expect do
get '/not_found_url', {}, { 'CONTENT-TYPE' => 'application/json' }
end.to raise_error ActionController::RoutingError
end
it 'does not touch the response if a server error gets triggered' do
- expect {
+ expect do
get '/error', {}, { 'CONTENT-TYPE' => 'application/json' }
- }.to raise_error ZeroDivisionError
+ end.to raise_error ZeroDivisionError
end
+
+ it 'does not fail on 507 error' do
+ expect do
+ get '/error-507', {}, 'CONTENT-TYPE' => 'appliation/json',
+ 'Accept' => 'application/json'
+ end.to_not raise_error
+ expect(last_response.status).to eq 507
+ expect(JSON.parse(last_response.body)).to eq({'error_code' => 507})
+ end
end
describe 'the configuration of allowed content types' do
it 'does not process requests and responses that have disallowed content types' do
get '/welcome', json_object, { 'CONTENT_TYPE' => 'text/html',
'request-json-format' => 'underscore' }
- last_json = MultiJson.load(last_response.body)
+ last_json = JSON.parse(last_response.body)
expect(last_json).to have_key 'fakeKey'
expect(last_json).to have_key 'keys'
end
it 'processes nothing if content-types configured contains nil and content type is sent' do
@@ -262,11 +272,11 @@
post '/posts', json, { 'request-json-format' => 'underscore',
'response-json-format' => 'underscore',
'CONTENT-TYPE' => '' }
- last_json = MultiJson.load(last_response.body)
+ last_json = JSON.parse(last_response.body)
expect(last_json['keys']).to include('userName')
expect(last_json['keys']).to include('DE')
end
it 'processes everything if content-types configured contains nil and content-type is empty' do
@@ -276,27 +286,28 @@
post '/posts', json, { 'request-json-format' => 'underscore',
'response-json-format' => 'underscore',
'CONTENT_TYPE' => ''}
- last_json = MultiJson.load(last_response.body)
+ last_json = JSON.parse(last_response.body)
expect(last_json['keys']).to include('user_name')
expect(last_json['keys']).to include('bar')
# at the moment the "let uppercase as it is"-option only works for
# camelCase. This test implies that.
expect(last_json['keys']).to include('de')
end
-
+
it 'processes everything if content-types configured contains nil and no content-type is sent' do
+ skip("Test is the same as above? Removal of content_type will fail the test.")
Sparrow.configure do |config|
config.allowed_content_types = [nil]
end
post '/posts', json, { 'request-json-format' => 'underscore',
'response-json-format' => 'underscore',
'CONTENT_TYPE' => ''}
- last_json = MultiJson.load(last_response.body)
+ last_json = JSON.parse(last_response.body)
expect(last_json['keys']).to include('user_name')
expect(last_json['keys']).to include('bar')
# at the moment the "let uppercase as it is"-option only works for
# camelCase. This test implies that.
expect(last_json['keys']).to include('de')