local cjson = require("cjson.safe")
local dns_mock = {}
dns_mock.__index = dns_mock
dns_mock.__tostring = function(self)
local out = {
mocks_only = self.mocks_only,
records = {}
}
for i = 1, 33 do
out.records[i] = self[i] or {}
end
local json = assert(cjson.encode(out))
return json
end
local TYPE_A, TYPE_AAAA, TYPE_CNAME, TYPE_SRV = 1, 28, 5, 33
function dns_mock.new(options)
return setmetatable(options or {}, dns_mock)
end
function dns_mock:SRV(rec)
if self == dns_mock then
error("can't operate on the class, you must create an instance", 2)
end
if getmetatable(self or {}) ~= dns_mock then
error("SRV method must be called using the colon notation", 2)
end
assert(rec, "Missing record parameter")
local name = assert(rec.name, "No name field in SRV record")
self[TYPE_SRV] = self[TYPE_SRV] or {}
local query_answer = self[TYPE_SRV][name]
if not query_answer then
query_answer = {}
self[TYPE_SRV][name] = query_answer
end
table.insert(query_answer, {
type = TYPE_SRV,
name = name,
target = assert(rec.target, "No target field in SRV record"),
port = assert(rec.port, "No port field in SRV record"),
weight = rec.weight or 10,
ttl = rec.ttl or 600,
priority = rec.priority or 20,
class = rec.class or 1
})
return true
end
function dns_mock:A(rec)
if self == dns_mock then
error("can't operate on the class, you must create an instance", 2)
end
if getmetatable(self or {}) ~= dns_mock then
error("A method must be called using the colon notation", 2)
end
assert(rec, "Missing record parameter")
local name = assert(rec.name, "No name field in A record")
self[TYPE_A] = self[TYPE_A] or {}
local query_answer = self[TYPE_A][name]
if not query_answer then
query_answer = {}
self[TYPE_A][name] = query_answer
end
table.insert(query_answer, {
type = TYPE_A,
name = name,
address = assert(rec.address, "No address field in A record"),
ttl = rec.ttl or 600,
class = rec.class or 1
})
return true
end
function dns_mock:AAAA(rec)
if self == dns_mock then
error("can't operate on the class, you must create an instance", 2)
end
if getmetatable(self or {}) ~= dns_mock then
error("AAAA method must be called using the colon notation", 2)
end
assert(rec, "Missing record parameter")
local name = assert(rec.name, "No name field in AAAA record")
self[TYPE_AAAA] = self[TYPE_AAAA] or {}
local query_answer = self[TYPE_AAAA][name]
if not query_answer then
query_answer = {}
self[TYPE_AAAA][name] = query_answer
end
table.insert(query_answer, {
type = TYPE_AAAA,
name = name,
address = assert(rec.address, "No address field in AAAA record"),
ttl = rec.ttl or 600,
class = rec.class or 1
})
return true
end
function dns_mock:CNAME(rec)
if self == dns_mock then
error("can't operate on the class, you must create an instance", 2)
end
if getmetatable(self or {}) ~= dns_mock then
error("CNAME method must be called using the colon notation", 2)
end
assert(rec, "Missing record parameter")
local name = assert(rec.name, "No name field in CNAME record")
self[TYPE_CNAME] = self[TYPE_CNAME] or {}
local query_answer = self[TYPE_CNAME][name]
if not query_answer then
query_answer = {}
self[TYPE_CNAME][name] = query_answer
end
table.insert(query_answer, {
type = TYPE_CNAME,
name = name,
cname = assert(rec.cname, "No cname field in CNAME record"),
ttl = rec.ttl or 600,
class = rec.class or 1
})
return true
end
return dns_mock