summaryrefslogtreecommitdiff
path: root/tests/test_etag_nose.py
blob: 8a8cd5d2fcdd238ace3be0e3f9787cec1ead8001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from webob.etag import IfRange, ETagMatcher
from webob import Response


def test_if_range_None():
    ir = IfRange.parse(None)
    assert str(ir) == ""
    assert not ir
    assert Response() in ir
    assert Response(etag="foo") in ir
    assert Response(etag="foo GMT") in ir


def test_if_range_match_date():
    date = "Fri, 09 Nov 2001 01:08:47 GMT"
    ir = IfRange.parse(date)
    assert str(ir) == date
    assert Response() not in ir
    assert Response(etag="etag") not in ir
    assert Response(etag=date) not in ir
    assert Response(last_modified="Fri, 09 Nov 2001 01:00:00 GMT") in ir
    assert Response(last_modified="Fri, 10 Nov 2001 01:00:00 GMT") not in ir


def test_if_range_match_etag():
    ir = IfRange.parse("ETAG")
    assert str(ir) == '"ETAG"'
    assert Response() not in ir
    assert Response(etag="other") not in ir
    assert Response(etag="ETAG") in ir
    assert Response(etag='W/"ETAG"') not in ir


def test_if_range_match_etag_weak():
    ir = IfRange.parse('W/"ETAG"')
    assert str(ir) == ""
    assert Response(etag="ETAG") not in ir
    assert Response(etag='W/"ETAG"') not in ir


def test_if_range_repr():
    assert repr(IfRange.parse(None)) == "IfRange(<ETag *>)"
    assert str(IfRange.parse(None)) == ""


def test_resp_etag():
    def t(tag, res, raw, strong):
        assert Response(etag=tag).etag == res
        assert Response(etag=tag).headers.get("etag") == raw
        assert Response(etag=tag).etag_strong == strong

    t("foo", "foo", '"foo"', "foo")
    t('"foo"', "foo", '"foo"', "foo")
    t('a"b', 'a"b', '"a\\"b"', 'a"b')
    t('W/"foo"', "foo", 'W/"foo"', None)
    t('W/"a\\"b"', 'a"b', 'W/"a\\"b"', None)
    t(("foo", True), "foo", '"foo"', "foo")
    t(("foo", False), "foo", 'W/"foo"', None)
    t(('"foo"', True), '"foo"', r'"\"foo\""', '"foo"')
    t(('W/"foo"', True), 'W/"foo"', r'"W/\"foo\""', 'W/"foo"')
    t(('W/"foo"', False), 'W/"foo"', r'W/"W/\"foo\""', None)


def test_matcher():
    matcher = ETagMatcher(["ETAGS"])
    matcher = ETagMatcher(["ETAGS"])
    assert matcher.etags == ["ETAGS"]
    assert "ETAGS" in matcher
    assert "WEAK" not in matcher
    assert "BEER" not in matcher
    assert None not in matcher
    assert repr(matcher) == "<ETag ETAGS>"
    assert str(matcher) == '"ETAGS"'

    matcher2 = ETagMatcher(("ETAG1", "ETAG2"))
    assert repr(matcher2) == "<ETag ETAG1 or ETAG2>"