From 51eaedb1a9041bc3a9d79e465de5c5e6d391d81e Mon Sep 17 00:00:00 2001 From: nj Date: Fri, 11 Mar 2016 08:43:38 +0100 Subject: [PATCH] Implemented support for MKCOL method MKCOL - part of the HTTP Extensions for Distributed Authoring (also known as WebDAV) - allow creation of a new collection resource at the location specified. By extending with MKCOL method, will cover basic WebDAV implementation and make httparty more generic and increase the fun. --- lib/httparty.rb | 5 +++++ lib/httparty/request.rb | 3 ++- spec/httparty/request_spec.rb | 20 ++++++++++++++++++++ spec/httparty_spec.rb | 6 ++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index 2a9e8bc..6fa6087 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -538,6 +538,11 @@ module HTTParty perform_request Net::HTTP::Options, path, options, &block end + # Perform a MKCOL request to a path + def mkcol(path, options = {}, &block) + perform_request Net::HTTP::Mkcol, path, options, &block + end + attr_reader :default_options private diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 1e232f8..3fc5a80 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -9,7 +9,8 @@ module HTTParty Net::HTTP::Head, Net::HTTP::Options, Net::HTTP::Move, - Net::HTTP::Copy + Net::HTTP::Copy, + Net::HTTP::Mkcol ] SupportedURISchemes = ['http', 'https', 'webcal', nil] diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 969498b..6b041c3 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -594,6 +594,11 @@ RSpec.describe HTTParty::Request do expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) end + it "should be handled by MKCOL transparently" do + @request.http_method = Net::HTTP::Mkcol + expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) + end + it "should keep track of cookies between redirects" do @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly' @request.perform @@ -715,6 +720,11 @@ RSpec.describe HTTParty::Request do expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) end + it "should be handled by MKCOL transparently" do + @request.http_method = Net::HTTP::Mkcol + expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) + end + it "should keep track of cookies between redirects" do @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly' @request.perform @@ -848,6 +858,11 @@ RSpec.describe HTTParty::Request do expect(@request.perform.code).to eq(304) end + it "should report 304 with a MKCOL request" do + @request.http_method = Net::HTTP::Mkcol + expect(@request.perform.code).to eq(304) + end + it 'should not log the redirection' do logger_double = double expect(logger_double).to receive(:info).once @@ -914,6 +929,11 @@ RSpec.describe HTTParty::Request do expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) end + it "should be handled by MKCOL transparently" do + @request.http_method = Net::HTTP::Mkcol + expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}}) + end + it "should keep track of cookies between redirects" do @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly' @request.perform diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index ed38509..38aea70 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -602,6 +602,12 @@ RSpec.describe HTTParty do @klass.options('/foo', no_follow: true) end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')} end + + it "should fail with redirected MKCOL" do + expect do + @klass.mkcol('/foo', no_follow: true) + end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')} + end end describe "head requests should follow redirects requesting HEAD only" do