Updates README, Fixes tests

This commit is contained in:
Nemo 2016-12-03 04:04:13 +05:30
parent 70ec73aebb
commit 152924eaaa
2 changed files with 8 additions and 62 deletions

View File

@ -1,36 +1,17 @@
# lightsaber [![Build Status](https://travis-ci.org/captn3m0/lightsaber.svg?branch=master)](https://travis-ci.org/captn3m0/lightsaber)
Lightsaber is a simple DNS Redirect service. It offers 301/302 redirects for
your domains. The configuration is kept public on this github repository itself.
your domains. The configuration is stored in TXT records.
## Usage
To add a DNS record, point your domain via a CNAME entry to `lightsaber.captnemo.in`.
You can also have fallthrough domain redirects
(thanks to @vivekprakash for the suggestion), by making the redirect a hash
with a `root` key set to the url prefix.
Then create a TXT record for the `_redirect` subdomain and use the following as the value:
```yaml
# This is a fallthrough redirect
# so t.co/ev would redirect to twitter.com/ev
t.co:
root: https://twitter.com
# All goo.gl routes will forward to google.com, and not use fallthrough
goo.gl: https://google.com
```
v=lr1;https://google.com
Next, you will need to do the following:
1. Fork this repo
2. Edit the redirects.yml file and add your redirect in the relevant section
3. File a Pull Request with your edit
Once the Pull Request is approved, the redirect will automatically be deployed
via Travis.
If you do not wish to make your domain redirects public, or give away your domain
names, you can self host this as well.
where https://google.com is the redirect URL.
## License

43
test.rb
View File

@ -6,48 +6,13 @@ require_relative './lightsaber'
class TestConfig < Minitest::Test
REDIRECTS = [301, 302]
def setup
@config = YAML::load_file 'redirects.yml'
end
def test_redirect_sections
@config.each do |code, zone|
assert REDIRECTS.include? code
end
end
def test_txt_record
saber = Lightsaber.new 'http://localhost:9292/test'
pp saber.get_response_from_dns 'captnemo.in'
res = saber.get_response_from_dns 'lstest.captnemo.in'
assert_equal 'https://google.com', res.headers['Location']
assert_equal 302, res.status
end
def test_each_domain
@config.each do |section, zone|
zone.each do |domain, redirect|
url = get_url(redirect, "")
refute_nil url, "Invalid YAML config for #{domain}"
assert resolves_to_lightsaber(domain),
"DNS for #{domain} isn't setup yet. See README"
end
end
end
def resolves_to_lightsaber(domain)
flag = domain === "lightsaber.captnemo.in"
Resolv::DNS.open do |dns|
records = dns.getresources domain, Resolv::DNS::Resource::IN::CNAME
records.each do |record|
flag||=record.name.to_s === "lightsaber.captnemo.in"
end
end
flag
end
def get_url(domain_object, rel_route)
if domain_object.is_a? Hash
return domain_object['root'] + "/" + rel_route
elsif domain_object.is_a? String
return domain_object
end
end
end