Now tested with pytest

master
Stephane Bortzmeyer 3 years ago
parent b6cdf22be6
commit 6773e12737
  1. 164
      test_service.py

@ -0,0 +1,164 @@
#!/usr/bin/env python3
# Test by executing 'pytest'
# https://docs.pytest.org/
# https://requests.readthedocs.io/en/master/
import requests
PREFIX = 'https://dns.bortzmeyer.org/'
import re
formats = {'': 'text/html; charset=UTF-8', # Default
'HTML': 'text/html; charset=UTF-8',
'TXT': 'text/plain; charset=UTF-8',
'ZONE': 'text/dns',
'JSON': 'application/json',
'XML': 'application/xml'}
def fstring(f, add=False):
if f is None or f == '':
return ''
else:
if add:
return '&format=%s' % f
else:
return '?format=%s' % f
def sstring(f):
try:
(left, right) = formats[f].split(';')
return left
except ValueError:
return formats[f]
def test_basic():
r = requests.get(PREFIX)
assert r.status_code == 200 and r.headers['content-type'] == 'text/html; charset=UTF-8' and \
'No data was found' in r.text
def test_notexist():
r = requests.get(PREFIX + 'doesnotexistatall')
assert r.status_code == 404 and 'does not exist' in r.text
# TODO test unknown format
def test_defaulttype(): # ADDR by default
for format in formats:
r = requests.get(PREFIX + 'www.ietf.org/%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'104.20.0.85' in r.text, 'format is %s' % format
def test_addr():
for format in formats:
r = requests.get(PREFIX + 'www.ietf.org/ADDR%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'104.20.0.85' in r.text, 'format is %s' % format
def test_addr_v4():
for format in formats:
r = requests.get(PREFIX + 'www.ietf.org/A%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'104.20.0.85' in r.text, 'format is %s' % format
def test_addr_v6():
for format in formats:
r = requests.get(PREFIX + 'www.ietf.org/AAAA%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'2606:4700:10::6814:155' in r.text, 'format is %s' % format
def test_mx():
for format in formats:
r = requests.get(PREFIX + 'framasoft.org/MX%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'mail.framasoft.org' in r.text, 'format is %s' % format
def test_naptr():
for format in formats:
r = requests.get(PREFIX + 'canalplusoverseas.fr/NAPTR%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'_sip._tcp.canalplusoverseas.fr' in r.text, 'format is %s' % format
def test_txt():
for format in formats:
r = requests.get(PREFIX + 'fr/TXT%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'RRs processed' in r.text, 'format is %s' % format
def test_dnskey():
for format in formats:
r = requests.get(PREFIX + 'paypal.com/DNSKEY%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'paypal.com' in r.text, 'format is %s' % format # Unfortunately,
# no text in the DNSKEY output, is available for all
# formats. Use one test per format, as in test_dnssec?
def test_loc():
for format in formats:
r = requests.get(PREFIX + '52100.cp.bortzmeyer.fr/LOC%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'48' in r.text, 'format is %s' % format
def test_ns():
for format in formats:
r = requests.get(PREFIX + 'frmug.org/NS%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'ns.eu.org' in r.text, 'format is %s' % format
def test_srv():
for format in formats:
r = requests.get(PREFIX + '_sipfederationtls._tcp.bretagne-ouest.cci.bzh/SRV%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'sipfed.online.lync.com' in r.text, 'format is %s' % format
def test_uri():
for format in formats:
r = requests.get(PREFIX + '78000.cp.bortzmeyer.fr/URI%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'https://www.openstreetmap.org/' in r.text, 'format is %s' % format
# TODO an unformatted QTYPE such as TLSA
def test_rev_v4():
for format in formats:
r = requests.get(PREFIX + '194.0.9.1?reverse=1%s' % fstring(format, add=True))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'nic.fr' in r.text, 'format is %s' % format
def test_rev_v6():
for format in formats:
r = requests.get(PREFIX + '2001:678:c::1?reverse=1%s' % fstring(format, add=True))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'nic.fr' in r.text, 'format is %s' % format
def test_dnssec():
for format in formats:
r = requests.get(PREFIX + 'cyberstructure.fr?dodnssec=1%s' % fstring(format, add=True))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
(((format == 'HTML' or format == '' or format == 'TXT') and 'Authentic Data' in r.text) or
(format == 'ZONE' and 'Flags: ad' in r.text) or
(format == 'JSON' and '"AD": true' in r.text) or
(format == 'XML' and '<ad>1</ad>' in r.text) or
('Authentic data' in r.text)), 'format is %s' % format
# TODO other options (tcp, server, edns), for all formats
# TODO test several options together
# TODO non-existing QTYPE
def test_idn():
for format in formats:
r = requests.get(PREFIX + 'potamochère.fr/SOA%s' % fstring(format))
assert r.status_code == 200 and r.headers['content-type'] == formats[format] and \
'hostmaster.gandi.net' in r.text, 'format is %s' % format
def test_content_negotiation():
for format in formats:
if format == '':
continue
r = requests.get(PREFIX + 'www.ietf.org/ADDR', headers={'Accept': sstring(format)})
assert r.status_code == 200 and r.headers['content-type'].startswith(formats[format]) and \
'104.20.0.85' in r.text, 'format is %s' % format
Loading…
Cancel
Save