Labnotes

New in Zombie.js: DNS masking and port mapping

Published on

Two new features just landed in Zombie.js 2.0 to make testing easier: DNS masking and port mapping.

First is DNS masking, which helps you test against real domain names (instead of localhost everything), and supports testing sub-domains.

Let's say you want to test an application with the domain example.com and the sub-domains foo.example.com and bar.example.com. Just map that domain to localhost:

Browser.dns.localhost('*.example.com');

Go on and visit foo.example.com, it will send the request right to localhost.

This messes with the local DNS.lookup in Node.js, so any code that makes a request to example.com or one of its sub-domains will end up talking to localhost. It won't affect any other program running on your machine.

Without the asterisk, only example.com will map to localhost. And you can map as many domains as you want, you can even play with individual DNS records like:

Browser.dns.map('*.example.com', 'A', '127.0.0.1');    // IPv4
Browser.dns.map('*.example.com', 'AAAA', '::1');       // IPv6
Browser.dns.map('*.example.com', 'CNAME', 'localhost');
Browser.dns.map('example.com', 'MX', {
  exchange: 'localhost',
  priority: 10
});

Just in case you happen to be testing emails and want all emails sent to example.com to be caught by your server.

Port mapping allows you to talk to your Web server on port 80, even though the actual server is listening on an unprivileged port, like port 3000:

Browser.ports.map('*.example.com', 3000);

Even better, with one line you can tell Zombie that you want to use a root domain as the default hostname, but also support all its sub-domains, and map port 80 to your test port:

// Global setting, applies to all browser instances
Browser.localhost('*.example.com', 3000);

// Browser instance for this test
var browser = new Browser();
browser.visit('/path', function() {
  // It picks example.com as the default host
  browser.assert.url("http://example.com/path");
});

Available now in Zombie 2.0. Since it's still Alpha (I know, I know), you'll have to check out the yet-unfinished docs.