diff options
author | Marc Abramowitz <marc@marc-abramowitz.com> | 2015-04-30 17:39:24 -0700 |
---|---|---|
committer | Marc Abramowitz <marc@marc-abramowitz.com> | 2015-04-30 17:39:24 -0700 |
commit | fa100c92c06d3a8a61a0dda1a2e06018437b09c6 (patch) | |
tree | a1cc50f93fbf257685c3849e03496c5e33949281 /tests/test_template.txt | |
download | paste-git-fa100c92c06d3a8a61a0dda1a2e06018437b09c6.tar.gz |
test_wsgirequest_charset: Use UTF-8 instead of iso-8859-1test_wsgirequest_charset_use_UTF-8_instead_of_iso-8859-1
because it seems that the defacto standard for encoding URIs is to use UTF-8.
I've been reading about url encoding and it seems like perhaps using an
encoding other than UTF-8 is very non-standard and not well-supported (this
test is trying to use `iso-8859-1`).
From http://en.wikipedia.org/wiki/Percent-encoding
> For a non-ASCII character, it is typically converted to its byte sequence in
> UTF-8, and then each byte value is represented as above.
> The generic URI syntax mandates that new URI schemes that provide for the
> representation of character data in a URI must, in effect, represent
> characters from the unreserved set without translation, and should convert
> all other characters to bytes according to UTF-8, and then percent-encode
> those values. This requirement was introduced in January 2005 with the
> publication of RFC 3986
From http://tools.ietf.org/html/rfc3986:
> Non-ASCII characters must first be encoded according to UTF-8 [STD63], and
> then each octet of the corresponding UTF-8 sequence must be percent-encoded
> to be represented as URI characters. URI producing applications must not use
> percent-encoding in host unless it is used to represent a UTF-8 character
> sequence.
From http://tools.ietf.org/html/rfc3987:
> Conversions from URIs to IRIs MUST NOT use any character encoding other than
> UTF-8 in steps 3 and 4, even if it might be possible to guess from the
> context that another character encoding than UTF-8 was used in the URI. For
> example, the URI "http://www.example.org/r%E9sum%E9.html" might with some
> guessing be interpreted to contain two e-acute characters encoded as
> iso-8859-1. It must not be converted to an IRI containing these e-acute
> characters. Otherwise, in the future the IRI will be mapped to
> "http://www.example.org/r%C3%A9sum%C3%A9.html", which is a different URI from
> "http://www.example.org/r%E9sum%E9.html".
See issue #7, which I think this at least partially fixes.
Diffstat (limited to 'tests/test_template.txt')
-rw-r--r-- | tests/test_template.txt | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/test_template.txt b/tests/test_template.txt new file mode 100644 index 0000000..45a85e2 --- /dev/null +++ b/tests/test_template.txt @@ -0,0 +1,136 @@ +The templating language is fairly simple, just {{stuff}}. For +example:: + + >>> from paste.util.template import Template, sub + >>> sub('Hi {{name}}', name='Ian') + 'Hi Ian' + >>> Template('Hi {{repr(name)}}').substitute(name='Ian') + "Hi 'Ian'" + >>> Template('Hi {{name+1}}').substitute(name='Ian') + Traceback (most recent call last): + ... + TypeError: cannot concatenate 'str' and 'int' objects at line 1 column 6 + +It also has Django-style piping:: + + >>> sub('Hi {{name|repr}}', name='Ian') + "Hi 'Ian'" + +Note that None shows up as an empty string:: + + >>> sub('Hi {{name}}', name=None) + 'Hi ' + +And if/elif/else:: + + >>> t = Template('{{if x}}{{y}}{{else}}{{z}}{{endif}}') + >>> t.substitute(x=1, y=2, z=3) + '2' + >>> t.substitute(x=0, y=2, z=3) + '3' + >>> t = Template('{{if x > 0}}positive{{elif x < 0}}negative{{else}}zero{{endif}}') + >>> t.substitute(x=1), t.substitute(x=-10), t.substitute(x=0) + ('positive', 'negative', 'zero') + +Plus a for loop:: + + >>> t = Template('{{for i in x}}i={{i}}\n{{endfor}}') + >>> t.substitute(x=range(3)) + 'i=0\ni=1\ni=2\n' + >>> t = Template('{{for a, b in sorted(z.items()):}}{{a}}={{b}},{{endfor}}') + >>> t.substitute(z={1: 2, 3: 4}) + '1=2,3=4,' + >>> t = Template('{{for i in x}}{{if not i}}{{break}}' + ... '{{endif}}{{i}} {{endfor}}') + >>> t.substitute(x=[1, 2, 0, 3, 4]) + '1 2 ' + >>> t = Template('{{for i in x}}{{if not i}}{{continue}}' + ... '{{endif}}{{i}} {{endfor}}') + >>> t.substitute(x=[1, 2, 0, 3, 0, 4]) + '1 2 3 4 ' + +Also Python blocks:: + + >>> sub('{{py:\nx=1\n}}{{x}}') + '1' + +And some syntax errors:: + + >>> t = Template('{{if x}}', name='foo.html') + Traceback (most recent call last): + ... + TemplateError: No {{endif}} at line 1 column 3 in foo.html + >>> t = Template('{{for x}}', name='foo2.html') + Traceback (most recent call last): + ... + TemplateError: Bad for (no "in") in 'x' at line 1 column 3 in foo2.html + +There's also an HTMLTemplate that uses HTMLisms:: + + >>> from paste.util.template import HTMLTemplate, sub_html, html + >>> sub_html('hi {{name}}', name='<foo>') + 'hi <foo>' + +But if you don't want quoting to happen you can do:: + + >>> sub_html('hi {{name}}', name=html('<foo>')) + 'hi <foo>' + >>> sub_html('hi {{name|html}}', name='<foo>') + 'hi <foo>' + +Also a couple handy functions;: + + >>> t = HTMLTemplate('<a href="article?id={{id|url}}" {{attr(class_=class_)}}>') + >>> t.substitute(id=1, class_='foo') + '<a href="article?id=1" class="foo">' + >>> t.substitute(id='with space', class_=None) + '<a href="article?id=with%20space" >' + +There's a handyish looper thing you can also use in your templates (or +in Python, but it's more useful in templates generally):: + + >>> from paste.util.looper import looper + >>> seq = ['apple', 'asparagus', 'Banana', 'orange'] + >>> for loop, item in looper(seq): + ... if item == 'apple': + ... assert loop.first + ... elif item == 'orange': + ... assert loop.last + ... if loop.first_group(lambda i: i[0].upper()): + ... print '%s:' % item[0].upper() + ... print loop.number, item + A: + 1 apple + 2 asparagus + B: + 3 Banana + O: + 4 orange + +It will also strip out empty lines, when there is a line that only +contains a directive/statement (if/for, etc):: + + >>> sub('{{if 1}}\n{{x}}\n{{endif}}\n', x=0) + '0\n' + >>> sub('{{if 1}}x={{x}}\n{{endif}}\n', x=1) + 'x=1\n' + >>> sub('{{if 1}}\nx={{x}}\n{{endif}}\n', x=1) + 'x=1\n' + +Lastly, there is a special directive that will create a default value +for a variable, if no value is given:: + + >>> sub('{{default x=1}}{{x}}', x=2) + '2' + >>> sub('{{default x=1}}{{x}}') + '1' + >>> # The normal case: + >>> sub('{{x}}') + Traceback (most recent call last): + ... + NameError: name 'x' is not defined at line 1 column 3 + +And comments work:: + + >>> sub('Test=x{{#whatever}}') + 'Test=x' |