~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/web2/dav/test/test_put.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
# Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
 
3
#
 
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:
 
10
 
11
# The above copyright notice and this permission notice shall be included in all
 
12
# copies or substantial portions of the Software.
 
13
 
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
 
20
# SOFTWARE.
 
21
#
 
22
# DRI: Wilfredo Sanchez, wsanchez@apple.com
 
23
##
 
24
 
 
25
import os
 
26
import filecmp
 
27
 
 
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
 
32
 
 
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
 
36
 
 
37
class PUT(twisted.web2.dav.test.util.TestCase):
 
38
    """
 
39
    PUT request
 
40
    """
 
41
    def test_PUT_simple(self):
 
42
        """
 
43
        PUT request
 
44
        """
 
45
        dst_path = os.path.join(self.docroot, "dst")
 
46
 
 
47
        def checkResult(response, path):
 
48
            response = IResponse(response)
 
49
 
 
50
            if response.code not in (
 
51
                responsecode.CREATED,
 
52
                responsecode.NO_CONTENT
 
53
            ):
 
54
                self.fail("PUT failed: %s" % (response.code,))
 
55
 
 
56
            if not os.path.isfile(dst_path):
 
57
                self.fail("PUT failed to create file %s." % (dst_path,))
 
58
 
 
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))
 
61
 
 
62
            etag = response.headers.getHeader("etag")
 
63
            if not etag:
 
64
                self.fail("No etag header in PUT response %r." % (response,))
 
65
 
 
66
        #
 
67
        # We need to serialize these request & test iterations because they can
 
68
        # interfere with each other.
 
69
        #
 
70
        def work():
 
71
            dst_uri = "/dst"
 
72
 
 
73
            for name in os.listdir(self.docroot):
 
74
                if name == "dst":
 
75
                    continue
 
76
 
 
77
                path = os.path.join(self.docroot, name)
 
78
 
 
79
                # Can't really PUT something you can't read
 
80
                if not os.path.isfile(path): continue
 
81
    
 
82
                def do_test(response): checkResult(response, path)
 
83
    
 
84
                request = SimpleRequest(self.site, "PUT", dst_uri)
 
85
                request.stream = FileStream(file(path, "rb"))
 
86
    
 
87
                yield (request, do_test)
 
88
 
 
89
        return serialize(self.send, work())
 
90
 
 
91
    def test_PUT_again(self):
 
92
        """
 
93
        PUT on existing resource with If-None-Match header
 
94
        """
 
95
        dst_path = os.path.join(self.docroot, "dst")
 
96
        dst_uri = "/dst"
 
97
 
 
98
        def work():
 
99
            for code in (
 
100
                responsecode.CREATED,
 
101
                responsecode.PRECONDITION_FAILED,
 
102
                responsecode.NO_CONTENT,
 
103
                responsecode.PRECONDITION_FAILED,
 
104
                responsecode.NO_CONTENT,
 
105
                responsecode.CREATED,
 
106
            ):
 
107
                def checkResult(response, code=code):
 
108
                    response = IResponse(response)
 
109
 
 
110
                    if response.code != code:
 
111
                        self.fail("Incorrect response code for PUT (%s != %s)"
 
112
                                  % (response.code, code))
 
113
 
 
114
                def onError(f):
 
115
                    f.trap(HTTPError)
 
116
                    return checkResult(f.value.response)
 
117
 
 
118
                request = SimpleRequest(self.site, "PUT", dst_uri)
 
119
                request.stream = FileStream(file(__file__, "rb"))
 
120
    
 
121
                if code == responsecode.CREATED:
 
122
                    if os.path.isfile(dst_path):
 
123
                        os.remove(dst_path)
 
124
                    request.headers.setHeader("if-none-match", ("*",))
 
125
                elif code == responsecode.PRECONDITION_FAILED:
 
126
                    request.headers.setHeader("if-none-match", ("*",))
 
127
    
 
128
                yield (request, (checkResult, onError))
 
129
 
 
130
        return serialize(self.send, work())
 
131
 
 
132
    def test_PUT_no_parent(self):
 
133
        """
 
134
        PUT with no parent
 
135
        """
 
136
        dst_uri = "/put/no/parent"
 
137
 
 
138
        def checkResult(response):
 
139
            response = IResponse(response)
 
140
 
 
141
            if response.code != responsecode.CONFLICT:
 
142
                self.fail("Incorrect response code for PUT with no parent (%s != %s)"
 
143
                          % (response.code, responsecode.CONFLICT))
 
144
 
 
145
        request = SimpleRequest(self.site, "PUT", dst_uri)
 
146
        request.stream = FileStream(file(__file__, "rb"))
 
147
 
 
148
        return self.send(request, checkResult)