# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/.
import collections import json import os from urllib.error import HTTPError from urllib.request import (
HTTPHandler,
ProxyHandler,
Request,
build_opener,
install_opener,
urlopen,
)
import mozhttpd import mozunit import pytest from six import ensure_binary, ensure_str
def httpd_url(httpd, path, querystr=None): """Return the URL to a started MozHttpd server for the given info."""
if querystr isnotNone:
url = "{url}?{querystr}".format(
url=url,
querystr=querystr,
)
return url
@pytest.fixture(name="num_requests") def fixture_num_requests(): """Return a defaultdict to count requests to HTTP handlers.""" return collections.defaultdict(int)
@pytest.fixture(name="try_get") def fixture_try_get(num_requests): """Return a function to try GET requests to the server."""
def try_get(httpd, querystr): """Try GET requests to the server."""
num_requests["get_handler"] = 0
f = urlopen(httpd_url(httpd, "/api/resource/1", querystr))
@pytest.fixture(name="httpd_no_urlhandlers") def fixture_httpd_no_urlhandlers(): """Yields a started MozHttpd server with no URL handlers."""
httpd = mozhttpd.MozHttpd(port=0)
httpd.start(block=False) yield httpd
httpd.stop()
@pytest.fixture(name="httpd_with_docroot") def fixture_httpd_with_docroot(num_requests): """Yields a started MozHttpd server with docroot set."""
@mozhttpd.handlers.json_response def get_handler(request, objid): """Handler for HTTP GET requests."""
# POST requests
try_post(httpd, "")
try_post(httpd, "?foo=bar")
# DEL requests
try_del(httpd, "")
try_del(httpd, "?foo=bar")
# GET: By default we don't serve any files if we just define an API with pytest.raises(HTTPError) as exc_info:
urlopen(httpd_url(httpd, "/"))
assert exc_info.value.code == 404
def test_nonexistent_resources(httpd_no_urlhandlers): # GET: Return 404 for non-existent endpoint with pytest.raises(HTTPError) as excinfo:
urlopen(httpd_url(httpd_no_urlhandlers, "/api/resource/")) assert excinfo.value.code == 404
# POST: POST should also return 404 with pytest.raises(HTTPError) as excinfo:
urlopen(
httpd_url(httpd_no_urlhandlers, "/api/resource/"),
data=ensure_binary(json.dumps({})),
) assert excinfo.value.code == 404
# DEL: DEL should also return 404
opener = build_opener(HTTPHandler)
request = Request(httpd_url(httpd_no_urlhandlers, "/api/resource/"))
request.get_method = lambda: "DEL"
with pytest.raises(HTTPError) as excinfo:
opener.open(request) assert excinfo.value.code == 404
# Make sure API methods still work
try_get(httpd_with_docroot, "")
try_get(httpd_with_docroot, "?foo=bar")
def index_contents(host): """Return the expected index contents for the given host.""" return"{host} index".format(host=host)
@pytest.fixture(name="hosts") def fixture_hosts(): """Returns a tuple of hosts.""" return ("mozilla.com", "mozilla.org")
@pytest.fixture(name="docroot") def fixture_docroot(tmpdir): """Returns a path object to a temporary docroot directory."""
docroot = tmpdir.mkdir("docroot")
index_file = docroot.join("index.html")
index_file.write(index_contents("*"))
yield docroot
docroot.remove()
@pytest.fixture(name="httpd_with_proxy_handler") def fixture_httpd_with_proxy_handler(docroot): """Yields a started MozHttpd server for the proxy test."""
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.