Exemplo n.º 1
0
    def test_special_characters(self):
        user_agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
        botify = Botify(
            {
                "HTTP_HOST": "http://example.test",
                "PATH_INFO": "/Français",
                "HTTP_USER_AGENT": user_agent,
                "REMOTE_ADDR": "127.0.0.1",
            }
        )

        bot = "<ip>%s</ip><ua>%s</ua>" % (botify.ip, botify.user_agent)

        with nested(patch.object(Botify, "send_data"), patch.object(time, "time")) as (send_data_method, time_method):
            send_data_method.return_value = True
            current_time = 1319812537
            time_method.return_value = current_time

            botify.set_code(200)

            data = botify.record()
Exemplo n.º 2
0
    def test_format_data(self):
        user_agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
        botify = Botify(
            {
                "HTTP_HOST": "http://example.test",
                "PATH_INFO": "/example",
                "HTTP_USER_AGENT": user_agent,
                "REMOTE_ADDR": "127.0.0.1",
            }
        )

        bot = "<ip>%s</ip><ua>%s</ua>" % (botify.ip, botify.user_agent)

        with nested(patch.object(Botify, "send_data"), patch.object(time, "time")) as (send_data_method, time_method):
            send_data_method.return_value = True
            current_time = 1319812537
            time_method.return_value = current_time

            botify.set_code(200)

            data = botify.record()

            self.assertEquals(
                data,
                '<?xml version="2.0"?><crawl><load>0</load><code>200</code><time>%(time)s</time><url>%(url)s</url><api_key>%(api_key)s</api_key><bot>%(bot_id)s</bot><id>%(client_id)s</id></crawl>'
                % {
                    "time": current_time,
                    "url": urllib.quote(botify.get_url()),
                    "api_key": settings.BOTIFY_API_KEY,
                    "client_id": settings.BOTIFY_CLIENT_ID,
                    "bot_id": bot,
                },
            )

            botify.add_tracker("example")

            self.assertRaises(ValueError, botify.add_tracker, ({"test": "test"}))

            data = botify.record()

            self.assertEquals(
                data,
                '<?xml version="2.0"?><crawl><load>0</load><code>200</code><time>%(time)s</time><url>%(url)s</url><api_key>%(api_key)s</api_key><bot>%(bot_id)s</bot><id>%(client_id)s</id><trackers><t>example</t></trackers></crawl>'
                % {
                    "time": current_time,
                    "url": urllib.quote(botify.get_url()),
                    "api_key": settings.BOTIFY_API_KEY,
                    "client_id": settings.BOTIFY_CLIENT_ID,
                    "bot_id": bot,
                },
            )

            botify.canonical = "http://example.test/home"

            data = botify.record()

            self.assertEquals(
                data,
                '<?xml version="2.0"?><crawl><load>0</load><code>200</code><time>%(time)s</time><url>%(url)s</url><api_key>%(api_key)s</api_key><bot>%(bot_id)s</bot><id>%(client_id)s</id><trackers><t>example</t></trackers><canonical>http%%3A//example.test/home</canonical></crawl>'
                % {
                    "time": current_time,
                    "url": urllib.quote(botify.get_url()),
                    "api_key": settings.BOTIFY_API_KEY,
                    "client_id": settings.BOTIFY_CLIENT_ID,
                    "bot_id": bot,
                },
            )