Ejemplo n.º 1
0
    def test_http_cloud_id(self):
        con = RequestsHttpConnection(
            cloud_id=
            "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng=="
        )
        self.assertTrue(con.use_ssl)
        self.assertEqual(
            con.host,
            "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
        self.assertEqual(con.port, None)
        self.assertEqual(
            con.hostname,
            "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
        self.assertTrue(con.http_compress)

        con = RequestsHttpConnection(
            cloud_id=
            "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
            port=9243,
        )
        self.assertEqual(
            con.host,
            "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io:9243",
        )
        self.assertEqual(con.port, 9243)
        self.assertEqual(
            con.hostname,
            "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
Ejemplo n.º 2
0
    def test_api_key_auth(self):
        # test with tuple
        con = RequestsHttpConnection(
            cloud_id=
            "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
            api_key=("elastic", "changeme1"),
        )
        self.assertEquals(con.session.headers["authorization"],
                          "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE=")

        # test with base64 encoded string
        con = RequestsHttpConnection(
            cloud_id=
            "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
            api_key="ZWxhc3RpYzpjaGFuZ2VtZTI=",
        )
        self.assertEquals(con.session.headers["authorization"],
                          "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI=")
Ejemplo n.º 3
0
 def test_http_cloud_id(self):
     con = RequestsHttpConnection(
         cloud_id=
         "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n"
     )
     self.assertTrue(con.use_ssl)
     self.assertEquals(
         con.host,
         "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243")
Ejemplo n.º 4
0
    def test_cloud_id_http_compress_override(self):
        # 'http_compress' will be 'True' by default for connections with
        # 'cloud_id' set but should prioritize user-defined values.
        con = RequestsHttpConnection(
            cloud_id="cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
        )
        self.assertEqual(con.http_compress, True)

        con = RequestsHttpConnection(
            cloud_id="cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
            http_compress=False,
        )
        self.assertEqual(con.http_compress, False)

        con = RequestsHttpConnection(
            cloud_id="cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
            http_compress=True,
        )
        self.assertEqual(con.http_compress, True)
Ejemplo n.º 5
0
    def test_cloud_id_http_compress_override(self):
        # 'http_compress' will be 'True' by default for connections with
        # 'cloud_id' set but should prioritize user-defined values.
        con = RequestsHttpConnection(
            cloud_id=
            "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
        )
        self.assertEquals(con.http_compress, True)

        con = RequestsHttpConnection(
            cloud_id=
            "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
            http_compress=False)
        self.assertEquals(con.http_compress, False)

        con = RequestsHttpConnection(
            cloud_id=
            "foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
            http_compress=True)
        self.assertEquals(con.http_compress, True)
Ejemplo n.º 6
0
 def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'):
     con = RequestsHttpConnection(**connection_params)
     def _dummy_send(*args, **kwargs):
         dummy_response = Mock()
         dummy_response.headers = {}
         dummy_response.status_code = status_code
         dummy_response.text = response_body
         dummy_response.request = args[0]
         dummy_response.cookies = {}
         _dummy_send.call_args = (args, kwargs)
         return dummy_response
     con.session.send = _dummy_send
     return con
Ejemplo n.º 7
0
    def test_api_key_auth(self):
        # test with tuple
        con = RequestsHttpConnection(
            cloud_id=
            "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
            api_key=("elastic", "changeme1"),
        )
        self.assertEqual(con.session.headers["authorization"],
                         "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE=")
        self.assertEqual(
            con.host,
            "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")

        # test with base64 encoded string
        con = RequestsHttpConnection(
            cloud_id=
            "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==",
            api_key="ZWxhc3RpYzpjaGFuZ2VtZTI=",
        )
        self.assertEqual(con.session.headers["authorization"],
                         "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI=")
        self.assertEqual(
            con.host,
            "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
Ejemplo n.º 8
0
 def test_http_auth_list(self):
     con = RequestsHttpConnection(http_auth=["username", "secret"])
     self.assertEquals(("username", "secret"), con.session.auth)
Ejemplo n.º 9
0
 def test_timeout_set(self):
     con = RequestsHttpConnection(timeout=42)
     self.assertEquals(42, con.timeout)
Ejemplo n.º 10
0
    def test_custom_http_auth_is_allowed(self):
        auth = AuthBase()
        c = RequestsHttpConnection(http_auth=auth)

        self.assertEquals(auth, c.session.auth)
Ejemplo n.º 11
0
 def test_http_auth_tuple(self):
     con = RequestsHttpConnection(http_auth=("username", "secret"))
     self.assertEqual(("username", "secret"), con.session.auth)
Ejemplo n.º 12
0
 def test_opaque_id(self):
     con = RequestsHttpConnection(opaque_id="app-1")
     self.assertEqual(con.headers["x-opaque-id"], "app-1")
Ejemplo n.º 13
0
    def test_requests_connection(self):
        # Defaults
        conn = RequestsHttpConnection("httpbin.org", port=443, use_ssl=True)
        user_agent = conn._get_default_user_agent()
        status, data = self.httpbin_anything(conn)
        assert status == 200
        assert data["method"] == "GET"
        assert data["headers"] == {
            "Accept-Encoding": "identity",
            "Content-Type": "application/json",
            "Host": "httpbin.org",
            "User-Agent": user_agent,
        }

        # http_compress=False
        conn = RequestsHttpConnection("httpbin.org",
                                      port=443,
                                      use_ssl=True,
                                      http_compress=False)
        status, data = self.httpbin_anything(conn)
        assert status == 200
        assert data["method"] == "GET"
        assert data["headers"] == {
            "Accept-Encoding": "identity",
            "Content-Type": "application/json",
            "Host": "httpbin.org",
            "User-Agent": user_agent,
        }

        # http_compress=True
        conn = RequestsHttpConnection("httpbin.org",
                                      port=443,
                                      use_ssl=True,
                                      http_compress=True)
        status, data = self.httpbin_anything(conn)
        assert status == 200
        assert data["headers"] == {
            "Accept-Encoding": "gzip,deflate",
            "Content-Type": "application/json",
            "Host": "httpbin.org",
            "User-Agent": user_agent,
        }

        # Headers
        conn = RequestsHttpConnection(
            "httpbin.org",
            port=443,
            use_ssl=True,
            http_compress=True,
            headers={"header1": "value1"},
        )
        status, data = self.httpbin_anything(conn,
                                             headers={
                                                 "header2": "value2",
                                                 "header1": "override!"
                                             })
        assert status == 200
        assert data["headers"] == {
            "Accept-Encoding": "gzip,deflate",
            "Content-Type": "application/json",
            "Host": "httpbin.org",
            "Header1": "override!",
            "Header2": "value2",
            "User-Agent": user_agent,
        }
 def test_http_auth_list(self):
     con = RequestsHttpConnection(http_auth=['username', 'secret'])
     self.assertEquals(('username', 'secret'), con.session.auth)