624
637
d = request.locateResource("/foo")
625
638
d.addCallback(gotResource)
643
class ParsePostDataTests(unittest.TestCase):
645
Tests for L{server.parsePOSTData}.
648
def test_noData(self):
650
Parsing a request without data should succeed but should not fill the
651
C{args} and C{files} attributes of the request.
653
root = resource.Resource()
654
request = SimpleRequest(server.Site(root), "GET", "/")
656
self.assertEquals(request.args, {})
657
self.assertEquals(request.files, {})
658
return server.parsePOSTData(request).addCallback(cb)
661
def test_noContentType(self):
663
Parsing a request without content-type should succeed but should not
664
fill the C{args} and C{files} attributes of the request.
666
root = resource.Resource()
667
request = SimpleRequest(server.Site(root), "GET", "/", content="foo")
669
self.assertEquals(request.args, {})
670
self.assertEquals(request.files, {})
671
return server.parsePOSTData(request).addCallback(cb)
674
def test_urlencoded(self):
676
Test parsing data in urlencoded format: it should end in the C{args}
679
ctype = http_headers.MimeType('application', 'x-www-form-urlencoded')
680
content = "key=value&multiple=two+words&multiple=more%20words"
681
root = resource.Resource()
682
request = SimpleRequest(server.Site(root), "GET", "/",
683
http_headers.Headers({'content-type': ctype}), content)
685
self.assertEquals(request.files, {})
686
self.assertEquals(request.args,
687
{'multiple': ['two words', 'more words'], 'key': ['value']})
688
return server.parsePOSTData(request).addCallback(cb)
691
def test_multipart(self):
693
Test parsing data in multipart format: it should fill the C{files}
696
ctype = http_headers.MimeType('multipart', 'form-data',
697
(('boundary', '---weeboundary'),))
698
content="""-----weeboundary\r
699
Content-Disposition: form-data; name="FileNameOne"; filename="myfilename"\r
700
Content-Type: text/html\r
702
my great content wooo\r
705
root = resource.Resource()
706
request = SimpleRequest(server.Site(root), "GET", "/",
707
http_headers.Headers({'content-type': ctype}), content)
709
self.assertEquals(request.args, {})
710
self.assertEquals(request.files.keys(), ['FileNameOne'])
711
self.assertEquals(request.files.values()[0][0][:2],
712
('myfilename', http_headers.MimeType('text', 'html', {})))
713
f = request.files.values()[0][0][2]
714
self.assertEquals(f.read(), "my great content wooo")
715
return server.parsePOSTData(request).addCallback(cb)
718
def test_multipartWithNoBoundary(self):
720
If the boundary type is not specified, parsing should fail with a
723
ctype = http_headers.MimeType('multipart', 'form-data')
724
content="""-----weeboundary\r
725
Content-Disposition: form-data; name="FileNameOne"; filename="myfilename"\r
726
Content-Type: text/html\r
728
my great content wooo\r
731
root = resource.Resource()
732
request = SimpleRequest(server.Site(root), "GET", "/",
733
http_headers.Headers({'content-type': ctype}), content)
734
return self.assertFailure(server.parsePOSTData(request),
738
def test_wrongContentType(self):
740
Check that a content-type not handled raise a C{http.HTTPError}.
742
ctype = http_headers.MimeType('application', 'foobar')
743
content = "key=value&multiple=two+words&multiple=more%20words"
744
root = resource.Resource()
745
request = SimpleRequest(server.Site(root), "GET", "/",
746
http_headers.Headers({'content-type': ctype}), content)
747
return self.assertFailure(server.parsePOSTData(request),
751
def test_mimeParsingError(self):
753
A malformed content should result in a C{http.HTTPError}.
755
The tested content has an invalid closing boundary.
757
ctype = http_headers.MimeType('multipart', 'form-data',
758
(('boundary', '---weeboundary'),))
759
content="""-----weeboundary\r
760
Content-Disposition: form-data; name="FileNameOne"; filename="myfilename"\r
761
Content-Type: text/html\r
763
my great content wooo\r
766
root = resource.Resource()
767
request = SimpleRequest(server.Site(root), "GET", "/",
768
http_headers.Headers({'content-type': ctype}), content)
769
return self.assertFailure(server.parsePOSTData(request),
773
def test_multipartMaxMem(self):
775
Check that the C{maxMem} parameter makes the parsing raise an
776
exception if the value is reached.
778
ctype = http_headers.MimeType('multipart', 'form-data',
779
(('boundary', '---weeboundary'),))
780
content="""-----weeboundary\r
781
Content-Disposition: form-data; name="FileNameOne"\r
782
Content-Type: text/html\r
784
my great content wooo
785
and even more and more\r
788
root = resource.Resource()
789
request = SimpleRequest(server.Site(root), "GET", "/",
790
http_headers.Headers({'content-type': ctype}), content)
792
self.assertEquals(res.response.description,
793
"Maximum length of 10 bytes exceeded.")
794
return self.assertFailure(server.parsePOSTData(request, maxMem=10),
795
http.HTTPError).addCallback(cb)
798
def test_multipartMaxSize(self):
800
Check that the C{maxSize} parameter makes the parsing raise an
801
exception if the data is too big.
803
ctype = http_headers.MimeType('multipart', 'form-data',
804
(('boundary', '---weeboundary'),))
805
content="""-----weeboundary\r
806
Content-Disposition: form-data; name="FileNameOne"; filename="myfilename"\r
807
Content-Type: text/html\r
809
my great content wooo
810
and even more and more\r
813
root = resource.Resource()
814
request = SimpleRequest(server.Site(root), "GET", "/",
815
http_headers.Headers({'content-type': ctype}), content)
817
self.assertEquals(res.response.description,
818
"Maximum length of 10 bytes exceeded.")
819
return self.assertFailure(server.parsePOSTData(request, maxSize=10),
820
http.HTTPError).addCallback(cb)
823
def test_maxFields(self):
825
Check that the C{maxSize} parameter makes the parsing raise an
826
exception if the data contains too many fields.
828
ctype = http_headers.MimeType('multipart', 'form-data',
829
(('boundary', '---xyz'),))
830
content = """-----xyz\r
831
Content-Disposition: form-data; name="foo"\r
835
Content-Disposition: form-data; name="foo"\r
839
Content-Disposition: form-data; name="file"; filename="filename"\r
840
Content-Type: text/html\r
844
Content-Disposition: form-data; name="file"; filename="filename"\r
845
Content-Type: text/plain\r
850
root = resource.Resource()
851
request = SimpleRequest(server.Site(root), "GET", "/",
852
http_headers.Headers({'content-type': ctype}), content)
854
self.assertEquals(res.response.description,
855
"Maximum number of fields 3 exceeded")
856
return self.assertFailure(server.parsePOSTData(request, maxFields=3),
857
http.HTTPError).addCallback(cb)
860
def test_otherErrors(self):
862
Test that errors durign parsing other than C{MimeFormatError} are
865
ctype = http_headers.MimeType('multipart', 'form-data',
866
(('boundary', '---weeboundary'),))
867
# XXX: maybe this is not a good example
868
# parseContentDispositionFormData could handle this problem
869
content="""-----weeboundary\r
870
Content-Disposition: form-data; name="FileNameOne"; filename="myfilename and invalid data \r
873
root = resource.Resource()
874
request = SimpleRequest(server.Site(root), "GET", "/",
875
http_headers.Headers({'content-type': ctype}), content)
876
return self.assertFailure(server.parsePOSTData(request),