From d4ae13cde03d158708f47b90e135f5e20f9f6e05 Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Wed, 20 Jan 2010 15:21:57 +0100 Subject: [PATCH] forgot to commit tests --- tests/001-test-parser.py | 165 +++++++++++++++++++++++++++++++++++++++ tests/requests/001.http | 6 ++ tests/requests/002.http | 5 ++ tests/requests/003.http | 10 +++ tests/requests/004.http | 3 + tests/requests/005.http | 2 + tests/requests/006.http | 2 + tests/requests/007.http | 3 + tests/requests/008.http | 4 + tests/requests/009.http | 6 ++ tests/requests/010.http | 6 ++ tests/requests/011.http | 8 ++ tests/requests/012.http | 10 +++ tests/requests/013.http | 8 ++ tests/requests/014.http | 2 + tests/requests/015.http | 5 ++ tests/requests/016.http | 33 ++++++++ tests/responses/001.http | 15 ++++ tests/responses/002.http | 16 ++++ tests/responses/003.http | 2 + tests/responses/004.http | 2 + tests/responses/005.http | 11 +++ tests/responses/006.http | 5 ++ tests/responses/007.http | 7 ++ tests/t.py | 70 +++++++++++++++++ 25 files changed, 406 insertions(+) create mode 100644 tests/001-test-parser.py create mode 100644 tests/requests/001.http create mode 100644 tests/requests/002.http create mode 100644 tests/requests/003.http create mode 100644 tests/requests/004.http create mode 100644 tests/requests/005.http create mode 100644 tests/requests/006.http create mode 100644 tests/requests/007.http create mode 100644 tests/requests/008.http create mode 100644 tests/requests/009.http create mode 100644 tests/requests/010.http create mode 100644 tests/requests/011.http create mode 100644 tests/requests/012.http create mode 100644 tests/requests/013.http create mode 100644 tests/requests/014.http create mode 100644 tests/requests/015.http create mode 100644 tests/requests/016.http create mode 100644 tests/responses/001.http create mode 100644 tests/responses/002.http create mode 100644 tests/responses/003.http create mode 100644 tests/responses/004.http create mode 100644 tests/responses/005.http create mode 100644 tests/responses/006.http create mode 100644 tests/responses/007.http create mode 100644 tests/t.py diff --git a/tests/001-test-parser.py b/tests/001-test-parser.py new file mode 100644 index 00000000..a690d256 --- /dev/null +++ b/tests/001-test-parser.py @@ -0,0 +1,165 @@ +import t + +@t.request("001.http") +def test_001(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + + t.eq(p.method, "PUT") + t.eq(p.version, (1,0)) + t.eq(p.path, "/stuff/here") + t.eq(p.query_string, "foo=bar") + t.eq(sorted(p.headers), [ + ('Content-Length', '14'), + ('Content-Type', 'application/json'), + ('Server', 'http://127.0.0.1:5984') + ]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, '{"nom": "nom"}') + +@t.request("002.http") +def test_002(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/test") + t.eq(p.query_string, "") + t.eq(sorted(p.headers), [ + ("Accept", "*/*"), + ("Host", "0.0.0.0=5000"), + ("User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1") + ]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + +@t.request("003.http") +def test_003(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/favicon.ico") + t.eq(p.query_string, "") + t.eq(sorted(p.headers), [ + ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), + ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"), + ("Accept-Encoding", "gzip,deflate"), + ("Accept-Language", "en-us,en;q=0.5"), + ("Connection", "keep-alive"), + ("Host", "0.0.0.0=5000"), + ("Keep-Alive", "300"), + ("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0"), + ]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + +@t.request("004.http") +def test_004(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/dumbfuck") + t.eq(p.query_string, "") + t.eq(p.headers, [("Aaaaaaaaaaaaa", "++++++++++")]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + + +@t.request("005.http") +def test_005(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/forums/1/topics/2375") + t.eq(p.query_string, "page=1") + + + t.eq(p.fragment, "posts-17408") + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + +@t.request("006.http") +def test_006(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/get_no_headers_no_body/world") + t.eq(p.query_string, "") + t.eq(p.fragment, "") + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + +@t.request("007.http") +def test_007(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/get_one_header_no_body") + t.eq(p.query_string, "") + t.eq(p.fragment, "") + t.eq(p.headers, [('Accept', '*/*')]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "") + +@t.request("008.http") +def test_008(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "GET") + t.eq(p.version, (1, 0)) + t.eq(p.path, "/get_funky_content_length_body_hello") + t.eq(p.query_string, "") + t.eq(p.fragment, "") + t.eq(p.headers, [('Content-Length', '5')]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "HELLO") + +@t.request("009.http") +def test_009(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "POST") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/post_identity_body_world") + t.eq(p.query_string, "q=search") + t.eq(p.fragment, "hey") + t.eq(sorted(p.headers), [ + ('Accept', '*/*'), + ('Content-Length', '5'), + ('Transfer-Encoding', 'identity') + ]) + body, tr = p.filter_body(buf[i:]) + t.eq(body, "World") + +@t.request("010.http") +def test_010(buf, p): + headers = [] + i = p.filter_headers(headers, buf) + t.ne(i, -1) + t.eq(p.method, "POST") + t.eq(p.version, (1, 1)) + t.eq(p.path, "/post_chunked_all_your_base") + t.eq(p.headers, [('Transfer-Encoding', 'chunked')]) + t.eq(p.is_chunked, True) + body = "" + buf = buf[i:] + print buf + while not p.body_eof(): + chunk, buf = p.filter_body(buf) + body += chunk + t.eq(body, "all your base are belong to us") diff --git a/tests/requests/001.http b/tests/requests/001.http new file mode 100644 index 00000000..4ad7ed9d --- /dev/null +++ b/tests/requests/001.http @@ -0,0 +1,6 @@ +PUT /stuff/here?foo=bar HTTP/1.0 +Server: http://127.0.0.1:5984 +Content-Type: application/json +Content-Length: 14 + +{"nom": "nom"} \ No newline at end of file diff --git a/tests/requests/002.http b/tests/requests/002.http new file mode 100644 index 00000000..4dac660e --- /dev/null +++ b/tests/requests/002.http @@ -0,0 +1,5 @@ +GET /test HTTP/1.1 +User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1 +Host: 0.0.0.0=5000 +Accept: */* + diff --git a/tests/requests/003.http b/tests/requests/003.http new file mode 100644 index 00000000..583f1a26 --- /dev/null +++ b/tests/requests/003.http @@ -0,0 +1,10 @@ +GET /favicon.ico HTTP/1.1 +Host: 0.0.0.0=5000 +User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0 +Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 +Accept-Language: en-us,en;q=0.5 +Accept-Encoding: gzip,deflate +Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 +Keep-Alive: 300 +Connection: keep-alive + diff --git a/tests/requests/004.http b/tests/requests/004.http new file mode 100644 index 00000000..a1f5c2fc --- /dev/null +++ b/tests/requests/004.http @@ -0,0 +1,3 @@ +GET /dumbfuck HTTP/1.1 +aaaaaaaaaaaaa:++++++++++ + diff --git a/tests/requests/005.http b/tests/requests/005.http new file mode 100644 index 00000000..4907a20c --- /dev/null +++ b/tests/requests/005.http @@ -0,0 +1,2 @@ +GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1 + diff --git a/tests/requests/006.http b/tests/requests/006.http new file mode 100644 index 00000000..78c7b3a2 --- /dev/null +++ b/tests/requests/006.http @@ -0,0 +1,2 @@ +GET /get_no_headers_no_body/world HTTP/1.1 + diff --git a/tests/requests/007.http b/tests/requests/007.http new file mode 100644 index 00000000..0e450f2d --- /dev/null +++ b/tests/requests/007.http @@ -0,0 +1,3 @@ +GET /get_one_header_no_body HTTP/1.1 +Accept: */* + diff --git a/tests/requests/008.http b/tests/requests/008.http new file mode 100644 index 00000000..86f42b7e --- /dev/null +++ b/tests/requests/008.http @@ -0,0 +1,4 @@ +GET /get_funky_content_length_body_hello HTTP/1.0 +conTENT-Length: 5 + +HELLO \ No newline at end of file diff --git a/tests/requests/009.http b/tests/requests/009.http new file mode 100644 index 00000000..7cebe7f1 --- /dev/null +++ b/tests/requests/009.http @@ -0,0 +1,6 @@ +POST /post_identity_body_world?q=search#hey HTTP/1.1 +Accept: */* +Transfer-Encoding: identity +Content-Length: 5 + +World \ No newline at end of file diff --git a/tests/requests/010.http b/tests/requests/010.http new file mode 100644 index 00000000..c619c8f1 --- /dev/null +++ b/tests/requests/010.http @@ -0,0 +1,6 @@ +POST /post_chunked_all_your_base HTTP/1.1 +Transfer-Encoding: chunked + +1e +all your base are belong to us +0 diff --git a/tests/requests/011.http b/tests/requests/011.http new file mode 100644 index 00000000..09cac99c --- /dev/null +++ b/tests/requests/011.http @@ -0,0 +1,8 @@ +POST /two_chunks_mult_zero_end HTTP/1.1 +Transfer-Encoding: chunked + +5 +hello +6 + world +000 diff --git a/tests/requests/012.http b/tests/requests/012.http new file mode 100644 index 00000000..60570af7 --- /dev/null +++ b/tests/requests/012.http @@ -0,0 +1,10 @@ +POST /chunked_w_trailing_headers HTTP/1.1 +Transfer-Encoding: chunked + +5 +hello +6 + world +0 +Vary: * +Content-Type: text/plain diff --git a/tests/requests/013.http b/tests/requests/013.http new file mode 100644 index 00000000..3d035cd7 --- /dev/null +++ b/tests/requests/013.http @@ -0,0 +1,8 @@ +POST /chunked_w_bullshit_after_length HTTP/1.1 +Transfer-Encoding: chunked + +5; some; parameters=stuff +hello +6; blahblah; blah + world +0 diff --git a/tests/requests/014.http b/tests/requests/014.http new file mode 100644 index 00000000..beb09ee5 --- /dev/null +++ b/tests/requests/014.http @@ -0,0 +1,2 @@ +GET /with_"stupid"_quotes?foo="bar" HTTP/1.1 + diff --git a/tests/requests/015.http b/tests/requests/015.http new file mode 100644 index 00000000..6baf0951 --- /dev/null +++ b/tests/requests/015.http @@ -0,0 +1,5 @@ +GET /test HTTP/1.0 +Host: 0.0.0.0:5000 +User-Agent: ApacheBench/2.3 +Accept: */* + diff --git a/tests/requests/016.http b/tests/requests/016.http new file mode 100644 index 00000000..a37dd1cd --- /dev/null +++ b/tests/requests/016.http @@ -0,0 +1,33 @@ +GET / HTTP/1.1 +X-SSL-Bullshit: -----BEGIN CERTIFICATE----- + MIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx + ETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT + AkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu + dWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV + SzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV + BAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB + BQADggEPADCCAQoCggEBANPEQBgl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aWQt5hYy51ay9wdWIv + Y3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3 + XCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8 + UO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk + hTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK + wTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu + Yhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3 + RA== + -----END CERTIFICATE----- diff --git a/tests/responses/001.http b/tests/responses/001.http new file mode 100644 index 00000000..8a68daad --- /dev/null +++ b/tests/responses/001.http @@ -0,0 +1,15 @@ +HTTP/1.1 301 Moved Permanently +Location: http://www.google.com/ +Content-Type: text/html; charset=UTF-8 +Date: Sun, 26 Apr 2009 11:11:49 GMT +Expires: Tue, 26 May 2009 11:11:49 GMT +Cache-Control: public, max-age=2592000 +Server: gws +Content-Length: 219 + + +301 Moved +

301 Moved

+The document has moved +here. + \ No newline at end of file diff --git a/tests/responses/002.http b/tests/responses/002.http new file mode 100644 index 00000000..359fbddc --- /dev/null +++ b/tests/responses/002.http @@ -0,0 +1,16 @@ +HTTP/1.1 200 OK +Date: Tue, 04 Aug 2009 07:59:32 GMT +Server: Apache +X-Powered-By: Servlet/2.5 JSP/2.1 +Content-Type: text/xml; charset=utf-8 +Connection: close + + + + + + SOAP-ENV:Client + Client Error + + + \ No newline at end of file diff --git a/tests/responses/003.http b/tests/responses/003.http new file mode 100644 index 00000000..b237b01f --- /dev/null +++ b/tests/responses/003.http @@ -0,0 +1,2 @@ +HTTP/1.1 404 Not Found + diff --git a/tests/responses/004.http b/tests/responses/004.http new file mode 100644 index 00000000..92afe60e --- /dev/null +++ b/tests/responses/004.http @@ -0,0 +1,2 @@ +HTTP/1.1 301 + diff --git a/tests/responses/005.http b/tests/responses/005.http new file mode 100644 index 00000000..8beccfd3 --- /dev/null +++ b/tests/responses/005.http @@ -0,0 +1,11 @@ +HTTP/1.1 200 OK +Content-Type: text/plain +Transfer-Encoding: chunked + +25 +This is the data in the first chunk + +1C +and this is the second one + +0 diff --git a/tests/responses/006.http b/tests/responses/006.http new file mode 100644 index 00000000..f99f2d71 --- /dev/null +++ b/tests/responses/006.http @@ -0,0 +1,5 @@ +HTTP/1.1 200 OK +Content-Type: text/html; charset=utf-8 +Connection: close + +these headers are from http://news.ycombinator.com/ \ No newline at end of file diff --git a/tests/responses/007.http b/tests/responses/007.http new file mode 100644 index 00000000..e4113b7e --- /dev/null +++ b/tests/responses/007.http @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Content-Type: text/html; charset=UTF-8 +Content-Length: 11 +Proxy-Connection: close +Date: Thu, 31 Dec 2009 20:55:48 +0000 + +hello world \ No newline at end of file diff --git a/tests/t.py b/tests/t.py new file mode 100644 index 00000000..880bcb23 --- /dev/null +++ b/tests/t.py @@ -0,0 +1,70 @@ +# Copyright 2009 Paul J. Davis +# +# This file is part of the pywebmachine package released +# under the MIT license. +import inspect +import os +import re +import unittest + +from gunicorn.http import HttpParser + +dirname = os.path.dirname(__file__) + + +def data_source(fname, eol): + with open(fname) as handle: + lines = [] + for line in handle: + next = line.rstrip("\r\n") + eol + if next == "\r\n": + eol = "" + lines.append(next) + return "".join(lines) + +class request(object): + def __init__(self, name, eol="\r\n"): + self.fname = os.path.join(dirname, "requests", name) + self.eol = eol + + def __call__(self, func): + def run(): + src = data_source(self.fname, self.eol) + func(src, HttpParser()) + run.func_name = func.func_name + return run + +def eq(a, b): + assert a == b, "%r != %r" % (a, b) + +def ne(a, b): + assert a != b, "%r == %r" % (a, b) + +def lt(a, b): + assert a < b, "%r >= %r" % (a, b) + +def gt(a, b): + assert a > b, "%r <= %r" % (a, b) + +def isin(a, b): + assert a in b, "%r is not in %r" % (a, b) + +def isnotin(a, b): + assert a not in b, "%r is in %r" % (a, b) + +def has(a, b): + assert hasattr(a, b), "%r has no attribute %r" % (a, b) + +def hasnot(a, b): + assert not hasattr(a, b), "%r has an attribute %r" % (a, b) + +def raises(exctype, func, *args, **kwargs): + try: + func(*args, **kwargs) + except exctype, inst: + pass + else: + func_name = getattr(func, "func_name", "") + raise AssertionError("Function %s did not raise %s" % ( + func_name, exctype.__name__)) +