def test_middleware_default_callback(self):
     from SnapSearch import Interceptor
     from SnapSearch.wsgi import InterceptorMiddleware
     a = DummyApp()
     i = Interceptor(self.client, self.detector)
     im = InterceptorMiddleware(a, i)
     cb = im.response_callback
     self.assertTrue(callable(cb))
     # bad response with dirty headers
     response = {
         'status':
         200,
         'headers': [
             ('name', 'value'),  # not a dict
             {
                 'name': 1,
             },  # no value
             {
                 'value': 1,
             },  # no name
             {},
         ],
         'html':
         ""
     }
     message = cb(response)
     self.assertTrue(isinstance(message, dict))
     self.assertTrue("headers" in message)
     pass  # void return
 def test_controller_default_callback(self):
     from SnapSearch import Interceptor
     from SnapSearch.cgi import InterceptorController
     i = Interceptor(self.client, self.detector)
     ic = InterceptorController(i)
     cb = ic.response_callback
     self.assertTrue(callable(cb))
     # bad response with dirty headers
     response = {
         'status':
         200,
         'headers': [
             ('name', 'value'),  # not a dict
             {
                 'name': 1,
             },  # no value
             {
                 'value': 1,
             },  # no name
             {},
         ],
         'html':
         ""
     }
     message = cb(response)
     self.assertTrue(isinstance(message, dict))
     self.assertTrue("headers" in message)
     pass  # void return
 def test_interceptor_init_callback(self):
     from SnapSearch import Interceptor
     cb_pre = lambda url: None
     cb_post = lambda url, resp: None
     i = Interceptor(self.client, self.detector, cb_pre, cb_post)
     self.assertEqual(i.before_intercept, cb_pre)
     self.assertEqual(i.after_intercept, cb_post)
     pass  # void return
 def test_interceptor_call_with_intercept(self):
     from SnapSearch import Interceptor
     i = Interceptor(self.client, self.detector)
     response = i(self.NORMAL_SITE_ENVIRON)
     self.assertTrue(isinstance(response, dict))
     self.assertTrue("status" in response)
     self.assertTrue(response["status"])  # may not be 200
     pass  # void return
 def test_interceptor_pre_callback(self):
     # pre-intercaption callback
     from SnapSearch import Interceptor
     cb_pre = lambda url: {'url': url}
     i = Interceptor(self.client, self.detector, cb_pre)
     response = i(self.NORMAL_SITE_ENVIRON)
     self.assertTrue(isinstance(response, dict))
     self.assertTrue("url" in response)
     self.assertEqual(response['url'], self.NORMAL_SITE_URL)
     pass  # void return
    def test_interceptor_post_callback(self):
        # post-intercaption callback
        from SnapSearch import Interceptor

        def cb_post(url, response):
            response['status'] = -1
            return None

        i = Interceptor(self.client, self.detector, None, cb_post)
        response = i(self.NORMAL_SITE_ENVIRON)
        self.assertEqual(response['status'], -1)
        pass  # void return
    def test_controller_call_normal(self):
        from SnapSearch import Interceptor
        from SnapSearch.api import response
        from SnapSearch.cgi import InterceptorController
        i = Interceptor(self.client, self.detector)

        from io import BytesIO
        old_stdout, sys.stdout = sys.stdout, BytesIO(b"Hello World!\r\n")
        ic = InterceptorController(i)
        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "Mozilla"
        self.assertFalse(ic.start(environ))
        ic.stop(False)
        sys.stdout = old_stdout
        pass  # void return
    def test_middleware_call_intercepted(self):
        from SnapSearch import Interceptor
        from SnapSearch.wsgi import InterceptorMiddleware
        a = DummyApp()
        i = Interceptor(self.client, self.detector)
        im = InterceptorMiddleware(a, i)

        def start_response(status, headers, exc_info=None):
            self.assertTrue(status)  # may not be b"200 OK"
            self.assertTrue(headers)

        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "AdsBot-Google"
        msg = im(environ, start_response)
        self.assertTrue(msg.startswith(b"<html"))
        pass  # void return
    def test_middleware_call_normal(self):
        from SnapSearch import Interceptor
        from SnapSearch.wsgi import InterceptorMiddleware
        a = DummyApp()
        i = Interceptor(self.client, self.detector)
        im = InterceptorMiddleware(a, i)

        def start_response(status, headers, exc_info=None):
            self.assertEqual(status, b"200 OK")
            self.assertTrue(headers)

        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "Mozilla"
        msg = im(environ, start_response)
        self.assertTrue(msg.startswith(b"Hello"))
        pass  # void return
    def test_controller_fallback_to_normal(self):
        from SnapSearch import Interceptor
        from SnapSearch.api import response
        from SnapSearch.cgi import InterceptorController
        i = Interceptor(self.client, self.detector)

        from io import BytesIO
        old_stdout, sys.stdout = sys.stdout, BytesIO(b"Hello World!\r\n")
        ic = InterceptorController(i)
        # invalidate the ``match` list in robots, and force detector to raise
        # an exception. this will trigger the ``except`` branch in ic.__call__
        # and force it to resume to non-intercepted mode
        ic.interceptor.detector.robots['match'] = None
        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "AdsBot-Google"
        self.assertFalse(ic.start(environ))
        ic.stop(False)
        sys.stdout = old_stdout
        pass  # void return
 def test_controller_init(self):
     from SnapSearch import Interceptor
     from SnapSearch.cgi import InterceptorController
     i = Interceptor(self.client, self.detector)
     cb = lambda r: {
         b"status":
         200,
         b"headers": [
             (b"Content-Type", b"text/html"),
             (b"Server", b"apache (CentOS)"),
         ],
         b"html":
         b"Hello World!\r\n",
     }
     ic = InterceptorController(i, cb)
     self.assertEqual(i, ic.interceptor)
     self.assertEqual(cb, ic.response_callback)
     self.assertRaises(AssertionError, ic.start)
     pass  # void return
 def test_middleware_init(self):
     from SnapSearch import Interceptor
     from SnapSearch.wsgi import InterceptorMiddleware
     a = DummyApp()
     i = Interceptor(self.client, self.detector)
     cb = lambda r: {
         b"status":
         200,
         b"headers": [
             (b"Content-Type", b"text/html"),
             (b"Server", b"apache (CentOS)"),
         ],
         b"html":
         b"Hello World!\r\n",
     }
     im = InterceptorMiddleware(a, i, cb)
     self.assertEqual(a, im.application)
     self.assertEqual(i, im.interceptor)
     self.assertEqual(cb, im.response_callback)
     pass  # void return
    def test_middleware_fallback_to_normal(self):
        from SnapSearch import Interceptor
        from SnapSearch.wsgi import InterceptorMiddleware
        a = DummyApp()
        i = Interceptor(self.client, self.detector)
        im = InterceptorMiddleware(a, i)

        def start_response(status, headers, exc_info=None):
            self.assertEqual(status, b"200 OK")
            self.assertTrue(headers)

        # invalidaate the ``match` list in robots, and force detector to raise
        # an exception. this will trigger the ``except`` branch in im.__call__
        # and force it to resume to non-intercepted mode
        im.interceptor.detector.robots['match'] = None
        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "AdsBot-Google"
        msg = im(environ, start_response)
        self.assertTrue(msg.startswith(b"Hello"))
        pass  # void return
    def test_controller_call_intercepted(self):
        from SnapSearch import Interceptor
        from SnapSearch.api import response
        from SnapSearch.cgi import InterceptorController
        i = Interceptor(self.client, self.detector)

        def cb(response_body):
            body = response._extract_message(response_body)
            self.assertTrue(isinstance(body, dict))
            self.assertTrue("html" in body)
            self.msg = body['html']
            return body

        from io import BytesIO
        old_stdout, sys.stdout = sys.stdout, BytesIO(b"Hello World!\r\n")
        ic = InterceptorController(i, cb)
        environ = self.NORMAL_SITE_ENVIRON.copy()
        environ['HTTP_USER_AGENT'] = "AdsBot-Google"
        self.assertTrue(ic.start(environ))
        self.assertTrue(self.msg.startswith(b"<html"))
        ic.stop(False)
        sys.stdout = old_stdout
        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())
 def test_interceptor_call_no_intercept(self):
     from SnapSearch import Interceptor
     i = Interceptor(self.client, self.detector)
     response = i(self.FIREFOX_REQUEST)
     self.assertEqual(response, None)
     pass  # void return
 def test_interceptor_init(self):
     from SnapSearch import Interceptor
     i = Interceptor(self.client, self.detector)
     self.assertEqual(i.client, self.client)
     self.assertEqual(i.detector, self.detector)
     pass  # void return