def test__resource_request_data(self):
        expected = RequestAdapter(
            HTTP_GET,
            RFC_RESOURCE_FULL_URL,
            b(''),
            headers = {
                HEADER_AUTHORIZATION_CAPS: b('''\
OAuth realm="Photos",\
oauth_consumer_key="dpf43f3p2l4k3l03",\
oauth_token="nnch734d00sl2jdk",\
oauth_signature_method="HMAC-SHA1",\
oauth_timestamp="137131202",\
oauth_nonce="chapoH",\
oauth_signature="MdpQcU8iPSUjWoN%2FUDMsK2sui9I%3D"'''),
            }
        )
        auth_credentials = Credentials(RFC_TOKEN_IDENTIFIER,
                                       RFC_TOKEN_SECRET)
        client_credentials = Credentials(RFC_CLIENT_IDENTIFIER,
                                         RFC_CLIENT_SECRET)

        class MockClient(_OAuthClient):
            @classmethod
            def generate_timestamp(cls):
                return RFC_TIMESTAMP_3

            @classmethod
            def generate_nonce(cls):
                return RFC_NONCE_3

        got = MockClient._request(
            client_credentials,
            HTTP_GET,
            RFC_RESOURCE_URI,
            params={
                "file": b("vacation.jpg"),
                "size": b("original"),
            },
            realm=RFC_REALM,
            auth_credentials=auth_credentials,
            oauth_version=None
        )
        self.assertEqual(got.method, expected.method)
        self.assertEqual(got.url, expected.url)
        self.assertEqual(got.body, expected.body)
        expected_headers, expected_realm = parse_authorization_header(
            expected.headers[HEADER_AUTHORIZATION_CAPS],
        )
        got_headers, got_realm = parse_authorization_header(
            got.headers[HEADER_AUTHORIZATION_CAPS],
        )
        self.assertDictEqual(got_headers, expected_headers)
        self.assertEqual(got_realm, expected_realm)
    def test_equality_encoding_realm_emptyValues(self):
        expected_value = ({
            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: [utf8_decode(OAUTH_VERSION_1)],
            OAUTH_PARAM_TOKEN: ['ad180jjd733klru7'],
            'oauth_empty': [''],
            OAUTH_PARAM_SIGNATURE: ['wOJIO9A2W5mFwDgiDvZbTSMK/PY='],
            }, 'Examp%20le'
        )
        self.assertEqual(expected_value, parse_authorization_header('''\
            OAuth\
\
            realm="Examp%20le",\
            oauth_consumer_key="0685bd9184jfhq22",\
            oauth_token="ad180jjd733klru7",\
            oauth_signature_method="HMAC-SHA1",\
            oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",\
            oauth_timestamp="137131200",\
            oauth_nonce="4572616e48616d6d65724c61686176",\
            oauth_version="1.0",\
            oauth_something="%20Some+Example",\
            oauth_empty=""\
        '''), "parsing failed.")
    def test__request_data(self):
        expected = RequestAdapter(
            HTTP_POST,
            RFC_TEMP_URI,
            b(''),
            headers = {
                HEADER_AUTHORIZATION_CAPS: b('''\
OAuth realm="Photos",\
oauth_consumer_key="dpf43f3p2l4k3l03",\
oauth_signature_method="HMAC-SHA1",\
oauth_timestamp="137131200",\
oauth_nonce="wIjqoS",\
oauth_callback="http%3A%2F%2Fprinter.example.com%2Fready",\
oauth_signature="74KNZJeDHnMBp0EMJ9ZHt%2FXKycU%3D"'''),
            }
        )
        client_credentials = Credentials(RFC_CLIENT_IDENTIFIER,
                                         RFC_CLIENT_SECRET)

        class MockClient(_OAuthClient):
            @classmethod
            def generate_timestamp(cls):
                return RFC_TIMESTAMP_1

            @classmethod
            def generate_nonce(cls):
                return RFC_NONCE_1

        got = MockClient._request(
            client_credentials,
            HTTP_POST,
            RFC_TEMP_URI,
            realm=RFC_REALM,
            oauth_version=None,
            oauth_callback=RFC_OAUTH_CALLBACK_URI
        )
        self.assertEqual(got.method, expected.method)
        self.assertEqual(got.url, expected.url)
        self.assertEqual(got.body, expected.body)
        expected_headers, expected_realm = parse_authorization_header(
            expected.headers[HEADER_AUTHORIZATION_CAPS],
        )
        got_headers, got_realm = parse_authorization_header(
            got.headers[HEADER_AUTHORIZATION_CAPS],
        )
        self.assertDictEqual(got_headers, expected_headers)
        self.assertEqual(got_realm, expected_realm)
 def test_value_can_have_newlines_when_not_strict(self):
     test_value = '''OAuth realm="Examp%20le",
         oauth_something="%20Some+Example"
     '''
     expected_value = ({
         "oauth_something": [" Some Example"],
     }, "Examp%20le")
     got = parse_authorization_header(test_value, strict=False)
     self.assertDictEqual(got[0], expected_value[0])
     self.assertEqual(got[1], expected_value[1])
 def test_dict_does_not_contain_string_OAuth_realm(self):
     header = b('''OAuth realm="http://example.com",\
         oauth_consumer_key="0685bd9184jfhq22",\
         oauth_token="ad180jjd733klru7",\
         oauth_signature_method="HMAC-SHA1",\
         oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",\
         oauth_timestamp="137131200",\
         oauth_nonce="4572616e48616d6d65724c61686176",\
         oauth_version="1.0",\
         oauth_something="%20Some+Example",\
         oauth_empty=""\
     ''')
     params, _ = parse_authorization_header(header)
     for name, _ in params.items():
         self.assertFalse(name.lower() == 'oauth realm',
                      '`OAuth realm` found in header names')
         self.assertFalse(name.lower() == "realm",
                          '`realm` found in header names')