Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
2af8cd22
Kaydet (Commit)
2af8cd22
authored
Şub 01, 2017
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Imported specific functions in tests.utils_tests.test_html.
üst
597bfcbf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
37 deletions
+36
-37
test_html.py
tests/utils_tests/test_html.py
+36
-37
No files found.
tests/utils_tests/test_html.py
Dosyayı görüntüle @
2af8cd22
...
...
@@ -2,8 +2,12 @@ import os
from
datetime
import
datetime
from
django.test
import
SimpleTestCase
from
django.utils
import
html
,
safestring
from
django.utils.functional
import
lazystr
from
django.utils.html
import
(
conditional_escape
,
escape
,
escapejs
,
format_html
,
html_safe
,
linebreaks
,
smart_urlquote
,
strip_spaces_between_tags
,
strip_tags
,
)
from
django.utils.safestring
import
mark_safe
class
TestUtilsHtml
(
SimpleTestCase
):
...
...
@@ -18,7 +22,6 @@ class TestUtilsHtml(SimpleTestCase):
self
.
assertEqual
(
function
(
value
),
output
)
def
test_escape
(
self
):
f
=
html
.
escape
items
=
(
(
'&'
,
'&'
),
(
'<'
,
'<'
),
...
...
@@ -30,26 +33,26 @@ class TestUtilsHtml(SimpleTestCase):
patterns
=
(
"
%
s"
,
"asdf
%
sfdsa"
,
"
%
s1"
,
"1
%
sb"
)
for
value
,
output
in
items
:
for
pattern
in
patterns
:
self
.
check_output
(
f
,
pattern
%
value
,
pattern
%
output
)
self
.
check_output
(
f
,
lazystr
(
pattern
%
value
),
pattern
%
output
)
self
.
check_output
(
escape
,
pattern
%
value
,
pattern
%
output
)
self
.
check_output
(
escape
,
lazystr
(
pattern
%
value
),
pattern
%
output
)
# Check repeated values.
self
.
check_output
(
f
,
value
*
2
,
output
*
2
)
self
.
check_output
(
escape
,
value
*
2
,
output
*
2
)
# Verify it doesn't double replace &.
self
.
check_output
(
f
,
'<&'
,
'<&'
)
self
.
check_output
(
escape
,
'<&'
,
'<&'
)
def
test_format_html
(
self
):
self
.
assertEqual
(
html
.
format_html
(
"{} {} {third} {fourth}"
,
"< Dangerous >"
,
html
.
mark_safe
(
"<b>safe</b>"
),
third
=
"< dangerous again"
,
fourth
=
html
.
mark_safe
(
"<i>safe again</i>"
)
),
format_html
(
"{} {} {third} {fourth}"
,
"< Dangerous >"
,
mark_safe
(
"<b>safe</b>"
),
third
=
"< dangerous again"
,
fourth
=
mark_safe
(
"<i>safe again</i>"
),
),
"< Dangerous > <b>safe</b> < dangerous again <i>safe again</i>"
)
def
test_linebreaks
(
self
):
f
=
html
.
linebreaks
items
=
(
(
"para1
\n\n
para2
\r\r
para3"
,
"<p>para1</p>
\n\n
<p>para2</p>
\n\n
<p>para3</p>"
),
(
"para1
\n
sub1
\r
sub2
\n\n
para2"
,
"<p>para1<br />sub1<br />sub2</p>
\n\n
<p>para2</p>"
),
...
...
@@ -57,11 +60,10 @@ class TestUtilsHtml(SimpleTestCase):
(
"para1
\t
more
\n\n
para2"
,
"<p>para1
\t
more</p>
\n\n
<p>para2</p>"
),
)
for
value
,
output
in
items
:
self
.
check_output
(
f
,
value
,
output
)
self
.
check_output
(
f
,
lazystr
(
value
),
output
)
self
.
check_output
(
linebreaks
,
value
,
output
)
self
.
check_output
(
linebreaks
,
lazystr
(
value
),
output
)
def
test_strip_tags
(
self
):
f
=
html
.
strip_tags
items
=
(
(
'<p>See: 'é is an apostrophe followed by e acute</p>'
,
'See: 'é is an apostrophe followed by e acute'
),
...
...
@@ -83,14 +85,14 @@ class TestUtilsHtml(SimpleTestCase):
(
'&gotcha&#;<>'
,
'&gotcha&#;<>'
),
)
for
value
,
output
in
items
:
self
.
check_output
(
f
,
value
,
output
)
self
.
check_output
(
f
,
lazystr
(
value
),
output
)
self
.
check_output
(
strip_tags
,
value
,
output
)
self
.
check_output
(
strip_tags
,
lazystr
(
value
),
output
)
# Some convoluted syntax for which parsing may differ between python versions
output
=
html
.
strip_tags
(
'<sc<!-- -->ript>test<<!-- -->/script>'
)
output
=
strip_tags
(
'<sc<!-- -->ript>test<<!-- -->/script>'
)
self
.
assertNotIn
(
'<script>'
,
output
)
self
.
assertIn
(
'test'
,
output
)
output
=
html
.
strip_tags
(
'<script>alert()</script>&h'
)
output
=
strip_tags
(
'<script>alert()</script>&h'
)
self
.
assertNotIn
(
'<script>'
,
output
)
self
.
assertIn
(
'alert()'
,
output
)
...
...
@@ -100,19 +102,18 @@ class TestUtilsHtml(SimpleTestCase):
with
open
(
path
,
'r'
)
as
fp
:
content
=
fp
.
read
()
start
=
datetime
.
now
()
stripped
=
html
.
strip_tags
(
content
)
stripped
=
strip_tags
(
content
)
elapsed
=
datetime
.
now
()
-
start
self
.
assertEqual
(
elapsed
.
seconds
,
0
)
self
.
assertIn
(
"Please try again."
,
stripped
)
self
.
assertNotIn
(
'<'
,
stripped
)
def
test_strip_spaces_between_tags
(
self
):
f
=
html
.
strip_spaces_between_tags
# Strings that should come out untouched.
items
=
(
' <adf>'
,
'<adf> '
,
' </adf> '
,
' <f> x</f>'
)
for
value
in
items
:
self
.
check_output
(
f
,
value
)
self
.
check_output
(
f
,
lazystr
(
value
))
self
.
check_output
(
strip_spaces_between_tags
,
value
)
self
.
check_output
(
strip_spaces_between_tags
,
lazystr
(
value
))
# Strings that have spaces to strip.
items
=
(
(
'<d> </d>'
,
'<d></d>'
),
...
...
@@ -120,11 +121,10 @@ class TestUtilsHtml(SimpleTestCase):
(
'
\n
<p>
\t
</p>
\n
<p> </p>
\n
'
,
'
\n
<p></p><p></p>
\n
'
),
)
for
value
,
output
in
items
:
self
.
check_output
(
f
,
value
,
output
)
self
.
check_output
(
f
,
lazystr
(
value
),
output
)
self
.
check_output
(
strip_spaces_between_tags
,
value
,
output
)
self
.
check_output
(
strip_spaces_between_tags
,
lazystr
(
value
),
output
)
def
test_escapejs
(
self
):
f
=
html
.
escapejs
items
=
(
(
'"double quotes" and
\'
single quotes
\'
'
,
'
\\
u0022double quotes
\\
u0022 and
\\
u0027single quotes
\\
u0027'
),
(
r'\ : backslashes, too'
,
'
\\
u005C : backslashes, too'
),
...
...
@@ -139,11 +139,11 @@ class TestUtilsHtml(SimpleTestCase):
),
)
for
value
,
output
in
items
:
self
.
check_output
(
f
,
value
,
output
)
self
.
check_output
(
f
,
lazystr
(
value
),
output
)
self
.
check_output
(
escapejs
,
value
,
output
)
self
.
check_output
(
escapejs
,
lazystr
(
value
),
output
)
def
test_smart_urlquote
(
self
):
quote
=
html
.
smart_urlquote
quote
=
smart_urlquote
# IDNs are properly quoted
self
.
assertEqual
(
quote
(
'http://öäü.com/'
),
'http://xn--4ca9at.com/'
)
self
.
assertEqual
(
quote
(
'http://öäü.com/öäü/'
),
'http://xn--4ca9at.com/
%
C3
%
B6
%
C3
%
A4
%
C3
%
BC/'
)
...
...
@@ -159,12 +159,11 @@ class TestUtilsHtml(SimpleTestCase):
def
test_conditional_escape
(
self
):
s
=
'<h1>interop</h1>'
self
.
assertEqual
(
html
.
conditional_escape
(
s
),
'<h1>interop</h1>'
)
self
.
assertEqual
(
html
.
conditional_escape
(
safestring
.
mark_safe
(
s
)),
s
)
self
.
assertEqual
(
conditional_escape
(
s
),
'<h1>interop</h1>'
)
self
.
assertEqual
(
conditional_escape
(
mark_safe
(
s
)),
s
)
def
test_html_safe
(
self
):
@html
.html
_safe
@html_safe
class
HtmlClass
:
def
__str__
(
self
):
return
"<h1>I'm a html class!</h1>"
...
...
@@ -183,7 +182,7 @@ class TestUtilsHtml(SimpleTestCase):
def
__str__
(
self
):
return
'some non html content'
@html
.html
_safe
@html_safe
class
Subclass
(
BaseClass
):
def
__str__
(
self
):
# overrides __str__ and is marked as html_safe
...
...
@@ -195,7 +194,7 @@ class TestUtilsHtml(SimpleTestCase):
def
test_html_safe_defines_html_error
(
self
):
msg
=
"can't apply @html_safe to HtmlClass because it defines __html__()."
with
self
.
assertRaisesMessage
(
ValueError
,
msg
):
@html
.html
_safe
@html_safe
class
HtmlClass
:
def
__html__
(
self
):
return
"<h1>I'm a html class!</h1>"
...
...
@@ -203,6 +202,6 @@ class TestUtilsHtml(SimpleTestCase):
def
test_html_safe_doesnt_define_str
(
self
):
msg
=
"can't apply @html_safe to HtmlClass because it doesn't define __str__()."
with
self
.
assertRaisesMessage
(
ValueError
,
msg
):
@html
.html
_safe
@html_safe
class
HtmlClass
:
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment