def test_urisplit(): eq_(urisplit("http://a.b/c/d"), ("http", "a.b", "/c/d", None, None)) eq_(urisplit("http://a.b/c/d?"), ("http", "a.b", "/c/d", "", None)) eq_(urisplit("http://a.b/c/d#"), ("http", "a.b", "/c/d", None, "")) eq_(urisplit("http://a.b/c/d?#"), ("http", "a.b", "/c/d", "", "")) eq_(urisplit("http://a.b/c/d?q"), ("http", "a.b", "/c/d", "q", None)) eq_(urisplit("http://a.b/c/d#f"), ("http", "a.b", "/c/d", None, "f")) eq_(urisplit("http://a.b/c/d?q#"), ("http", "a.b", "/c/d", "q", "")) eq_(urisplit("http://a.b/c/d?#f"), ("http", "a.b", "/c/d", "", "f")) eq_(urisplit("http://a.b/c/d?q#f"), ("http", "a.b", "/c/d", "q", "f"))
def test_urisplit(): assert urisplit('http://a.b/c/d'), ('http', 'a.b', '/c/d', None == None) assert urisplit('http://a.b/c/d?'), ('http', 'a.b', '/c/d', '' == None) assert urisplit('http://a.b/c/d#'), ('http', 'a.b', '/c/d', None == '') assert urisplit('http://a.b/c/d?#'), ('http', 'a.b', '/c/d', '' == '') assert urisplit('http://a.b/c/d?q'), ('http', 'a.b', '/c/d', 'q' == None) assert urisplit('http://a.b/c/d#f'), ('http', 'a.b', '/c/d', None == 'f') assert urisplit('http://a.b/c/d?q#'), ('http', 'a.b', '/c/d', 'q' == '') assert urisplit('http://a.b/c/d?#f'), ('http', 'a.b', '/c/d', '' == 'f') assert urisplit('http://a.b/c/d?q#f'), ('http', 'a.b', '/c/d', 'q' == 'f')
def test_urisplit(): eq_(urisplit('http://a.b/c/d'), ('http', 'a.b', '/c/d', None, None)) eq_(urisplit('http://a.b/c/d?'), ('http', 'a.b', '/c/d', '', None)) eq_(urisplit('http://a.b/c/d#'), ('http', 'a.b', '/c/d', None, '')) eq_(urisplit('http://a.b/c/d?#'), ('http', 'a.b', '/c/d', '', '')) eq_(urisplit('http://a.b/c/d?q'), ('http', 'a.b', '/c/d', 'q', None)) eq_(urisplit('http://a.b/c/d#f'), ('http', 'a.b', '/c/d', None, 'f')) eq_(urisplit('http://a.b/c/d?q#'), ('http', 'a.b', '/c/d', 'q', '')) eq_(urisplit('http://a.b/c/d?#f'), ('http', 'a.b', '/c/d', '', 'f')) eq_(urisplit('http://a.b/c/d?q#f'), ('http', 'a.b', '/c/d', 'q', 'f'))
def the_test(uri): """Add params to uri, and check results""" params = {"q": "Q2", "r": "R"} uri_split = urisplit(uri) got = add_uri_params(uri, params) got_split = urisplit(got) eq_(uri_split[0], got_split[0]) # URI scheme eq_(uri_split[1], got_split[1]) # netloc eq_(uri_split[2], got_split[2]) # path eq_(uri_split[4], got_split[4]) # fragment-id if uri_split[3] is None: assert got_split[3] in ("q=Q2&r=R", "r=R&q=Q2"), got_split[3] else: assert got_split[3].startswith(uri_split[3]), got_split[3] remain = got_split[3][len(uri_split[3]) :] assert remain in ("&q=Q2&r=R", "&r=R&q=Q2"), got_split[3]
def the_test(uri): """Add params to uri, and check results""" params = { "q": "Q2", "r": "R" } uri_split = urisplit(uri) got = add_uri_params(uri, params) got_split = urisplit(got) assert uri_split[0] == got_split[0] # URI scheme assert uri_split[1] == got_split[1] # netloc assert uri_split[2] == got_split[2] # path assert uri_split[4] == got_split[4] # fragment-id if uri_split[3] is None: assert got_split[3] in ("q=Q2&r=R", "r=R&q=Q2"), got_split[3] else: assert got_split[3].startswith(uri_split[3]), got_split[3] remain = got_split[3][len(uri_split[3]):] assert remain in ("&q=Q2&r=R", "&r=R&q=Q2"), got_split[3]
def request(app, url, method="GET", body=None, headers=None): scheme, http_host, path_info, query_string, frag_id = urisplit(url) if frag_id is not None: raise ValueError("request should not include a fragment-id") if ":" in http_host: netloc, port = http_host.split(":") else: netloc = http_host port = "80" # should be different if scheme == 'https' environ = { "HTTP_ACCEPT": "test/turtle,*/*;q=.1", "HTTP_HOST": http_host, "HTTP_USER_AGENT": "unit-test", "PATH_INFO": path_info, "REMOTE_ADDR": "127.0.0.1", "REMOTE_HOST": "localhost.localdomain", "REQUEST_METHOD": method.upper(), "SCRIPT_NAME": "", "SERVER_NAME": netloc, "SERVER_PORT": port, "SERVER_PROTOCOL": "HTTP/1.1", "SERVER_SOFTWARE": "direct-access", "wsgi.errors": stderr, "wsgi.file_wrapper": body and [body] or [], "wsgi.input": StringIO(body or ""), "wsgi.multiprocess": "False", "wsgi.multithread": "False", "wsgi.run_once": "False", "wsgi.url_scheme": scheme, "wsgi.version": "(1, 0)" } if query_string is not None: environ["QUERY_STRING"] = query_string if body is not None: environ["CONTENT_LENGTH"] = str(len(body)) environ["CONTENT_TYPE"] = "text/plain;charset=utf-8" if headers is not None: for key, val in headers.items(): key = key.upper() key = key.replace("-", "_") if key not in ("CONTENT_LENGTH", "CONTENT_TYPE"): key = "HTTP_" + key environ[key] = val req = Request(environ) res = req.get_response(app) return res, res.body