예제 #1
0
    def test_only_default_ports_are_dropped(self):
        result = (
            b("http"),
            b("social.yahooapis.com"),
            b("/v1/user/6677/connections"),
            b("start=0;count=20"),
            b(""),
            b(""),
        )
        url = b("""http://social.yahooapis.com:80/v1/user/6677/connections\
;start=0;count=20""")
        self.assertEqual(urlparse_normalized(url), result)

        result = (
            b("https"),
            b("social.yahooapis.com"),
            b("/v1/user/6677/connections"),
            b("start=0;count=20"),
            b(""),
            b(""),
        )
        url = b("""https://social.yahooapis.com:443/v1/user/6677/connections\
;start=0;count=20""")
        self.assertEqual(urlparse_normalized(url), result)

        result = (
            b("http"),
            b("social.yahooapis.com:8000"),
            b("/v1/user/6677/connections"),
            b("start=0;count=20"),
            b(""),
            b(""),
        )
        url = b("""http://social.yahooapis.com:8000/v1/user/6677/connections\
;start=0;count=20""")
        self.assertEqual(urlparse_normalized(url), result)

        result = (
            b("https"),
            b("social.yahooapis.com:8000"),
            b("/v1/user/6677/connections"),
            b("start=0;count=20"),
            b(""),
            b(""),
        )
        url = b("""https://social.yahooapis.com:8000/v1/user/6677/connections\
;start=0;count=20""")
        self.assertEqual(urlparse_normalized(url), result)
예제 #2
0
    def test_only_default_ports_are_dropped(self):
        result = (
            "http",
            "social.yahooapis.com",
            "/v1/user/6677/connections",
            "start=0;count=20",
            "",
            "",
        )
        url = "http://social.yahooapis.com:80/v1/user/6677/connections;start=0;count=20"
        assert_equal(urlparse_normalized(url), result)

        result = (
            "https",
            "social.yahooapis.com",
            "/v1/user/6677/connections",
            "start=0;count=20",
            "",
            "",
        )
        url = "https://social.yahooapis.com:443/v1/user/6677/connections;start=0;count=20"
        assert_equal(urlparse_normalized(url), result)

        result = (
            "http",
            "social.yahooapis.com:8000",
            "/v1/user/6677/connections",
            "start=0;count=20",
            "",
            "",
        )
        url = "http://social.yahooapis.com:8000/v1/user/6677/connections;start=0;count=20"
        assert_equal(urlparse_normalized(url), result)

        result = (
            "https",
            "social.yahooapis.com:8000",
            "/v1/user/6677/connections",
            "start=0;count=20",
            "",
            "",
        )
        url = "https://social.yahooapis.com:8000/v1/user/6677/connections;start=0;count=20"
        assert_equal(urlparse_normalized(url), result)
예제 #3
0
 def test_url_with_matrix_params(self):
     result = (
         "http",
         "social.yahooapis.com",
         "/v1/user/6677/connections",
         "start=0;count=20",
         "format=json",
         "fragment",
     )
     url = "http://social.yahooapis.com/v1/user/6677/connections;start=0;count=20?format=json#fragment"
     assert_equal(urlparse_normalized(url), result)
예제 #4
0
 def test_path_is_never_empty(self):
     url = "HTTP://*****:*****@WWW.EXAMPLE.COM:8000/?a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2#fragment"
     result = (
         "http",
         "UserName:[email protected]:8000",
         "/",
         "",
         "a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2",
         "fragment",
     )
     assert_equal(urlparse_normalized(url), result)
예제 #5
0
 def test_valid_parts_and_normalization(self):
     url = "HTTP://*****:*****@WWW.EXAMPLE.COM:8000/result;param1?a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2#fragment"
     result = (
         "http",
         "UserName:[email protected]:8000",
         "/result",
         "param1",
         "a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2",
         "fragment",
     )
     assert_equal(urlparse_normalized(url), result)
예제 #6
0
    def test_url_with_matrix_params(self):
        result = (
            b("http"),
            b("social.yahooapis.com"),
            b("/v1/user/6677/connections"),
            b("start=0;count=20"),
            b("format=json"),
            b("fragment"),
        )
        url = b("""http://social.yahooapis.com:80/v1/user/6677/connections\
;start=0;count=20?format=json#fragment""")
        self.assertEqual(urlparse_normalized(url), result)
