Ejemplo n.º 1
0
 def testETag(self):
     table = (
         ('"xyzzy"', http_headers.ETag('xyzzy')),
         ('W/"xyzzy"', http_headers.ETag('xyzzy', weak=True)),
         ('""', http_headers.ETag('')),
     )
     self.runRoundtripTest("ETag", table)
Ejemplo n.º 2
0
 def testIfRange(self):
     table = (
         ('"xyzzy"', http_headers.ETag('xyzzy')),
         ('W/"xyzzy"', http_headers.ETag('xyzzy', weak=True)),
         ('W/"xyzzy"', http_headers.ETag('xyzzy', weak=True)),
         ("Sun, 09 Sep 2001 01:46:40 GMT", 1000000000),
     )
     self.runRoundtripTest("If-Range", table)
Ejemplo n.º 3
0
 def testIfMatch(self):
     table = (
         ('"xyzzy"', [http_headers.ETag('xyzzy')]),
         ('"xyzzy", "r2d2xxxx", "c3piozzzz"', [
             http_headers.ETag('xyzzy'),
             http_headers.ETag('r2d2xxxx'),
             http_headers.ETag('c3piozzzz')
         ]),
         ('*', ['*']),
     )
Ejemplo n.º 4
0
    def testIfMatch(self):
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        out_headers = http_headers.Headers()
        response = http.Response(responsecode.OK, out_headers, None)

        # Behavior with no ETag set, should be same as with an ETag
        request.headers.setRawHeaders("If-Match", ('*', ))
        self.checkPreconditions(request, response, True, responsecode.OK)
        self.checkPreconditions(request,
                                response,
                                False,
                                responsecode.PRECONDITION_FAILED,
                                entityExists=False)

        # Ask for tag, but no etag set.
        request.headers.setRawHeaders("If-Match", ('"frob"', ))
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)

        ## Actually set the ETag header
        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        out_headers.setHeader("Last-Modified",
                              946771200)  # Sun, 02 Jan 2000 00:00:00 GMT

        # behavior of entityExists
        request.headers.setRawHeaders("If-Match", ('*', ))
        self.checkPreconditions(request, response, True, responsecode.OK)
        self.checkPreconditions(request,
                                response,
                                False,
                                responsecode.PRECONDITION_FAILED,
                                entityExists=False)

        # tag matches
        request.headers.setRawHeaders("If-Match", ('"frob", "foo"', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        # none match
        request.headers.setRawHeaders("If-Match", ('"baz", "bob"', ))
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)

        # But if we have an error code already, ignore this header
        response.code = responsecode.INTERNAL_SERVER_ERROR
        self.checkPreconditions(request, response, True,
                                responsecode.INTERNAL_SERVER_ERROR)
        response.code = responsecode.OK

        # Must only compare strong tags
        out_headers.setHeader("ETag", http_headers.ETag('foo', weak=True))
        request.headers.setRawHeaders("If-Match", ('W/"foo"', ))
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)
Ejemplo n.º 5
0
    def testIfRange(self):
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        response = TestResponse()

        self.assertEquals(http.checkIfRange(request, response), True)

        request.headers.setRawHeaders("If-Range", ('"foo"', ))
        self.assertEquals(http.checkIfRange(request, response), False)

        response.headers.setHeader("ETag", http_headers.ETag('foo'))
        self.assertEquals(http.checkIfRange(request, response), True)

        request.headers.setRawHeaders("If-Range", ('"bar"', ))
        response.headers.setHeader("ETag", http_headers.ETag('foo'))
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range", ('W/"foo"', ))
        response.headers.setHeader("ETag", http_headers.ETag('foo', weak=True))
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range", ('"foo"', ))
        response.headers.removeHeader("ETag")
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range",
                                      ('Sun, 02 Jan 2000 00:00:00 GMT', ))
        response.headers.setHeader("Last-Modified",
                                   946771200)  # Sun, 02 Jan 2000 00:00:00 GMT
        self.assertEquals(http.checkIfRange(request, response), True)

        request.headers.setRawHeaders("If-Range",
                                      ('Sun, 02 Jan 2000 00:00:01 GMT', ))
        response.headers.setHeader("Last-Modified",
                                   946771200)  # Sun, 02 Jan 2000 00:00:00 GMT
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range",
                                      ('Sun, 01 Jan 2000 23:59:59 GMT', ))
        response.headers.setHeader("Last-Modified",
                                   946771200)  # Sun, 02 Jan 2000 00:00:00 GMT
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range",
                                      ('Sun, 01 Jan 2000 23:59:59 GMT', ))
        response.headers.removeHeader("Last-Modified")
        self.assertEquals(http.checkIfRange(request, response), False)

        request.headers.setRawHeaders("If-Range", ('jwerlqjL#$Y*KJAN', ))
        self.assertEquals(http.checkIfRange(request, response), False)
