http_get: fix retries on broken connections
[omgdav.git] / test / test_proppatch.rb
blob47ae47c54eb18f0460b27bd84a749cb84b7f14ca
1 # -*- encoding: binary -*-
2 # Copyright (C) 2012-2017 all contributors <omgdav-public@bogomips.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 require './test/integration'
5 class TestProppatch < Minitest::Test
6   include TestMogileFSIntegration
8   def test_proppatch_live
9     assert_equal 201, req("MKCOL", "/prop")[0].to_i
11     t = Time.at(666)
12     xml = <<-EOF
13 <?xml version="1.0" encoding="utf-8"?>
14 <D:propertyupdate xmlns:D="DAV:">
15   <D:set>
16     <D:prop>
17       <getlastmodified xmlns="DAV:">#{t.httpdate}</getlastmodified>
18     </D:prop>
19   </D:set>
20 </D:propertyupdate>
21     EOF
22     opts = {
23       input: StringIO.new(xml.strip!)
24     }
26     assert_equal 200, req("PROPPATCH", "/prop", opts)[0].to_i
27     assert_equal 666, @db[:paths][name: "prop"][:mtime]
28   end
30   def test_getcontenttype
31     path = "//D:multistatus/D:response/D:propstat/D:prop/D:getcontenttype"
32     assert_equal 201, req("PUT", "/type")[0].to_i
33     _, _, body = req("PROPFIND", "/type", "HTTP_DEPTH" => '0')
34     t = Nokogiri::XML(body).search(path)
35     assert_equal "application/octet-stream", t.text
37     xml = <<-EOF
38 <?xml version="1.0" encoding="utf-8"?>
39 <D:propertyupdate xmlns:D="DAV:">
40   <D:set>
41     <D:prop>
42       <getcontenttype xmlns="DAV:">audio/flac</getcontenttype>
43     </D:prop>
44   </D:set>
45 </D:propertyupdate>
46     EOF
47     opts = {
48       input: StringIO.new(xml.strip!)
49     }
51     assert_equal 200, req("PROPPATCH", "/type", opts)[0].to_i
52     _, _, body = req("PROPFIND", "/type", "HTTP_DEPTH" => '0')
53     t = Nokogiri::XML(body).search(path)
54     assert_equal "audio/flac", t.text
56     xml = <<-EOF
57 <?xml version="1.0" encoding="utf-8" ?>
58 <D:propertyupdate xmlns:D="DAV:"
59        xmlns:Z="http://ns.example.com/standards/z39.50/">
60  <D:remove>
61    <D:prop><D:getcontenttype/></D:prop>
62  </D:remove>
63 </D:propertyupdate>
64     EOF
65     opts = {
66       input: StringIO.new(xml.strip!)
67     }
68     assert_equal 200, req("PROPPATCH", "/type", opts)[0].to_i
69     _, _, body = req("PROPFIND", "/type", "HTTP_DEPTH" => '0')
70     t = Nokogiri::XML(body).search(path)
71     assert_equal "application/octet-stream", t.text
73     opts = {
74       "HTTP_DESTINATION" => "http://example.com/type.txt",
75       "HTTP_HOST" => "example.com",
76     }
77     assert_equal 201, req("MOVE", "/type", opts)[0].to_i
78     _, _, body = req("PROPFIND", "/type.txt", "HTTP_DEPTH" => '0')
79     t = Nokogiri::XML(body).search(path)
80     assert_equal "text/plain", t.text
82     # try something invalid
83     [
84       "audio flac",
85       "foo/bar adlkj",
86       "foo/bar paramet()=ff"
87     ].each do |bad|
88       xml = <<-EOF
89   <?xml version="1.0" encoding="utf-8"?>
90   <D:propertyupdate xmlns:D="DAV:">
91     <D:set>
92       <D:prop>
93         <getcontenttype xmlns="DAV:">#{bad}</getcontenttype>
94       </D:prop>
95     </D:set>
96   </D:propertyupdate>
97       EOF
98       opts = { input: StringIO.new(xml.strip!) }
99       assert_equal 400, req("PROPPATCH", "/type.txt", opts)[0].to_i, bad
100       assert_nil @db[:paths][name: "type.txt"][:contenttype]
101     end
103     xml = <<-EOF
104 <?xml version="1.0" encoding="utf-8"?>
105 <D:propertyupdate xmlns:D="DAV:">
106   <D:set>
107     <D:prop>
108       <getcontenttype xmlns="DAV:">teXt/plain;  chArset="ASCII"</getcontenttype>
109     </D:prop>
110   </D:set>
111 </D:propertyupdate>
112     EOF
113     opts = { input: StringIO.new(xml.strip!) }
114     assert_equal 200, req("PROPPATCH", "/type.txt", opts)[0].to_i
115     i = @db[:paths][name: "type.txt"][:contenttype]
116     assert_kind_of Integer, i
117     t = @db[:prop_mappings][id: i][:value]
118     assert_equal 'text/plain; charset="ASCII"', t
119     refute_equal i, @db[:prop_mappings][value: 'audio/flac'][:id]
120   end
122   def test_proppatch_dead
123     assert_equal 201, req("MKCOL", "/prop")[0].to_i
125     xml = <<-EOF
126 <?xml version="1.0" encoding="utf-8" ?>
127 <D:propertyupdate xmlns:D="DAV:"
128        xmlns:Z="http://ns.example.com/standards/z39.50/">
129  <D:set>
130    <D:prop>
131      <Z:Authors>
132        <Z:Author>Jim Whitehead</Z:Author>
133        <Z:Author>Roy Fielding</Z:Author>
134      </Z:Authors>
135    </D:prop>
136  </D:set>
137  <D:remove>
138    <D:prop><Z:Copyright-Owner/></D:prop>
139  </D:remove>
140 </D:propertyupdate>
141     EOF
142     opts = {
143       input: StringIO.new(xml.strip!)
144     }
146     assert_equal 200, req("PROPPATCH", "/prop", opts)[0].to_i
147     prop = @db[:paths][name: "prop"]
148     props = @app.dead_props_get(prop)
149     ns_prop = props["http://ns.example.com/standards/z39.50/"]
150     assert_instance_of Hash, ns_prop
151     authors = ns_prop["Authors"].strip
152     expect = "<Author>Jim Whitehead</Author>\n" \
153              "       <Author>Roy Fielding</Author>"
154     assert_equal expect, authors
155   end