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
97fe70d3
Kaydet (Commit)
97fe70d3
authored
Agu 11, 2012
tarafından
Claude Paroz
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
[py3] Used BytesIO to test request streams
üst
87e0a75c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
28 deletions
+28
-28
tests.py
tests/regressiontests/requests/tests.py
+28
-28
No files found.
tests/regressiontests/requests/tests.py
Dosyayı görüntüle @
97fe70d3
...
...
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import
time
import
warnings
from
datetime
import
datetime
,
timedelta
from
io
import
BytesIO
from
django.conf
import
settings
from
django.core.handlers.wsgi
import
WSGIRequest
,
LimitedStream
...
...
@@ -10,17 +11,16 @@ from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_r
from
django.test.utils
import
str_prefix
from
django.utils
import
unittest
from
django.utils.http
import
cookie_date
from
django.utils.six
import
StringIO
from
django.utils.timezone
import
utc
class
RequestsTests
(
unittest
.
TestCase
):
def
test_httprequest
(
self
):
request
=
HttpRequest
()
self
.
assertEqual
(
request
.
GET
.
keys
(
),
[])
self
.
assertEqual
(
request
.
POST
.
keys
(
),
[])
self
.
assertEqual
(
request
.
COOKIES
.
keys
(
),
[])
self
.
assertEqual
(
request
.
META
.
keys
(
),
[])
self
.
assertEqual
(
list
(
request
.
GET
.
keys
()
),
[])
self
.
assertEqual
(
list
(
request
.
POST
.
keys
()
),
[])
self
.
assertEqual
(
list
(
request
.
COOKIES
.
keys
()
),
[])
self
.
assertEqual
(
list
(
request
.
META
.
keys
()
),
[])
def
test_httprequest_repr
(
self
):
request
=
HttpRequest
()
...
...
@@ -35,17 +35,17 @@ class RequestsTests(unittest.TestCase):
str_prefix
(
"<HttpRequest
\n
path:/otherpath/,
\n
GET:{
%(_)
s'a':
%(_)
s'b'},
\n
POST:{
%(_)
s'c':
%(_)
s'd'},
\n
COOKIES:{
%(_)
s'e':
%(_)
s'f'},
\n
META:{
%(_)
s'g':
%(_)
s'h'}>"
))
def
test_wsgirequest
(
self
):
request
=
WSGIRequest
({
'PATH_INFO'
:
'bogus'
,
'REQUEST_METHOD'
:
'bogus'
,
'wsgi.input'
:
StringIO
(
''
)})
self
.
assertEqual
(
request
.
GET
.
keys
(
),
[])
self
.
assertEqual
(
request
.
POST
.
keys
(
),
[])
self
.
assertEqual
(
request
.
COOKIES
.
keys
(
),
[])
request
=
WSGIRequest
({
'PATH_INFO'
:
'bogus'
,
'REQUEST_METHOD'
:
'bogus'
,
'wsgi.input'
:
BytesIO
(
b
''
)})
self
.
assertEqual
(
list
(
request
.
GET
.
keys
()
),
[])
self
.
assertEqual
(
list
(
request
.
POST
.
keys
()
),
[])
self
.
assertEqual
(
list
(
request
.
COOKIES
.
keys
()
),
[])
self
.
assertEqual
(
set
(
request
.
META
.
keys
()),
set
([
'PATH_INFO'
,
'REQUEST_METHOD'
,
'SCRIPT_NAME'
,
'wsgi.input'
]))
self
.
assertEqual
(
request
.
META
[
'PATH_INFO'
],
'bogus'
)
self
.
assertEqual
(
request
.
META
[
'REQUEST_METHOD'
],
'bogus'
)
self
.
assertEqual
(
request
.
META
[
'SCRIPT_NAME'
],
''
)
def
test_wsgirequest_repr
(
self
):
request
=
WSGIRequest
({
'PATH_INFO'
:
'/somepath/'
,
'REQUEST_METHOD'
:
'get'
,
'wsgi.input'
:
StringIO
(
''
)})
request
=
WSGIRequest
({
'PATH_INFO'
:
'/somepath/'
,
'REQUEST_METHOD'
:
'get'
,
'wsgi.input'
:
BytesIO
(
b
''
)})
request
.
GET
=
{
'get-key'
:
'get-value'
}
request
.
POST
=
{
'post-key'
:
'post-value'
}
request
.
COOKIES
=
{
'post-key'
:
'post-value'
}
...
...
@@ -207,26 +207,26 @@ class RequestsTests(unittest.TestCase):
def
test_limited_stream
(
self
):
# Read all of a limited stream
stream
=
LimitedStream
(
String
IO
(
b
'test'
),
2
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'test'
),
2
)
self
.
assertEqual
(
stream
.
read
(),
b
'te'
)
# Reading again returns nothing.
self
.
assertEqual
(
stream
.
read
(),
b
''
)
# Read a number of characters greater than the stream has to offer
stream
=
LimitedStream
(
String
IO
(
b
'test'
),
2
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'test'
),
2
)
self
.
assertEqual
(
stream
.
read
(
5
),
b
'te'
)
# Reading again returns nothing.
self
.
assertEqual
(
stream
.
readline
(
5
),
b
''
)
# Read sequentially from a stream
stream
=
LimitedStream
(
String
IO
(
b
'12345678'
),
8
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'12345678'
),
8
)
self
.
assertEqual
(
stream
.
read
(
5
),
b
'12345'
)
self
.
assertEqual
(
stream
.
read
(
5
),
b
'678'
)
# Reading again returns nothing.
self
.
assertEqual
(
stream
.
readline
(
5
),
b
''
)
# Read lines from a stream
stream
=
LimitedStream
(
String
IO
(
b
'1234
\n
5678
\n
abcd
\n
efgh
\n
ijkl'
),
24
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'1234
\n
5678
\n
abcd
\n
efgh
\n
ijkl'
),
24
)
# Read a full line, unconditionally
self
.
assertEqual
(
stream
.
readline
(),
b
'1234
\n
'
)
# Read a number of characters less than a line
...
...
@@ -246,7 +246,7 @@ class RequestsTests(unittest.TestCase):
# If a stream contains a newline, but the provided length
# is less than the number of provided characters, the newline
# doesn't reset the available character count
stream
=
LimitedStream
(
String
IO
(
b
'1234
\n
abcdef'
),
9
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'1234
\n
abcdef'
),
9
)
self
.
assertEqual
(
stream
.
readline
(
10
),
b
'1234
\n
'
)
self
.
assertEqual
(
stream
.
readline
(
3
),
b
'abc'
)
# Now expire the available characters
...
...
@@ -255,7 +255,7 @@ class RequestsTests(unittest.TestCase):
self
.
assertEqual
(
stream
.
readline
(
2
),
b
''
)
# Same test, but with read, not readline.
stream
=
LimitedStream
(
String
IO
(
b
'1234
\n
abcdef'
),
9
)
stream
=
LimitedStream
(
Bytes
IO
(
b
'1234
\n
abcdef'
),
9
)
self
.
assertEqual
(
stream
.
read
(
6
),
b
'1234
\n
a'
)
self
.
assertEqual
(
stream
.
read
(
2
),
b
'bc'
)
self
.
assertEqual
(
stream
.
read
(
2
),
b
'd'
)
...
...
@@ -266,7 +266,7 @@ class RequestsTests(unittest.TestCase):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
request
.
read
(),
b
'name=value'
)
def
test_read_after_value
(
self
):
...
...
@@ -277,7 +277,7 @@ class RequestsTests(unittest.TestCase):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
request
.
POST
,
{
'name'
:
[
'value'
]})
self
.
assertEqual
(
request
.
body
,
b
'name=value'
)
self
.
assertEqual
(
request
.
read
(),
b
'name=value'
)
...
...
@@ -290,7 +290,7 @@ class RequestsTests(unittest.TestCase):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
request
.
read
(
2
),
b
'na'
)
self
.
assertRaises
(
Exception
,
lambda
:
request
.
body
)
self
.
assertEqual
(
request
.
POST
,
{})
...
...
@@ -312,7 +312,7 @@ class RequestsTests(unittest.TestCase):
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_TYPE'
:
'multipart/form-data; boundary=boundary'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
request
.
POST
,
{
'name'
:
[
'value'
]})
self
.
assertRaises
(
Exception
,
lambda
:
request
.
body
)
...
...
@@ -334,14 +334,14 @@ class RequestsTests(unittest.TestCase):
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_TYPE'
:
'multipart/form-data; boundary=boundary'
,
'CONTENT_LENGTH'
:
0
,
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
request
.
POST
,
{})
def
test_read_by_lines
(
self
):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
self
.
assertEqual
(
list
(
request
),
[
b
'name=value'
])
def
test_POST_after_body_read
(
self
):
...
...
@@ -351,7 +351,7 @@ class RequestsTests(unittest.TestCase):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
raw_data
=
request
.
body
self
.
assertEqual
(
request
.
POST
,
{
'name'
:
[
'value'
]})
...
...
@@ -363,7 +363,7 @@ class RequestsTests(unittest.TestCase):
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
raw_data
=
request
.
body
self
.
assertEqual
(
request
.
read
(
1
),
b
'n'
)
self
.
assertEqual
(
request
.
POST
,
{
'name'
:
[
'value'
]})
...
...
@@ -383,7 +383,7 @@ class RequestsTests(unittest.TestCase):
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_TYPE'
:
'multipart/form-data; boundary=boundary'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)})
'wsgi.input'
:
Bytes
IO
(
payload
)})
raw_data
=
request
.
body
# Consume enough data to mess up the parsing:
self
.
assertEqual
(
request
.
read
(
13
),
b
'--boundary
\r\n
C'
)
...
...
@@ -397,7 +397,7 @@ class RequestsTests(unittest.TestCase):
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
String
IO
(
payload
)
'wsgi.input'
:
Bytes
IO
(
payload
)
})
with
warnings
.
catch_warnings
(
record
=
True
):
...
...
@@ -408,14 +408,14 @@ class RequestsTests(unittest.TestCase):
If wsgi.input.read() raises an exception while trying to read() the
POST, the exception should be identifiable (not a generic IOError).
"""
class
Exploding
StringIO
(
String
IO
):
class
Exploding
BytesIO
(
Bytes
IO
):
def
read
(
self
,
len
=
0
):
raise
IOError
(
"kaboom!"
)
payload
=
b
'name=value'
request
=
WSGIRequest
({
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_LENGTH'
:
len
(
payload
),
'wsgi.input'
:
Exploding
String
IO
(
payload
)})
'wsgi.input'
:
Exploding
Bytes
IO
(
payload
)})
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warnings
.
simplefilter
(
"always"
)
...
...
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