Esempio n. 1
0
    def test_roundtrip(self):
        from ClientCookie._HeadersUtil import split_header_words, join_header_words

        tests = [
            ("foo", "foo"),
            ("foo=bar", "foo=bar"),
            ("   foo   ", "foo"),
            ("foo=", 'foo=""'),
            ("foo=bar bar=baz", "foo=bar; bar=baz"),
            ("foo=bar;bar=baz", "foo=bar; bar=baz"),
            ("foo bar baz", "foo; bar; baz"),
            (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
            ("foo,,,bar", "foo, bar"),
            ("foo=bar,bar=baz", "foo=bar, bar=baz"),
            ("text/html; charset=iso-8859-1", 'text/html; charset="iso-8859-1"'),
            ('foo="bar"; port="80,81"; discard, bar=baz', 'foo=bar; port="80,81"; discard, bar=baz'),
            (r'Basic realm="\"foo\\\\bar\""', r'Basic; realm="\"foo\\\\bar\""'),
        ]

        for arg, expect in tests:
            input = split_header_words([arg])
            res = join_header_words(input)
            assert (
                res == expect
            ), """
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
Input was:    '%s'""" % (
                arg,
                expect,
                res,
                input,
            )
Esempio n. 2
0
    def test_split_header_words(self):
        from ClientCookie._HeadersUtil import split_header_words

        tests = [
            ("foo", [[("foo", None)]]),
            ("foo=bar", [[("foo", "bar")]]),
            ("   foo   ", [[("foo", None)]]),
            ("   foo=   ", [[("foo", "")]]),
            ("   foo=", [[("foo", "")]]),
            ("   foo=   ; ", [[("foo", "")]]),
            ("   foo=   ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
            ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
            # doesn't really matter if this next fails, but it works ATM
            ("foo= bar=baz", [[("foo", "bar=baz")]]),
            ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
            ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
            ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
            (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
             [[("foo", None), ("bar", "baz")],
              [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
            ]

        for arg, expect in tests:
            try:
                result = split_header_words([arg])
            except:
                import traceback, StringIO
                f = StringIO.StringIO()
                traceback.print_exc(None, f)
                result = "(error -- traceback follows)\n\n%s" % f.getvalue()
            assert result == expect, """
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
""" % (arg, expect, result)
Esempio n. 3
0
    def test_roundtrip(self):
        from ClientCookie._HeadersUtil import split_header_words, join_header_words

        tests = [("foo", "foo"), ("foo=bar", "foo=bar"), ("   foo   ", "foo"),
                 ("foo=", 'foo=""'), ("foo=bar bar=baz", "foo=bar; bar=baz"),
                 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
                 ('foo bar baz', "foo; bar; baz"),
                 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
                 ('foo,,,bar', 'foo, bar'),
                 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
                 ('text/html; charset=iso-8859-1',
                  'text/html; charset="iso-8859-1"'),
                 ('foo="bar"; port="80,81"; discard, bar=baz',
                  'foo=bar; port="80,81"; discard, bar=baz'),
                 (r'Basic realm="\"foo\\\\bar\""',
                  r'Basic; realm="\"foo\\\\bar\""')]

        for arg, expect in tests:
            input = split_header_words([arg])
            res = join_header_words(input)
            assert res == expect, """
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
Input was:    '%s'""" % (arg, expect, res, input)
Esempio n. 4
0
 def _encoding(self, response):
     # HTTPEquivProcessor may be in use, so both HTTP and HTTP-EQUIV
     # headers may be in the response.  HTTP-EQUIV headers come last,
     # so try in order from first to last.
     for ct in response.info().getheaders("content-type"):
         for k, v in split_header_words([ct])[0]:
             if k == "charset":
                 return v
     return self.default_encoding
Esempio n. 5
0
    def test_split_header_words(self):
        from ClientCookie._HeadersUtil import split_header_words

        tests = [
            ("foo", [[("foo", None)]]),
            ("foo=bar", [[("foo", "bar")]]),
            ("   foo   ", [[("foo", None)]]),
            ("   foo=   ", [[("foo", "")]]),
            ("   foo=", [[("foo", "")]]),
            ("   foo=   ; ", [[("foo", "")]]),
            ("   foo=   ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
            ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
            # doesn't really matter if this next fails, but it works ATM
            ("foo= bar=baz", [[("foo", "bar=baz")]]),
            ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
            ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
            ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
            (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ', [[("foo", None),
                                                            ("bar", "baz")],
                                                           [("spam", "")],
                                                           [("foo", ',;"')],
                                                           [("bar", "")]]),
        ]

        for arg, expect in tests:
            try:
                result = split_header_words([arg])
            except:
                import traceback, StringIO
                f = StringIO.StringIO()
                traceback.print_exc(None, f)
                result = "(error -- traceback follows)\n\n%s" % f.getvalue()
            assert result == expect, """
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
""" % (arg, expect, result)