Skip to content

Commit

Permalink
Merge pull request #79 from tenyo/add_http_patch
Browse files Browse the repository at this point in the history
Add PATCH HTTP method
  • Loading branch information
kenichi authored Nov 10, 2017
2 parents 4684a1e + b356c94 commit 60e3eb4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
5 changes: 3 additions & 2 deletions lib/angelo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
module Angelo

GET = 'GET'
PATCH = 'PATCH'
POST = 'POST'
PUT = 'PUT'
DELETE = 'DELETE'
OPTIONS = 'OPTIONS'

HTTPABLE = [:get, :post, :put, :delete, :options]
HTTPABLE = [:get, :patch, :post, :put, :delete, :options]
STATICABLE = [:get, :head]
POST_OVERRIDABLE = [:put, :delete]
POST_OVERRIDABLE = [:patch, :put, :delete]

ACCEPT_REQUEST_HEADER_KEY = 'Accept'

Expand Down
2 changes: 1 addition & 1 deletion lib/angelo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def params
@params ||= case request.method
when GET, DELETE, OPTIONS
parse_query_string
when POST, PUT
when PATCH, POST, PUT
parse_query_string_and_post_body
end.merge mustermann.params(request.path)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/angelo/minitest/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def http_req method, path, params = {}, headers = {}
end
private :http_req

[:get, :post, :put, :delete, :options, :head].each do |m|
[:get, :patch, :post, :put, :delete, :options, :head].each do |m|
define_method m do |path, params = {}, headers = {}, &block|
hc_req m, path, params, headers, &block
end
Expand Down
39 changes: 20 additions & 19 deletions test/angelo/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@set_by_before = params
end

[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|
__send__ m, '/before' do
content_type :json
@set_by_before
Expand All @@ -26,7 +26,7 @@
get '/before', obj
last_response_must_be_json obj_s

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj
end
Expand All @@ -51,7 +51,7 @@
@bat = params[:bat] if @bar
end

[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|
__send__ m, '/before' do
content_type :json
{ foo: @foo, bar: @bar, bat: @bat }
Expand All @@ -65,7 +65,7 @@
get '/before', obj
last_response_must_be_json obj_s

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj
end
Expand Down Expand Up @@ -94,7 +94,7 @@
@id = params[:id].to_i
end

[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|

__send__ m, '/before' do
content_type :json
Expand Down Expand Up @@ -124,7 +124,7 @@
get '/before', obj
last_response_must_be_json obj_s.select {|k,v| k == 'foo'}

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj.select {|k,v| k == 'foo'}
end
Expand All @@ -136,23 +136,23 @@
get '/before_bar', obj
last_response_must_be_json obj_s.select {|k,v| ['foo','bar'].include? k}

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before_bar', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj.select {|k,v| ['foo','bar'].include? k}
end

get '/before_bat', obj
last_response_must_be_json obj_s.select {|k,v| ['foo','bat'].include? k}

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before_bat', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj.select {|k,v| ['foo','bat'].include? k}
end

end

it 'matches regexes' do
[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|
id = rand 1000
__send__ m, "/before/#{id}"
last_response_must_be_json({'id' => id})
Expand Down Expand Up @@ -188,8 +188,8 @@
end

it 'runs after filters after routes' do
a = %w[2 6 14 30 62]
b = [4, 12, 28, 60, 124]
a = %w[2 6 14 30 62 126]
b = [4, 12, 28, 60, 124, 252]

Angelo::HTTPABLE.each_with_index do |m,i|
__send__ m, '/after', obj
Expand Down Expand Up @@ -227,8 +227,8 @@
end

it 'runs after filters in order' do
a = %w[0 2 6 14 30]
b = [2, 6, 14, 30, 62]
a = %w[0 2 6 14 30 62]
b = [2, 6, 14, 30, 62, 126]

Angelo::HTTPABLE.each_with_index do |m,i|
__send__ m, '/after', obj
Expand Down Expand Up @@ -275,26 +275,27 @@

it 'runs default and specific after filters' do

a = %w[0 2 4 6 8]
b = [2, 4, 6, 8, 10]
a = %w[0 2 4 6 8 10]
b = [2, 4, 6, 8, 10, 12]

Angelo::HTTPABLE.each_with_index do |m,i|
__send__ m, '/after', obj
last_response_must_be_html a[i]
invoked.must_equal b[i]
end

c = %w[10 24 52 108 220]
d = [24, 52, 108, 220, 444]
c = %w[12 28 60 124 252 508]
d = [28, 60, 124, 252, 508, 1020]


Angelo::HTTPABLE.each_with_index do |m,i|
__send__ m, '/after_bar', obj
last_response_must_be_html c[i]
invoked.must_equal d[i]
end

e = %w[444 442 440 438 436]
f = [442, 440, 438, 436, 434]
e = %w[1020 1018 1016 1014 1012 1010]
f = [1018, 1016, 1014, 1012, 1010, 1008]

Angelo::HTTPABLE.each_with_index do |m,i|
__send__ m, '/after_bat', obj
Expand Down
14 changes: 7 additions & 7 deletions test/angelo/mustermann_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
params
end

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, pattern do
params
end
Expand All @@ -39,7 +39,7 @@
it 'overrides post body params' do
path = '/some/things/are_good'
headers = {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, path, {foo: 'other', bar: 'are_bad'}.to_json, headers
last_response_must_be_json mm_pattern.params(path)
end
Expand Down Expand Up @@ -101,7 +101,7 @@

content_type :json

[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|
__send__ m, '/before/:bar' do
{ bar: params[:bar], foo: params[:foo], foo_from_before: @foo }
end
Expand All @@ -110,7 +110,7 @@
end

it 'does not infect route block params with filter pattern params' do
[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|
__send__ m, '/before/hi'
last_response_must_be_json 'bar' => 'hi', 'foo' => nil, 'foo_from_before' => 'hi'
end
Expand All @@ -131,7 +131,7 @@
@bat = params[:bat] if @foo
end

[:get, :post, :put].each do |m|
[:get, :patch, :post, :put].each do |m|

__send__ m, '/before' do
content_type :json
Expand All @@ -156,15 +156,15 @@
get '/before_bar', obj
last_response_must_be_json obj_s

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before_bar', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj
end

get '/before_bat', obj
last_response_must_be_json obj_s

[:post, :put].each do |m|
[:patch, :post, :put].each do |m|
__send__ m, '/before_bat', obj.to_json, {Angelo::CONTENT_TYPE_HEADER_KEY => Angelo::JSON_TYPE}
last_response_must_be_json obj
end
Expand Down
4 changes: 2 additions & 2 deletions test/angelo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
end

it 'does not crash when receiving unknown http request type' do
r = HTTP.patch(url('/'))
r = HTTP.trace(url('/'))
assert @server.alive?
r.status.must_equal 404
end
Expand Down Expand Up @@ -350,7 +350,7 @@
last_response_must_be_json({})
end

(Angelo::HTTPABLE - [:post, :put]).each do |m|
(Angelo::HTTPABLE - [:patch, :post, :put]).each do |m|
it "returns a populated hash for #{m.to_s.upcase} requests" do
send m, '/json?foo=bar'
last_response_must_be_json('foo' => 'bar')
Expand Down

0 comments on commit 60e3eb4

Please sign in to comment.