예제 #7
0
    def test_path_is_never_empty(self):
        url = b("""HTTP://UserName:[email protected]:8000/\
?a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2#fragment""")
        result = (
            b("http"),
            b("UserName:[email protected]:8000"),
            b("/"),
            b(""),
            b("a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2"),
            b("fragment"),
        )
        self.assertEqual(urlparse_normalized(url), result)
예제 #8
0
    def test_valid_parts_and_normalization(self):
        url = b("""HTTP://UserName:[email protected]:8000/result\
;param1?a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2#fragment""")
        result = (
            b("http"),
            b("UserName:[email protected]:8000"),
            b("/result"),
            b("param1"),
            b("a=&a=1&a=2&oauth_consumer_key=9djdj82h48djs9d2"),
            b("fragment"),
        )
        self.assertEqual(urlparse_normalized(url), result)
예제 #9
0
def generate_base_string(method, url, oauth_params):
    """
    Calculates a signature base string based on the URL, method, and
    oauth parameters.

    Any query parameter by the name "oauth_signature" will be excluded
    from the base string.

    :see: Signature base string
    (http://tools.ietf.org/html/rfc5849#section-3.4.1)

    :param method:
        HTTP request method.
    :param url:
        The URL. If this includes a query string, query parameters are first
        extracted and encoded as well. All protocol-specific parameters
        will be ignored from the query string.
    :param oauth_params:
        Protocol-specific parameters must be specified in this dictionary.
        All non-protocol parameters will be ignored.
    :returns:
        Base string.
    """
    allowed_methods = HTTP_METHODS
    method_normalized = method.upper()
    if method_normalized not in allowed_methods:
        raise InvalidHttpMethodError(
            "Method must be one of the HTTP methods %s: "\
            "got `%s` instead" % (allowed_methods, method))
    if not url:
        raise InvalidUrlError("URL must be specified: got `%r`" % url)
    if not isinstance(oauth_params, dict):
        raise InvalidOAuthParametersError("Dictionary required: got `%r`" %
                                          oauth_params)

    scheme, netloc, path, matrix_params, query, _ = urlparse_normalized(url)
    query_string = generate_base_string_query(query, oauth_params)
    normalized_url = urlunparse((
        scheme,
        netloc,
        path,
        matrix_params,
        None,
        None
    ))
    return SYMBOL_AMPERSAND.join(map(percent_encode,
                           (method_normalized, normalized_url, query_string)))
예제 #10
0
def generate_signature_base_string(method, url, oauth_params):
    """
    Calculates a signature base string based on the URL, method, and
    oauth parameters.

    Any query parameter by the name "oauth_signature" will be excluded
    from the base string.

    :see: Signature base string (http://tools.ietf.org/html/rfc5849#section-3.4.1)

    :param method:
        HTTP request method.
    :param url:
        The URL. If this includes a query string, query parameters are first
        extracted and encoded as well. All protocol-specific parameters
        will be ignored from the query string.
    :param oauth_params:
        Protocol-specific parameters must be specified in this dictionary.
        All non-protocol parameters will be ignored.
    :returns:
        Base string.
    """
    allowed_methods = ("POST", "GET", "PUT", "DELETE",
                       "OPTIONS", "TRACE", "HEAD", "CONNECT",
                       "PATCH")
    method_normalized = method.upper()
    if method_normalized not in allowed_methods:
        raise InvalidHttpMethodError("Method must be one of the HTTP methods %s: got `%s` instead" % (allowed_methods, method))
    if not url:
        raise InvalidUrlError("URL must be specified: got `%r`" % (url, ))
    if not isinstance(oauth_params, dict):
        raise InvalidOAuthParametersError("Dictionary required: got `%r`" % (oauth_params, ))

    scheme, netloc, path, matrix_params, query, fragment = urlparse_normalized(url)
    query_string = _generate_signature_base_string_query(query, oauth_params)
    normalized_url = urlunparse((scheme, netloc, path, matrix_params, None, None))
    return "&".join([
        percent_encode(e) for e in [
            method_normalized, normalized_url, query_string]])