implementation of the webdav plugin

This commit is contained in:
HoneyryderChuck 2022-08-10 22:59:06 +01:00
parent f8f62b6710
commit eb3d3f9048
6 changed files with 195 additions and 4 deletions

View File

@ -24,6 +24,7 @@ services:
- AWS_ACCESS_KEY_ID=test
- AWS_SECRET_ACCESS_KEY=test
- AMZ_HOST=aws:4566
- WEBDAV_HOST=webdav
image: ruby:alpine
privileged: true
depends_on:
@ -34,6 +35,7 @@ services:
- nghttp2
- aws
- ws-echo-server
- webdav
volumes:
- ./:/home
links:
@ -137,3 +139,10 @@ services:
ports:
- 8083:80
image: jmalloc/echo-server
webdav:
image: bytemark/webdav
environment:
- AUTH_TYPE=Basic
- USERNAME=user
- PASSWORD=pass

View File

@ -143,10 +143,6 @@ module HTTPX
end
module RegexpExtensions
# If you wonder why this is there: the oauth feature uses a refinement to enhance the
# Regexp class locally with #match? , but this is never tested, because ActiveSupport
# monkey-patches the same method... Please ActiveSupport, stop being so intrusive!
# :nocov:
refine(Regexp) do
def match?(*args)
!match(*args).nil?

View File

@ -0,0 +1,78 @@
# frozen_string_literal: true
module HTTPX
module Plugins
#
# This plugin implements convenience methods for performing WEBDAV requests.
#
# https://gitlab.com/honeyryderchuck/httpx/wikis/WEBDAV
#
module WebDav
module InstanceMethods
def copy(src, dest)
request(:copy, src, headers: { "destination" => @options.origin.merge(dest) })
end
def move(src, dest)
request(:move, src, headers: { "destination" => @options.origin.merge(dest) })
end
def lock(path, timeout: nil, &blk)
headers = {}
headers["timeout"] = if timeout && timeout.positive?
"Second-#{timeout}"
else
"Infinite, Second-4100000000"
end
xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" \
"<D:lockinfo xmlns:D=\"DAV:\">" \
"<D:lockscope><D:exclusive/></D:lockscope>" \
"<D:locktype><D:write/></D:locktype>" \
"<D:owner>null</D:owner>" \
"</D:lockinfo>"
response = request(:lock, path, headers: headers, xml: xml)
return response unless blk && response.status == 200
lock_token = response.headers["lock-token"]
begin
blk.call(response)
ensure
unlock(path, lock_token)
end
end
def unlock(path, lock_token)
request(:unlock, path, headers: { "lock-token" => lock_token })
end
def mkcol(dir)
request(:mkcol, dir)
end
def propfind(path, xml = nil)
body = case xml
when :acl
'<?xml version="1.0" encoding="utf-8" ?><D:propfind xmlns:D="DAV:"><D:prop><D:owner/>' \
"<D:supported-privilege-set/><D:current-user-privilege-set/><D:acl/></D:prop></D:propfind>"
when nil
'<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>'
else
xml
end
request(:propfind, path, headers: { "depth" => "1" }, xml: body)
end
def proppatch(path, xml)
body = "<?xml version=\"1.0\"?>" \
"<D:propertyupdate xmlns:D=\"DAV:\" xmlns:Z=\"http://ns.example.com/standards/z39.50/\">#{xml}</D:propertyupdate>"
request(:proppatch, path, xml: body)
end
# %i[ orderpatch acl report search]
end
end
register_plugin(:webdav, WebDav)
end
end

View File

@ -32,6 +32,7 @@ class HTTPTest < Minitest::Test
include Plugins::GRPC if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.3.0"
include Plugins::ResponseCache
include Plugins::CircuitBreaker
include Plugins::WebDav
def test_verbose_log
log = StringIO.new

View File

@ -33,6 +33,7 @@ class HTTPSTest < Minitest::Test
include Plugins::GRPC if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.3.0"
include Plugins::ResponseCache
include Plugins::CircuitBreaker
include Plugins::WebDav
def test_connection_coalescing
coalesced_origin = "https://#{ENV["HTTPBIN_COALESCING_HOST"]}"

View File

@ -0,0 +1,106 @@
# frozen_string_literal: true
module Requests
module Plugins
module WebDav
def test_plugin_webdav_mkcol
# put file
webdav_client.delete("/mkcol_dir_test/")
response = webdav_client.mkcol("/mkcol_dir_test/")
verify_status(response, 201)
end
def test_plugin_webdav_copy
# put file
webdav_client.delete("/copied_copy.html")
webdav_client.put("/copy.html", body: "<html></html>")
response = webdav_client.get("/copied_copy.html")
verify_status(response, 404)
response = webdav_client.copy("/copy.html", "/copied_copy.html")
verify_status(response, 201)
response = webdav_client.get("/copied_copy.html")
verify_status(response, 200)
response = webdav_client.get("/copy.html")
verify_status(response, 200)
end
def test_plugin_webdav_move
# put file
webdav_client.delete("/moved_move.html")
webdav_client.put("/move.html", body: "<html></html>")
response = webdav_client.get("/moved_move.html")
verify_status(response, 404)
response = webdav_client.move("/move.html", "/moved_move.html")
verify_status(response, 201)
response = webdav_client.get("/move.html")
verify_status(response, 404)
response = webdav_client.get("/moved_move.html")
verify_status(response, 200)
end
def test_plugin_webdav_lock
# put file
webdav_client.put("/lockfile.html", body: "bang")
response = webdav_client.lock("/lockfile.html")
verify_status(response, 200)
lock_token = response.headers["lock-token"]
response = webdav_client.delete("/lockfile.html")
verify_status(response, 423)
response = webdav_client.unlock("/lockfile.html", lock_token)
verify_status(response, 204)
response = webdav_client.delete("/lockfile.html")
verify_status(response, 204)
end
def test_plugin_webdav_lock_blk
# put file
webdav_client.put("/lockfileblk.html", body: "bang")
webdav_client.lock("/lockfileblk.html") do |response|
verify_status(response, 200)
response = webdav_client.delete("/lockfileblk.html")
verify_status(response, 423)
end
response = webdav_client.delete("/lockfileblk.html")
verify_status(response, 204)
end
def test_plugin_webdav_propfind_proppatch
# put file
webdav_client.put("/propfind.html", body: "bang")
response = webdav_client.propfind("/propfind.html")
verify_status(response, 207)
xml = "<D:set>" \
"<D:prop>" \
"<Z:Authors>" \
"<Z:Author>Jim Bean</Z:Author>" \
"</Z:Authors>" \
"</D:prop>" \
"</D:set>"
response = webdav_client.proppatch("/propfind.html", xml)
verify_status(response, 207)
response = webdav_client.propfind("/propfind.html")
verify_status(response, 207)
assert response.to_s.include?("Jim Bean")
end
private
def webdav_client
@webdav_client ||= HTTPX.plugin(:basic_authentication).plugin(:webdav, origin: start_webdav_server).basic_auth("user", "pass")
end
def start_webdav_server
origin = ENV.fetch("WEBDAV_HOST")
"http://#{origin}"
end
end
end
end