Exemple #1
0
def element_states() -> SearchStrategy[protocol.JsonLike]:
    def add_ref(e):
        e['ref'] = 1
        return e

    return dictionaries(keys=text(alphabet=['a', 'b', 'c'], max_size=3),
                        values=integers(min_value=0, max_value=5),
                        max_size=5).map(add_ref)
def urls() -> SearchStrategy[str]:
    """A strategy for :rfc:`3986`, generating http/https URLs."""
    def url_encode(s):
        return "".join(c if c in URL_SAFE_CHARACTERS else "%%%02X" % ord(c)
                       for c in s)

    schemes = st.sampled_from(["http", "https"])
    ports = st.integers(min_value=0, max_value=2**16 - 1).map(":{}".format)
    paths = st.lists(st.text(string.printable).map(url_encode)).map("/".join)

    return st.builds("{}://{}{}/{}".format, schemes, domains(),
                     st.just("") | ports, paths)