コード例 #1
0
ファイル: test_agentspy.py プロジェクト: withshubh/treq
    def test_provides_iagent(self):
        """
        The agent returned by agent_spy() provides the IAgent interface.
        """
        agent, _ = agent_spy()

        self.assertTrue(IAgent.providedBy(agent))
コード例 #2
0
    def test_add_unknown_auth(self):
        """
        add_auth() raises UnknownAuthConfig when given anything other than
        a tuple.
        """
        agent, _ = agent_spy()
        invalidAuth = 1234

        self.assertRaises(UnknownAuthConfig, add_auth, agent, invalidAuth)
コード例 #3
0
ファイル: test_agentspy.py プロジェクト: withshubh/treq
    def test_type_validation(self):
        """
        The request method enforces correctness by raising TypeError when
        passed parameters of the wrong type.
        """
        agent, _ = agent_spy()

        self.assertRaises(TypeError, agent.request, u"method not bytes",
                          b"uri")
        self.assertRaises(TypeError, agent.request, b"method",
                          u"uri not bytes")
        self.assertRaises(TypeError, agent.request, b"method", b"uri",
                          {"not": "headers"})
        self.assertRaises(TypeError, agent.request, b"method", b"uri", None,
                          b"not ibodyproducer")
コード例 #4
0
    def test_add_basic_auth(self):
        """
        add_auth() wraps the given agent with one that adds an ``Authorization:
        Basic ...`` HTTP header that contains the given credentials.
        """
        agent, requests = agent_spy()
        authAgent = add_auth(agent, ('username', 'password'))

        authAgent.request(b'method', b'uri')

        self.assertTrue(IAgent.providedBy(authAgent))
        self.assertEqual(
            requests[0].headers,
            Headers({b'authorization': [b'Basic dXNlcm5hbWU6cGFzc3dvcmQ=']})
        )
コード例 #5
0
    def test_add_basic_auth_bytes(self):
        """
        Basic auth can be passed as `bytes`, allowing the user full control
        over the encoding.
        """
        agent, requests = agent_spy()
        auth = (b'\x01\x0f\xff', b'\xff\xf0\x01')
        authAgent = add_auth(agent, auth)

        authAgent.request(b'method', b'uri')

        self.assertEqual(
            requests[0].headers,
            Headers({b'Authorization': [b'Basic AQ//Ov/wAQ==']}),
        )
コード例 #6
0
    def test_add_basic_auth_utf8(self):
        """
        Basic auth username and passwords given as `str` are encoded as UTF-8.

        https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Character_encoding_of_HTTP_authentication
        """
        agent, requests = agent_spy()
        auth = (u'\u16d7', u'\u16b9')
        authAgent = add_auth(agent, auth)

        authAgent.request(b'method', b'uri')

        self.assertEqual(
            requests[0].headers,
            Headers({b'Authorization': [b'Basic 4ZuXOuGauQ==']}),
        )
コード例 #7
0
ファイル: test_agentspy.py プロジェクト: withshubh/treq
    def test_records(self):
        """
        Each request made with the agent is recorded.
        """
        agent, requests = agent_spy()

        body = FileBodyProducer(BytesIO(b"..."))
        d1 = agent.request(b"GET", b"https://foo")
        d2 = agent.request(b"POST", b"http://bar", Headers({}))
        d3 = agent.request(b"PUT", b"https://baz", None, bodyProducer=body)

        self.assertEqual(
            requests,
            [
                RequestRecord(b"GET", b"https://foo", None, None, d1),
                RequestRecord(b"POST", b"http://bar", Headers({}), None, d2),
                RequestRecord(b"PUT", b"https://baz", None, body, d3),
            ],
        )
コード例 #8
0
ファイル: test_agentspy.py プロジェクト: withshubh/treq
    def test_record_attributes(self):
        """
        Each parameter passed to `request` is available as an attribute of the
        RequestRecord. Additionally, the deferred returned by the call is
        available.
        """
        agent, requests = agent_spy()
        headers = Headers()
        body = FileBodyProducer(BytesIO(b"..."))

        deferred = agent.request(b"method",
                                 b"uri",
                                 headers=headers,
                                 bodyProducer=body)

        [rr] = requests
        self.assertIs(rr.method, b"method")
        self.assertIs(rr.uri, b"uri")
        self.assertIs(rr.headers, headers)
        self.assertIs(rr.bodyProducer, body)
        self.assertIs(rr.deferred, deferred)
コード例 #9
0
    def test_add_basic_auth_huge(self):
        """
        The Authorization header doesn't include linebreaks, even if the
        credentials are so long that Python's base64 implementation inserts
        them.
        """
        agent, requests = agent_spy()
        pwd = ('verylongpasswordthatextendsbeyondthepointwheremultiplel'
               'inesaregenerated')
        expectedAuth = (
            b'Basic dXNlcm5hbWU6dmVyeWxvbmdwYXNzd29yZHRoYXRleHRlbmRzY'
            b'mV5b25kdGhlcG9pbnR3aGVyZW11bHRpcGxlbGluZXNhcmVnZW5lcmF0ZWQ='
        )
        authAgent = add_auth(agent, ('username', pwd))

        authAgent.request(b'method', b'uri')

        self.assertEqual(
            requests[0].headers,
            Headers({b'authorization': [expectedAuth]}),
        )
コード例 #10
0
 def setUp(self):
     self.agent, self.requests = agent_spy()