2
# Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
# of this software and associated documentation files (the "Software"), to deal
6
# in the Software without restriction, including without limitation the rights
7
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
# copies of the Software, and to permit persons to whom the Software is
9
# furnished to do so, subject to the following conditions:
11
# The above copyright notice and this permission notice shall be included in all
12
# copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
# DRI: Wilfredo Sanchez, wsanchez@apple.com
28
from twisted.web2 import responsecode
29
from twisted.web2.iweb import IResponse
30
from twisted.web2.stream import FileStream
31
from twisted.web2.http import HTTPError
33
import twisted.web2.dav.test.util
34
from twisted.web2.test.test_server import SimpleRequest
35
from twisted.web2.dav.test.util import serialize
37
class PUT(twisted.web2.dav.test.util.TestCase):
41
def test_PUT_simple(self):
45
dst_path = os.path.join(self.docroot, "dst")
47
def checkResult(response, path):
48
response = IResponse(response)
50
if response.code not in (
52
responsecode.NO_CONTENT
54
self.fail("PUT failed: %s" % (response.code,))
56
if not os.path.isfile(dst_path):
57
self.fail("PUT failed to create file %s." % (dst_path,))
59
if not filecmp.cmp(path, dst_path):
60
self.fail("PUT failed to preserve data for file %s in file %s." % (path, dst_path))
62
etag = response.headers.getHeader("etag")
64
self.fail("No etag header in PUT response %r." % (response,))
67
# We need to serialize these request & test iterations because they can
68
# interfere with each other.
73
for name in os.listdir(self.docroot):
77
path = os.path.join(self.docroot, name)
79
# Can't really PUT something you can't read
80
if not os.path.isfile(path): continue
82
def do_test(response): checkResult(response, path)
84
request = SimpleRequest(self.site, "PUT", dst_uri)
85
request.stream = FileStream(file(path, "rb"))
87
yield (request, do_test)
89
return serialize(self.send, work())
91
def test_PUT_again(self):
93
PUT on existing resource with If-None-Match header
95
dst_path = os.path.join(self.docroot, "dst")
100
responsecode.CREATED,
101
responsecode.PRECONDITION_FAILED,
102
responsecode.NO_CONTENT,
103
responsecode.PRECONDITION_FAILED,
104
responsecode.NO_CONTENT,
105
responsecode.CREATED,
107
def checkResult(response, code=code):
108
response = IResponse(response)
110
if response.code != code:
111
self.fail("Incorrect response code for PUT (%s != %s)"
112
% (response.code, code))
116
return checkResult(f.value.response)
118
request = SimpleRequest(self.site, "PUT", dst_uri)
119
request.stream = FileStream(file(__file__, "rb"))
121
if code == responsecode.CREATED:
122
if os.path.isfile(dst_path):
124
request.headers.setHeader("if-none-match", ("*",))
125
elif code == responsecode.PRECONDITION_FAILED:
126
request.headers.setHeader("if-none-match", ("*",))
128
yield (request, (checkResult, onError))
130
return serialize(self.send, work())
132
def test_PUT_no_parent(self):
136
dst_uri = "/put/no/parent"
138
def checkResult(response):
139
response = IResponse(response)
141
if response.code != responsecode.CONFLICT:
142
self.fail("Incorrect response code for PUT with no parent (%s != %s)"
143
% (response.code, responsecode.CONFLICT))
145
request = SimpleRequest(self.site, "PUT", dst_uri)
146
request.stream = FileStream(file(__file__, "rb"))
148
return self.send(request, checkResult)