Ejemplo n.º 6
0
    def testWithoutHeaders(self):
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        out_headers = http_headers.Headers()
        response = http.Response(responsecode.OK, out_headers, None)

        self.checkPreconditions(request, response, True, responsecode.OK)

        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        self.checkPreconditions(request, response, True, responsecode.OK)

        out_headers.removeHeader("ETag")
        out_headers.setHeader("Last-Modified",
                              946771200)  # Sun, 02 Jan 2000 00:00:00 GMT
        self.checkPreconditions(request, response, True, responsecode.OK)

        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        self.checkPreconditions(request, response, True, responsecode.OK)
Ejemplo n.º 7
0
    def testNoResponse(self):
        # Ensure that passing etag/lastModified arguments instead of response works.
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        request.method = "PUT"
        request.headers.setRawHeaders("If-None-Match", ('"foo"', ))

        self.checkPreconditions(request, None, True, responsecode.OK)
        self.checkPreconditions(request,
                                None,
                                False,
                                responsecode.PRECONDITION_FAILED,
                                etag=http_headers.ETag('foo'),
                                lastModified=946771200)

        # Make sure that, while you shoudn't do this, that it doesn't cause an error
        request.method = "GET"
        self.checkPreconditions(request,
                                None,
                                False,
                                responsecode.NOT_MODIFIED,
                                etag=http_headers.ETag('foo'))
Ejemplo n.º 8
0
    def etag(self):
        if not self.fp.exists(): return None

        st = self.fp.statinfo

        #
        # Mark ETag as weak if it was modified more recently than we can
        # measure and report, as it could be modified again in that span
        # and we then wouldn't know to provide a new ETag.
        #
        weak = (time.time() - st.st_mtime <= 1)

        return http_headers.ETag("%X-%X-%X" %
                                 (st.st_ino, st.st_size, st.st_mtime),
                                 weak=weak)
