cp-sparrow 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/README.md +3 -1
- data/Rakefile +9 -5
- data/lib/sparrow/middleware.rb +4 -15
- data/lib/sparrow/request_middleware.rb +18 -0
- data/lib/sparrow/response_middleware.rb +7 -0
- data/lib/sparrow/strategies/ignore.rb +4 -0
- data/lib/sparrow/version.rb +1 -1
- data/spec/integration/apps/rails_app/app/assets/images/grumpy-cat.gif +0 -0
- data/spec/integration/apps/rails_app/app/controllers/welcome_controller.rb +10 -0
- data/spec/integration/apps/rails_app/config/routes.rb +4 -0
- data/spec/integration/rails/camel_caser_spec.rb +120 -20
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c0adb0e16e2c7a97a4b7c4d6db2506cd3c3f91e
|
4
|
+
data.tar.gz: 7e58676322eb7f4981618f359ab3a2fcef609bd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e653bf471ed0d7d59ce627d1b26dd219b9807bf42e52663af3a3a4f884ea53c3a1c6472fdeb02cda302666ebdb2a9d293cbcdd0195d6f163b8581f1f38cec34c
|
7
|
+
data.tar.gz: a4542efb1cd4984760480db9b200bc1e6da846bbfde61b44294715c83ae3d625e486be14bef7bf01c9ccf4fa49daa6c0c9d91a4de6d6858e724e59e5094c2e0b
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
A Rack middleware for converting the params keys and JSON response keys of a Rack application.
|
4
4
|
|
5
|
+
[](https://travis-ci.org/GateprotectGmbH/sparrow) [](http://badge.fury.io/rb/cp-sparrow)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -14,7 +16,7 @@ And then execute:
|
|
14
16
|
|
15
17
|
Or install it yourself as:
|
16
18
|
|
17
|
-
$ gem install sparrow
|
19
|
+
$ gem install cp-sparrow
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ require "rspec/core/rake_task"
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
6
|
RAILS_VERSIONS = [
|
7
|
-
"3.2.
|
7
|
+
"3.2.21",
|
8
8
|
"4.0.13"
|
9
9
|
]
|
10
10
|
|
@@ -12,14 +12,14 @@ def run_tests_for_version(version)
|
|
12
12
|
commands = []
|
13
13
|
|
14
14
|
commands << "rm Gemfile.lock"
|
15
|
-
commands << "
|
16
|
-
commands << "bundle
|
15
|
+
commands << "gem install rails -v #{version}"
|
16
|
+
commands << "bundle install"
|
17
17
|
commands << "bundle exec rspec"
|
18
18
|
|
19
|
-
system(commands.join(';'))
|
19
|
+
system({'RAILS_VERSION' => version}, commands.join(';'))
|
20
20
|
end
|
21
21
|
|
22
|
-
task :
|
22
|
+
task :all do
|
23
23
|
RAILS_VERSIONS.each do |version|
|
24
24
|
puts "Testing gem for rails version: #{version}"
|
25
25
|
success = run_tests_for_version(version)
|
@@ -30,3 +30,7 @@ task :default do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
33
|
+
|
34
|
+
task :default do
|
35
|
+
run_tests_for_version(ENV['RAILS_VERSION'] || '3.2.21')
|
36
|
+
end
|
data/lib/sparrow/middleware.rb
CHANGED
@@ -24,12 +24,10 @@ module Sparrow
|
|
24
24
|
|
25
25
|
def strategy
|
26
26
|
if is_processable?
|
27
|
-
if
|
28
|
-
|
29
|
-
else
|
30
|
-
Strategies::RawInput
|
31
|
-
end
|
27
|
+
Rails.logger.debug 'Choosing strategy RawInput' if defined? Rails
|
28
|
+
Strategies::RawInput
|
32
29
|
else
|
30
|
+
Rails.logger.debug 'Choosing strategy Ignore' if defined? Rails
|
33
31
|
Strategies::Ignore
|
34
32
|
end
|
35
33
|
end
|
@@ -44,7 +42,7 @@ module Sparrow
|
|
44
42
|
end
|
45
43
|
|
46
44
|
def accepted_content_type?
|
47
|
-
content_type_equals?(
|
45
|
+
content_type_equals?(content_type) || content_type_matches?(content_type)
|
48
46
|
end
|
49
47
|
|
50
48
|
def accepted_accept_header?
|
@@ -68,15 +66,6 @@ module Sparrow
|
|
68
66
|
request_class.new(last_env)
|
69
67
|
end
|
70
68
|
|
71
|
-
def request_content_type
|
72
|
-
content_type = request.content_type ||
|
73
|
-
last_env['CONTENT-TYPE'] ||
|
74
|
-
last_env['Content-Type'] ||
|
75
|
-
last_env['CONTENT_TYPE']
|
76
|
-
|
77
|
-
content_type.present? ? content_type : nil
|
78
|
-
end
|
79
|
-
|
80
69
|
def content_type_equals?(type)
|
81
70
|
Sparrow.configuration.allowed_content_types.include?(type)
|
82
71
|
end
|
@@ -7,5 +7,23 @@ module Sparrow
|
|
7
7
|
strategy.handle(env, :request)
|
8
8
|
env
|
9
9
|
end
|
10
|
+
|
11
|
+
def content_type
|
12
|
+
my_content_type = request.content_type ||
|
13
|
+
last_env['CONTENT-TYPE'] ||
|
14
|
+
last_env['Content-Type'] ||
|
15
|
+
last_env['CONTENT_TYPE']
|
16
|
+
|
17
|
+
my_content_type.present? ? my_content_type : nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def strategy
|
21
|
+
if is_processable? && last_env[Strategies::FormHash::REQUEST_FORM_HASH_KEY]
|
22
|
+
Rails.logger.debug 'Choosing strategy FormHash' if defined? Rails
|
23
|
+
Strategies::FormHash
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
10
28
|
end
|
11
29
|
end
|
@@ -33,5 +33,12 @@ module Sparrow
|
|
33
33
|
def unprocessable_status?
|
34
34
|
@status.in?(500..511) || @status == 404
|
35
35
|
end
|
36
|
+
|
37
|
+
def content_type
|
38
|
+
headers['Content-Type'].split(';').first #||
|
39
|
+
# last_env['CONTENT-TYPE'] ||
|
40
|
+
# last_env['Content-Type'] ||
|
41
|
+
# last_env['CONTENT_TYPE']
|
42
|
+
end
|
36
43
|
end
|
37
44
|
end
|
data/lib/sparrow/version.rb
CHANGED
Binary file
|
@@ -11,6 +11,16 @@ class WelcomeController < ApplicationController
|
|
11
11
|
render json: {keys: extract_keys, camelCase: false, snake_case: true}
|
12
12
|
end
|
13
13
|
|
14
|
+
def non_json_text_response
|
15
|
+
render text: "----- BEGIN PUBLIC KEY -----\n#{extract_keys}\n----- END PUBLIC KEY -----"
|
16
|
+
end
|
17
|
+
|
18
|
+
def non_json_binary_response
|
19
|
+
file = File.join(Rails.root, 'app', 'assets', 'images', 'grumpy-cat.gif')
|
20
|
+
|
21
|
+
send_file(file, filename: "#{params}.gif")
|
22
|
+
end
|
23
|
+
|
14
24
|
def posts
|
15
25
|
render json: {keys: extract_keys}
|
16
26
|
end
|
@@ -51,9 +51,13 @@ Dummy::Application.routes.draw do
|
|
51
51
|
root :to => 'welcome#index', defaults: { format: 'json' }
|
52
52
|
post '/posts' => 'welcome#posts', defaults: {format: 'json'}
|
53
53
|
get '/welcome' => 'welcome#show', default: {format: 'json'}
|
54
|
+
get '/welcome/non_json_text_response' => 'welcome#non_json_text_response', default: {format: 'json'}
|
55
|
+
get '/welcome/non_json_binary_response' => 'welcome#non_json_binary_response', default: {format: 'json'}
|
54
56
|
get '/array_of_elements' => 'welcome#array_of_elements', default: {format: 'json'}
|
55
57
|
get '/upcase_first_name' => 'welcome#upcase_first_name', default: {format: 'json'}
|
56
58
|
get '/ignore' => 'welcome#ignore', default: {format: 'json'}
|
59
|
+
get '/ignore/non_json_text_response' => 'welcome#non_json_text_response', default: {format: 'json'}
|
60
|
+
get '/ignore/non_json_binary_response' => 'welcome#non_json_binary_response', default: {format: 'json'}
|
57
61
|
get '/error' => 'welcome#error', default: {format: 'json'}
|
58
62
|
|
59
63
|
# See how all your routes lay out with "rake routes"
|
@@ -5,8 +5,9 @@ describe "camel caser middleware for Rails", type: :rails do
|
|
5
5
|
let(:json_object) do
|
6
6
|
{
|
7
7
|
userName: 'dsci',
|
8
|
+
last_name: 'smith',
|
8
9
|
bar: { lordFüü: 12 },
|
9
|
-
"DE" => 'german'
|
10
|
+
"DE" => 'german',
|
10
11
|
}
|
11
12
|
end
|
12
13
|
let(:json) { MultiJson.dump(json_object) }
|
@@ -32,21 +33,121 @@ describe "camel caser middleware for Rails", type: :rails do
|
|
32
33
|
end
|
33
34
|
|
34
35
|
context 'exclude paths' do
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
context 'ignored json in and out' do
|
37
|
+
before do
|
38
|
+
get '/ignore', json_object, { 'CONTENT-TYPE' => 'application/json',
|
39
|
+
'request-json-format' => 'underscore',
|
40
|
+
'response-json-format' => 'underscore' }
|
41
|
+
end
|
42
|
+
|
43
|
+
subject { MultiJson.load(last_response.body) }
|
44
|
+
|
45
|
+
it 'should not touch the input keys and the response' do
|
46
|
+
expect(subject).to have_key('camelCase')
|
47
|
+
expect(subject).to have_key('snake_case')
|
48
|
+
expect(subject).to have_key('keys')
|
49
|
+
keys = subject['keys']
|
50
|
+
%w(userName lordFüü bar).each do |key|
|
51
|
+
expect(keys).to include(key)
|
52
|
+
end
|
53
|
+
end
|
39
54
|
end
|
40
55
|
|
41
|
-
|
56
|
+
context 'ignored json in and ignored text out' do
|
57
|
+
before do
|
58
|
+
get '/ignore/non_json_text_response', json_object,
|
59
|
+
{ 'CONTENT-TYPE' => 'application/json',
|
60
|
+
'request-json-format' => 'underscore',
|
61
|
+
'response-json-format' => 'underscore' }
|
62
|
+
end
|
63
|
+
|
64
|
+
subject { last_response }
|
65
|
+
|
66
|
+
it { is_expected.to be_successful }
|
67
|
+
|
68
|
+
it 'should answer with a KEY' do
|
69
|
+
expect(subject.body).to match /BEGIN PUBLIC KEY/
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should not touch the keys' do
|
73
|
+
expect(subject.body).to match /userName/
|
74
|
+
expect(subject.body).to match /lordFüü/
|
75
|
+
expect(subject.body).to match /bar/
|
76
|
+
expect(subject.body).to match /last_name/
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should be content-type html' do
|
80
|
+
expect(subject.header['Content-Type']).to match /text\/html/
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'ignored json in and ignored binary out' do
|
85
|
+
before do
|
86
|
+
get '/ignore/non_json_binary_response', json_object,
|
87
|
+
{ 'CONTENT-TYPE' => 'application/json',
|
88
|
+
'request-json-format' => 'underscore',
|
89
|
+
'response-json-format' => 'underscore' }
|
90
|
+
end
|
91
|
+
|
92
|
+
subject { last_response }
|
93
|
+
|
94
|
+
it { is_expected.to be_successful }
|
95
|
+
|
96
|
+
it 'should be content-type gif' do
|
97
|
+
expect(subject.header['Content-Type']).to eql "image/gif"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'convert json in and ignored text out' do
|
102
|
+
before do
|
103
|
+
get '/welcome/non_json_text_response', json_object,
|
104
|
+
{ 'CONTENT-TYPE' => 'application/json',
|
105
|
+
'request-json-format' => 'underscore',
|
106
|
+
'response-json-format' => 'underscore' }
|
107
|
+
end
|
108
|
+
|
109
|
+
subject { last_response }
|
110
|
+
|
111
|
+
it { is_expected.to be_successful }
|
112
|
+
|
113
|
+
it 'should answer with a KEY' do
|
114
|
+
expect(last_response.body).to match /BEGIN PUBLIC KEY/
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should touch the keys' do
|
118
|
+
expect(subject.body).to match /user_name/
|
119
|
+
expect(subject.body).to match /lord_füü/
|
120
|
+
expect(subject.body).to match /bar/
|
121
|
+
expect(subject.body).to match /last_name/
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should be content-type html' do
|
125
|
+
expect(last_response.header['Content-Type']).to match /text\/html/
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "convert json in and don't stumble over binary out" do
|
130
|
+
before do
|
131
|
+
get '/welcome/non_json_binary_response', json_object,
|
132
|
+
{ 'CONTENT-TYPE' => 'application/json',
|
133
|
+
'request-json-format' => 'underscore',
|
134
|
+
'response-json-format' => 'underscore' }
|
135
|
+
end
|
42
136
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
137
|
+
subject { last_response }
|
138
|
+
|
139
|
+
it { is_expected.to be_successful }
|
140
|
+
|
141
|
+
it 'should be content-type html' do
|
142
|
+
expect(last_response.header['Content-Type']).to eq('image/gif')
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should show the converted input params as file name content' do
|
146
|
+
expect(last_response.header['Content-Disposition']).
|
147
|
+
to include("user_name")
|
148
|
+
|
149
|
+
expect(last_response.header['Content-Disposition']).
|
150
|
+
to include("lord_füü")
|
50
151
|
end
|
51
152
|
end
|
52
153
|
end
|
@@ -145,9 +246,8 @@ describe "camel caser middleware for Rails", type: :rails do
|
|
145
246
|
end
|
146
247
|
|
147
248
|
describe 'the configuration of allowed content types' do
|
148
|
-
it 'does not process requests and responses that have disallowed content
|
149
|
-
|
150
|
-
get '/welcome', json_object, { 'CONTENT-TYPE' => 'text/html',
|
249
|
+
it 'does not process requests and responses that have disallowed content types' do
|
250
|
+
get '/welcome', json_object, { 'CONTENT_TYPE' => 'text/html',
|
151
251
|
'request-json-format' => 'underscore' }
|
152
252
|
|
153
253
|
last_json = MultiJson.load(last_response.body)
|
@@ -162,7 +262,8 @@ describe "camel caser middleware for Rails", type: :rails do
|
|
162
262
|
|
163
263
|
post '/posts', json, { 'request-json-format' => 'underscore',
|
164
264
|
'response-json-format' => 'underscore',
|
165
|
-
'CONTENT-TYPE' => '
|
265
|
+
'CONTENT-TYPE' => '' }
|
266
|
+
|
166
267
|
last_json = MultiJson.load(last_response.body)
|
167
268
|
expect(last_json['keys']).to include('userName')
|
168
269
|
expect(last_json['keys']).to include('DE')
|
@@ -174,9 +275,8 @@ describe "camel caser middleware for Rails", type: :rails do
|
|
174
275
|
end
|
175
276
|
|
176
277
|
post '/posts', json, { 'request-json-format' => 'underscore',
|
177
|
-
|
178
|
-
|
179
|
-
}
|
278
|
+
'response-json-format' => 'underscore',
|
279
|
+
'CONTENT_TYPE' => ''}
|
180
280
|
|
181
281
|
last_json = MultiJson.load(last_response.body)
|
182
282
|
expect(last_json['keys']).to include('user_name')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cp-sparrow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schmidt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-06-
|
12
|
+
date: 2015-06-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- ".gitignore"
|
151
151
|
- ".ruby-gemset"
|
152
152
|
- ".ruby-version"
|
153
|
+
- ".travis.yml"
|
153
154
|
- Gemfile
|
154
155
|
- LICENSE.txt
|
155
156
|
- README.md
|
@@ -181,6 +182,7 @@ files:
|
|
181
182
|
- spec/integration/apps/rack_app/config.ru
|
182
183
|
- spec/integration/apps/rails_app/README.rdoc
|
183
184
|
- spec/integration/apps/rails_app/Rakefile
|
185
|
+
- spec/integration/apps/rails_app/app/assets/images/grumpy-cat.gif
|
184
186
|
- spec/integration/apps/rails_app/app/assets/javascripts/application.js
|
185
187
|
- spec/integration/apps/rails_app/app/assets/stylesheets/application.css
|
186
188
|
- spec/integration/apps/rails_app/app/controllers/application_controller.rb
|
@@ -251,6 +253,7 @@ test_files:
|
|
251
253
|
- spec/integration/apps/rack_app/config.ru
|
252
254
|
- spec/integration/apps/rails_app/README.rdoc
|
253
255
|
- spec/integration/apps/rails_app/Rakefile
|
256
|
+
- spec/integration/apps/rails_app/app/assets/images/grumpy-cat.gif
|
254
257
|
- spec/integration/apps/rails_app/app/assets/javascripts/application.js
|
255
258
|
- spec/integration/apps/rails_app/app/assets/stylesheets/application.css
|
256
259
|
- spec/integration/apps/rails_app/app/controllers/application_controller.rb
|