samuelcolvin / dnserver Goto Github PK
View Code? Open in Web Editor NEWSimple development DNS server written in python
Home Page: https://pypi.org/project/dnserver/
License: MIT License
Simple development DNS server written in python
Home Page: https://pypi.org/project/dnserver/
License: MIT License
I use dnserver in addition to pytest-httpserver to build a full fake network environment for unit tests.
pytest-httpserver can open a httpserver on a random port, and has utilities to control the server response within the unit tests, which is very convenient:
def test_my_client(httpserver):
httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}
I a unit test scenario, suppose I want to run a DNS SRV request to get the hostname and the port for a given service (for instance caldav), then simulate that service with pytest-httpserver.
I would
Having to rely on the port being free at the moment the test is running is far from ideal. Manually setting a port for a pytest-httpserver is quite cumbersome and makes you write a lot of fixtures.
For those reasons I think having a way to dynamically set the DNS responses while the server is running (in fixtures or directly in unit tests) would be great. I dream of something like this:
async def test_caldav(dnserver, httpserver):
dnserver.add_zone(host='caldav.example.com', type='A', answer='127.0.0.1')
dnserver.add_zone(host='_caldav._tcp.example.com', type='SRV', answer=[0, 1, httpserver.port, 'caldav.example.com.'])
httpserver.expect_request("/").respond_with_data("some caldav response")
resolver_kwargs = dict(nameservers=[dnserver.host], tcp_port=dnserver.port, udp_port=dnserver.port)
http_resolver = aiohttp.resolver.AsyncResolver(**resolver_kwargs)
dns_resolver = aiodns.DNSResolver(**resolver_kwargs)
dns_answer = dns_resolver.query(f"_caldav._tcp.example.com", "SRV")
caldav_url = f"http://{dns_answer[0].host}:{dns_answer[0].port}"
connector = aiohttp.TCPConnector(resolver=http_resolver)
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get(caldav_url) as result:
assert (await result.text()) == "some caldav response"
Though some of this might be out of scope for dnserver and better suited in a pytest-dnserver package.
Any thoughts?
I used your code, and the server works, but websites dont load
Once (an if) #2 is merged, this would be awesome to release the project on pypi so the DNSServer
class could be used in other projects unit tests.
What do you think?
Hi. Thank you again for your amazing work!
I can now use dnserver in unit tests ๐
@pytest.fixture(scope="session")
def dns_server():
from dnserver import DNSServer
server = DNSServer(
zones_file="tests/fixtures/zones.toml", port=DNS_PORT, upstream=None
)
server.start()
yield server
server.stop()
However I would like to be able to disable any upstream DNS server, so my unit tests won't make any network I/O. At the moment this is prevented by this line:
Line 125 in ef85e97
What do you think?
As a testing/development DNS server, it would be useful to allow some kind of "catch all". For example, it might be useful for dnserver to respond to a request for an A record with a specific IP address (e.g. 10.11.12.13) regardless of what domain was actually queried.
Use cases might be:
An example toml entry might be:
[[zones]]
host = '__CATCH_ALL'
type = 'A'
answer = '10.11.12.13'
Might be fun to see how hard it would be to implement DOH for the upstream/proxy. With that, you could use this as a local proxy like https://github.com/AdguardTeam/dnsproxy.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.