Commit 4dc7faf6 authored by Romain Courteaud's avatar Romain Courteaud

More tests

parent 11840882
import unittest
from urlchecker_db import LogDB
import peewee
class UrlCheckerNetworkTestCase(unittest.TestCase):
def setUp(self):
self.db = LogDB(":memory:")
def test_createTable(self):
assert self.db._db.pragma("user_version") == 0
self.db.createTables()
assert self.db._db.pragma("user_version") == 1
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(UrlCheckerDBTestCase))
return suite
if __name__ == "__main__":
unittest.main(defaultTest="suite")
This diff is collapsed.
This diff is collapsed.
...@@ -47,8 +47,11 @@ class LogDB: ...@@ -47,8 +47,11 @@ class LogDB:
transport = peewee.TextField() transport = peewee.TextField()
port = peewee.IntegerField() port = peewee.IntegerField()
state = peewee.TextField() state = peewee.TextField()
class Meta: class Meta:
primary_key = peewee.CompositeKey("status", "ip", "transport", "port") primary_key = peewee.CompositeKey(
"status", "ip", "transport", "port"
)
class DnsChange(BaseModel): class DnsChange(BaseModel):
status = peewee.ForeignKeyField(Status) status = peewee.ForeignKeyField(Status)
...@@ -62,6 +65,7 @@ class LogDB: ...@@ -62,6 +65,7 @@ class LogDB:
ip = peewee.TextField() ip = peewee.TextField()
url = peewee.TextField() url = peewee.TextField()
status_code = peewee.IntegerField() status_code = peewee.IntegerField()
class Meta: class Meta:
primary_key = peewee.CompositeKey("status", "ip", "url") primary_key = peewee.CompositeKey("status", "ip", "url")
......
...@@ -21,12 +21,7 @@ def getUserAgent(version): ...@@ -21,12 +21,7 @@ def getUserAgent(version):
) )
def request( def request(url, headers=None, session=requests, version=0):
url,
headers=None,
session=requests,
version=0
):
if headers is None: if headers is None:
headers = {} headers = {}
...@@ -99,19 +94,16 @@ def checkHttpStatus(db, status_id, url, ip, bot_version): ...@@ -99,19 +94,16 @@ def checkHttpStatus(db, status_id, url, ip, bot_version):
) )
session = requests.Session() session = requests.Session()
session.mount(base_url, ForcedIPHTTPSAdapter(dest_ip=ip)) session.mount(base_url, ForcedIPHTTPSAdapter(dest_ip=ip))
request_kw['session'] = session request_kw["session"] = session
ip_url = url ip_url = url
elif parsed_url.scheme == "http": elif parsed_url.scheme == "http":
# Force IP location # Force IP location
parsed_url = parsed_url._replace(netloc=ip) parsed_url = parsed_url._replace(netloc=ip)
ip_url = parsed_url.geturl() ip_url = parsed_url.geturl()
else: else:
raise NotImplementedError('Unhandled url: %s' % url) raise NotImplementedError("Unhandled url: %s" % url)
response = request( response = request(
ip_url, ip_url, headers={"Host": hostname}, version=bot_version, **request_kw
headers={"Host": hostname},
version=bot_version,
**request_kw
) )
logHttpStatus(db, ip, url, response.status_code, status_id) logHttpStatus(db, ip, url, response.status_code, status_id)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment