def test_client_call_missing_site_url(self):
     from SnapSearch import Client
     client = Client(self.api_email, self.api_key, {'test': 1})
     response = client(self.NON_EXISTENT_SITE_URL)
     self.assertTrue(isinstance(response, dict))
     self.assertEqual(response.get('status', None), 404)
     pass  # void return
 def test_client_call_bad_api_url(self):
     from SnapSearch import Client, error
     client = Client(self.api_email,
                     self.api_key, {'test': 1},
                     api_url=self.BAD_API_URL)
     self.assertRaises(error.SnapSearchConnectionError, client,
                       self.NORMAL_SITE_URL)
     pass  # void return
 def setUpClass(cls):
     from SnapSearch import Client, Detector
     cls.api_email, cls.api_key = _config.get_api_credentials()
     cls.client = Client(cls.api_email, cls.api_key, {'test': 1})
     cls.detector = Detector()
     cls.ADSBOT_GOOG_GET = json.loads(_config.DATA_ADSBOT_GOOG_GET)
     cls.FIREFOX_REQUEST = json.loads(_config.DATA_FIREFOX_REQUEST)
     cls.NORMAL_SITE_URL = "http://snapsearch.io/"
     pass  # void return
 def test_client_init_external_ca_path(self):
     from SnapSearch import Client, error
     # existing pem file
     client = Client(self.api_email,
                     self.api_key,
                     ca_path=self.EXTERNAL_CA_BUNDLE_PEM)
     # non-existent pem file
     self.assertRaises(error.SnapSearchError,
                       Client,
                       self.api_email,
                       self.api_key, {'test': 1},
                       ca_path=self.NON_EXISTENT_PEM)
     pass  # void return
 def test_client_init_external_api_url(self):
     # initialize with default arguments
     from SnapSearch import Client, error
     # https api url
     client = Client(self.api_email,
                     self.api_key, {'test': 1},
                     api_url=self.HTTPS_API)
     # non-https api url
     self.assertRaises(error.SnapSearchError,
                       Client,
                       self.api_email,
                       self.api_key, {'test': 1},
                       api_url=self.NON_HTTPS_API)
     pass  # void return
    def test_client_call_bad_response(self):
        # uses a dummy dispatch function to emulate a broken backend service
        # returning None as response, causing client.__call__() to raise error.
        from SnapSearch import Client, error
        client = Client(self.api_email, self.api_key, {'test': 1})

        def dummy_dispatch(**kwds):
            return None

        from SnapSearch import api
        old_dispatch = api.dispatch
        api.dispatch = dummy_dispatch
        self.assertRaises(error.SnapSearchError, client, self.NORMAL_SITE_URL)
        api.dispatch = old_dispatch
        pass  # void return
 def setUpClass(cls):
     from SnapSearch import Client, Detector
     cls.api_email, cls.api_key = _config.get_api_credentials()
     cls.client = Client(cls.api_email, cls.api_key, {'test': 1})
     cls.detector = Detector()
     cls.ADSBOT_GOOG_GET = json.loads(_config.DATA_ADSBOT_GOOG_GET)
     cls.FIREFOX_REQUEST = json.loads(_config.DATA_FIREFOX_REQUEST)
     cls.NORMAL_SITE_URL = "http://snapsearch.io/"
     cls.NORMAL_SITE_ENVIRON = {
         'HTTP_USER_AGENT': "AdsBot-Google",
         'SERVER_NAME': "snapsearch.io",
         'SERVER_PORT': "80",
         'SCRIPT_NAME': "/",
         'PATH_INFO': "",
         'REQUEST_METHOD': "GET",
         'SERVER_PROTOCOL': "HTTP/1.1",
         'QUERY_STRING': "",
         'GATEWAY_INTERFACE': "CGI/1.1",
         'HTTPS': "off",
     }
     pass  # void return
    def test_client_call_unknown_response(self):
        # uses a dummy dispatch function to emulate a broken backend service
        # returning unknown response code, causing client.__call__() to fail.
        from SnapSearch import Client
        from SnapSearch.api.response import Response
        client = Client(self.api_email, self.api_key, {'test': 1})

        def dummy_dispatch(**kwds):
            return Response(status=200,
                            headers=[(), ()],
                            body={
                                'code': "unknown",
                                'content': "unknown"
                            })

        from SnapSearch import api
        old_dispatch = api.dispatch
        api.dispatch = dummy_dispatch
        response = client(self.NORMAL_SITE_URL)
        self.assertEqual(response, None)
        api.dispatch = old_dispatch
        pass  # void return
 def test_client_init(self):
     # initialize with default arguments
     from SnapSearch import Client
     client = Client(self.api_email, self.api_key, {'test': 1})
     pass  # void return
 def test_client_call_validation_error(self):
     from SnapSearch import Client, error
     client = Client(self.api_email, self.api_key, {'test': 1})
     self.assertRaises(error.SnapSearchError, client, self.INVALID_SITE_URL)
     pass  # void return
def hello_world():
    msg = b"Hello World!"
    sys.stdout.write(b"Status: 200 OK\r\n")
    sys.stdout.write(b"Content-Type: text/html; charset=utf-8\r\n")
    sys.stdout.write(b"Content-Length: ")
    sys.stdout.write(bytes(len(msg)))
    sys.stdout.write(b"\r\n\r\n")
    sys.stdout.write(msg)
    sys.stdout.write(b"\r\n")
    return 0


if __name__ == '__main__':

    # load SnapSearch API credentials
    import os
    credentials = os.environ.get('SNAPSEARCH_API_CREDENTIALS', ":")
    api_email, sep, api_key = credentials.partition(":")

    # initialize the interceptor
    from SnapSearch import Client, Detector, Interceptor
    interceptor = Interceptor(Client(api_email, api_key), Detector())

    # deploy the interceptor
    from SnapSearch.cgi import InterceptorController
    InterceptorController(interceptor).start()

    # start servicing
    sys.exit(hello_world())