def test_param_delimiter_can_be_changed(self): params = { OAUTH_PARAM_REALM: ['Examp%20le'], OAUTH_PARAM_NONCE: ['4572616e48616d6d65724c61686176'], OAUTH_PARAM_TIMESTAMP: ['137131200'], OAUTH_PARAM_CONSUMER_KEY: ['0685bd9184jfhq22'], 'oauth_something': [' Some Example'], OAUTH_PARAM_SIGNATURE_METHOD: ['HMAC-SHA1'], OAUTH_PARAM_VERSION: [OAUTH_VERSION_1], OAUTH_PARAM_TOKEN: ['ad180jjd733klru7'], 'oauth_empty': [''], OAUTH_PARAM_SIGNATURE: ['wOJIO9A2W5mFwDgiDvZbTSMK/PY='], } expected_value = b('''OAuth \ realm="http://example.com/"\ &oauth_consumer_key="0685bd9184jfhq22"\ &oauth_empty=""\ &oauth_nonce="4572616e48616d6d65724c61686176"\ &oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D"\ &oauth_signature_method="HMAC-SHA1"\ &oauth_something="%20Some%20Example"\ &oauth_timestamp="137131200"\ &oauth_token="ad180jjd733klru7"\ &oauth_version="1.0"''') self.assertEqual(generate_authorization_header(params, realm="http://example.com/", param_delimiter="&") , expected_value)
def _build_request(cls, method, url, params, body, headers, oauth_params, realm, use_authorization_header): """ Builds a request based on the HTTP arguments and OAuth protocol parameters. :param method: HTTP method. :param url: Request URL :param params: Additional query/payload parameters. If a `body` argument to this function is specified, the parameters are appended to the URL query string. If a `body` is not specified and a method other than GET is used the parameters will be added to the entity body. :param body: Entity body. :param oauth_params: Protocol-specific parameters. :param realm: OAuth authorization realm. :param use_authorization_header: ``True`` if the Authorization HTTP header should be used; ``False`` otherwise. :returns: An instance of :class:`pyoauth.http.RequestAdapter`. """ # http://tools.ietf.org/html/rfc5849#section-3.6 if HEADER_AUTHORIZATION_CAPS in headers or \ HEADER_AUTHORIZATION in headers: raise InvalidAuthorizationHeaderError( "Authorization field is already present in headers: %r" % \ headers ) if use_authorization_header: headers[HEADER_AUTHORIZATION_CAPS] = \ generate_authorization_header(oauth_params, realm) # Empty oauth params so that they are not included again below. oauth_params = None # OAuth requests can contain payloads. if body or method == HTTP_GET: # Append params to query string. url = url_append_query(url_add_query(url, params), oauth_params) if body and method == HTTP_GET: raise InvalidHttpRequestError( "HTTP method GET does not take an entity body" ) if body and \ HEADER_CONTENT_LENGTH not in headers and \ HEADER_CONTENT_LENGTH_CAPS not in headers: raise ValueError("You must set the `content-length` header.") else: if params or oauth_params: # Append to payload and set content type. body = utf8_encode(query_append(params, oauth_params)) headers[HEADER_CONTENT_TYPE] = CONTENT_TYPE_FORM_URLENCODED headers[HEADER_CONTENT_LENGTH] = str(len(body)).encode("ascii") else: # Zero-length body. body = SYMBOL_EMPTY_BYTES headers[HEADER_CONTENT_LENGTH] = SYMBOL_ZERO return RequestAdapter(method, url, body, headers)