Ejemplo n.º 9
0
    def testIfModifiedSince(self):
        if time.time() < 946771200:
            raise "Your computer's clock is way wrong, this test will be invalid."

        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        out_headers = http_headers.Headers()
        response = http.Response(responsecode.OK, out_headers, None)

        # No Last-Modified => always succeed
        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        # Set output headers
        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        out_headers.setHeader("Last-Modified",
                              946771200)  # Sun, 02 Jan 2000 00:00:00 GMT

        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, False,
                                responsecode.NOT_MODIFIED)

        # With a non-GET method
        request.method = "PUT"
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)
        request.method = "GET"

        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Sat, 01 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        # But if we have an error code already, ignore this header
        response.code = responsecode.INTERNAL_SERVER_ERROR
        self.checkPreconditions(request, response, True,
                                responsecode.INTERNAL_SERVER_ERROR)
        response.code = responsecode.OK

        # invalid date => header ignored
        request.headers.setRawHeaders("If-Modified-Since",
                                      ('alalalalalalalalalala', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        # date in the future => assume modified
        request.headers.setHeader("If-Modified-Since", time.time() + 500)
        self.checkPreconditions(request, response, True, responsecode.OK)
Ejemplo n.º 10
0
 def testIfNoneMatch(self):
     table = (
         ('"xyzzy"', [http_headers.ETag('xyzzy')]),
         ('W/"xyzzy", "r2d2xxxx", "c3piozzzz"', [
             http_headers.ETag('xyzzy', weak=True),
             http_headers.ETag('r2d2xxxx'),
             http_headers.ETag('c3piozzzz')
         ]),
         ('W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"', [
             http_headers.ETag('xyzzy', weak=True),
             http_headers.ETag('r2d2xxxx', weak=True),
             http_headers.ETag('c3piozzzz', weak=True)
         ]),
         ('*', ['*']),
     )
     self.runRoundtripTest("If-None-Match", table)
Ejemplo n.º 11
0
    def testIfUnmodifiedSince(self):
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        out_headers = http_headers.Headers()
        response = http.Response(responsecode.OK, out_headers, None)

        # No Last-Modified => always fail.
        request.headers.setRawHeaders("If-Unmodified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)

        # Set output headers
        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        out_headers.setHeader("Last-Modified",
                              946771200)  # Sun, 02 Jan 2000 00:00:00 GMT

        request.headers.setRawHeaders("If-Unmodified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        request.headers.setRawHeaders("If-Unmodified-Since",
                                      ('Sat, 01 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)

        # But if we have an error code already, ignore this header
        response.code = responsecode.INTERNAL_SERVER_ERROR
        self.checkPreconditions(request, response, True,
                                responsecode.INTERNAL_SERVER_ERROR)
        response.code = responsecode.OK

        # invalid date => header ignored
        request.headers.setRawHeaders("If-Unmodified-Since",
                                      ('alalalalalalalalalala', ))
        self.checkPreconditions(request, response, True, responsecode.OK)
Ejemplo n.º 12
0
    def testIfNoneMatch(self):
        request = http.Request(None, "GET", "/", "HTTP/1.1", 0,
                               http_headers.Headers())
        out_headers = http_headers.Headers()
        response = http.Response(responsecode.OK, out_headers, None)

        request.headers.setRawHeaders("If-None-Match", ('"foo"', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        out_headers.setHeader("ETag", http_headers.ETag('foo'))
        out_headers.setHeader("Last-Modified",
                              946771200)  # Sun, 02 Jan 2000 00:00:00 GMT

        # behavior of entityExists
        request.headers.setRawHeaders("If-None-Match", ('*', ))
        request.method = "PUT"
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)
        request.method = "GET"
        self.checkPreconditions(request, response, False,
                                responsecode.NOT_MODIFIED)
        self.checkPreconditions(request,
                                response,
                                True,
                                responsecode.OK,
                                entityExists=False)

        # tag matches
        request.headers.setRawHeaders("If-None-Match", ('"frob", "foo"', ))
        request.method = "PUT"
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)
        request.method = "GET"
        self.checkPreconditions(request, response, False,
                                responsecode.NOT_MODIFIED)

        # now with IMS, also:
        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        request.method = "PUT"
        self.checkPreconditions(request, response, False,
                                responsecode.PRECONDITION_FAILED)
        request.method = "GET"
        self.checkPreconditions(request, response, False,
                                responsecode.NOT_MODIFIED)

        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Sat, 01 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        request.headers.removeHeader("If-Modified-Since")

        # none match
        request.headers.setRawHeaders("If-None-Match", ('"baz", "bob"', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        # now with IMS, also:
        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Mon, 03 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        request.headers.setRawHeaders("If-Modified-Since",
                                      ('Sat, 01 Jan 2000 00:00:00 GMT', ))
        self.checkPreconditions(request, response, True, responsecode.OK)

        request.headers.removeHeader("If-Modified-Since")

        # But if we have an error code already, ignore this header
        response.code = responsecode.INTERNAL_SERVER_ERROR
        self.checkPreconditions(request, response, True,
                                responsecode.INTERNAL_SERVER_ERROR)
        response.code = responsecode.OK

        # Weak tags okay for GET
        out_headers.setHeader("ETag", http_headers.ETag('foo', weak=True))
        request.headers.setRawHeaders("If-None-Match", ('W/"foo"', ))
        self.checkPreconditions(request, response, False,
                                responsecode.NOT_MODIFIED)

        # Weak tags not okay for other methods
        request.method = "PUT"
        out_headers.setHeader("ETag", http_headers.ETag('foo', weak=True))
        request.headers.setRawHeaders("If-None-Match", ('W/"foo"', ))
        self.checkPreconditions(request, response, True, responsecode.OK)
Ejemplo n.º 13
0
 def etag(self):
     lastModified = self.lastModified()
     return http_headers.ETag("%X-%X" % (lastModified, hash(self.data)),
                              weak=(time.time() - lastModified <= 1))