PageRenderTime 82ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/whois/server_spec.rb

http://github.com/weppos/whois
Ruby | 302 lines | 274 code | 27 blank | 1 comment | 1 complexity | 2a7dd1932105c5bcd4947beaac4fbfdf MD5 | raw file
Possible License(s): MIT
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. describe Whois::Server do
  4. describe ".load_json" do
  5. it "loads a definition from a JSON file" do
  6. expect(File).to receive(:read).with("tld.json").and_return(<<~JSON)
  7. {
  8. "ae.org": {
  9. "host": "whois.centralnic.com"
  10. },
  11. "ar.com": {
  12. "host": "whois.centralnic.com"
  13. }
  14. }
  15. JSON
  16. with_definitions do
  17. described_class.load_json("tld.json")
  18. expect(described_class.definitions(:tld)).to eq([
  19. ["ae.org", "whois.centralnic.com", {}],
  20. ["ar.com", "whois.centralnic.com", {}],
  21. ])
  22. end
  23. end
  24. it "convert option keys to Symbol" do
  25. expect(File).to receive(:read).with("tld.json").and_return(<<~JSON)
  26. {
  27. "com": {
  28. "host": "whois.crsnic.net",
  29. "adapter": "verisign"
  30. }
  31. }
  32. JSON
  33. with_definitions do
  34. described_class.load_json("tld.json")
  35. expect(described_class.definitions(:tld)).to eq([
  36. ["com", "whois.crsnic.net", { adapter: "verisign" }],
  37. ])
  38. end
  39. end
  40. end
  41. describe ".definitions" do
  42. it "returns the definitions array for given type" do
  43. with_definitions do
  44. Whois::Server.define(Whois::Server::TYPE_TLD, "foo", "whois.foo")
  45. definition = described_class.definitions(Whois::Server::TYPE_TLD)
  46. expect(definition).to be_a(Array)
  47. expect(definition).to eq([["foo", "whois.foo", {}]])
  48. end
  49. end
  50. it "raises ArgumentError when the type is invalid" do
  51. with_definitions do
  52. expect {
  53. described_class.definitions(:foo)
  54. }.to raise_error(ArgumentError)
  55. end
  56. end
  57. end
  58. describe ".define" do
  59. it "adds a new definition with given arguments" do
  60. with_definitions do
  61. Whois::Server.define(Whois::Server::TYPE_TLD, "foo", "whois.foo")
  62. expect(described_class.definitions(Whois::Server::TYPE_TLD)).to eq([["foo", "whois.foo", {}]])
  63. end
  64. end
  65. it "accepts a hash of options" do
  66. with_definitions do
  67. Whois::Server.define(Whois::Server::TYPE_TLD, "foo", "whois.foo", foo: "bar")
  68. expect(described_class.definitions(Whois::Server::TYPE_TLD)).to eq([["foo", "whois.foo", { foo: "bar" }]])
  69. end
  70. end
  71. end
  72. describe ".factory" do
  73. it "returns an adapter initialized with given arguments" do
  74. server = Whois::Server.factory(:tld, "test", "whois.test")
  75. expect(server.type).to eq(:tld)
  76. expect(server.allocation).to eq("test")
  77. expect(server.host).to eq("whois.test")
  78. expect(server.options).to eq({})
  79. end
  80. it "returns a standard adapter by default" do
  81. server = Whois::Server.factory(:tld, "test", "whois.test")
  82. expect(server).to be_a(Whois::Server::Adapters::Standard)
  83. end
  84. it "accepts an :adapter option as Class and returns an instance of given adapter" do
  85. a = Class.new do
  86. attr_reader :args
  87. def initialize(*args)
  88. @args = args
  89. end
  90. end
  91. server = Whois::Server.factory(:tld, "test", "whois.test", adapter: a)
  92. expect(server).to be_a(a)
  93. expect(server.args).to eq([:tld, "test", "whois.test", {}])
  94. end
  95. it "accepts an :adapter option as Symbol or String, load Class and returns an instance of given adapter" do
  96. server = Whois::Server.factory(:tld, "test", "whois.test", adapter: :none)
  97. expect(server).to be_a(Whois::Server::Adapters::None)
  98. server = Whois::Server.factory(:tld, "test", "whois.test", adapter: "none")
  99. expect(server).to be_a(Whois::Server::Adapters::None)
  100. end
  101. it "deletes the adapter option" do
  102. server = Whois::Server.factory(:tld, "test", "whois.test", adapter: Whois::Server::Adapters::None, foo: "bar")
  103. expect(server.options).to eq({ foo: "bar" })
  104. end
  105. end
  106. describe ".guess" do
  107. it "recognizes tld" do
  108. server = Whois::Server.guess(".com")
  109. expect(server).to be_a(Whois::Server::Adapters::Base)
  110. expect(server.type).to eq(Whois::Server::TYPE_TLD)
  111. end
  112. it "recognizes domain" do
  113. server = Whois::Server.guess("example.com")
  114. expect(server).to be_a(Whois::Server::Adapters::Base)
  115. expect(server.type).to eq(Whois::Server::TYPE_TLD)
  116. end
  117. it "recognizes ipv4" do
  118. server = Whois::Server.guess("127.0.0.1")
  119. expect(server).to be_a(Whois::Server::Adapters::Base)
  120. expect(server.type).to eq(Whois::Server::TYPE_IPV4)
  121. end
  122. it "recognizes ipv6" do
  123. server = Whois::Server.guess("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
  124. expect(server).to be_a(Whois::Server::Adapters::Base)
  125. expect(server.type).to eq(Whois::Server::TYPE_IPV6)
  126. end
  127. it "recognizes ipv6 when zero groups" do
  128. server = Whois::Server.guess("2002::1")
  129. expect(server).to be_a(Whois::Server::Adapters::Base)
  130. expect(server.type).to eq(Whois::Server::TYPE_IPV6)
  131. end
  132. it "recognizes asn16" do
  133. server = Whois::Server.guess("AS23456")
  134. expect(server).to be_a(Whois::Server::Adapters::Base)
  135. expect(server.type).to eq(Whois::Server::TYPE_ASN16)
  136. end
  137. it "recognizes asn32" do
  138. server = Whois::Server.guess("AS131072")
  139. expect(server).to be_a(Whois::Server::Adapters::Base)
  140. expect(server.type).to eq(Whois::Server::TYPE_ASN32)
  141. end
  142. it "recognizes email" do
  143. expect {
  144. Whois::Server.guess("email@example.org")
  145. }.to raise_error(Whois::ServerNotSupported, /email/)
  146. end
  147. it "raises when unrecognized value" do
  148. expect {
  149. Whois::Server.guess("invalid")
  150. }.to raise_error(Whois::ServerNotFound)
  151. end
  152. context "when the input is a tld" do
  153. it "returns a IANA adapter" do
  154. expect(Whois::Server.guess(".com")).to eq(Whois::Server.factory(:tld, ".", "whois.iana.org"))
  155. end
  156. it "returns a IANA adapter when the input is an idn" do
  157. expect(Whois::Server.guess(".xn--fiqs8s")).to eq(Whois::Server.factory(:tld, ".", "whois.iana.org"))
  158. end
  159. end
  160. context "when the input is a domain" do
  161. it "lookups definitions and returns the adapter" do
  162. with_definitions do
  163. Whois::Server.define(:tld, "test", "whois.test")
  164. expect(Whois::Server.guess("example.test")).to eq(Whois::Server.factory(:tld, "test", "whois.test"))
  165. end
  166. end
  167. it "doesn't consider the dot as a regexp pattern" do
  168. with_definitions do
  169. Whois::Server.define(:tld, "no.com", "whois.no.com")
  170. Whois::Server.define(:tld, "com", "whois.com")
  171. expect(Whois::Server.guess("antoniocangiano.com")).to eq(Whois::Server.factory(:tld, "com", "whois.com"))
  172. end
  173. end
  174. it "returns the closer definition" do
  175. with_definitions do
  176. Whois::Server.define(:tld, "com", com = "whois.com")
  177. Whois::Server.define(:tld, "com.foo", comfoo = "whois.com.foo")
  178. Whois::Server.define(:tld, "foo.com", foocom = "whois.foo.com")
  179. expect(Whois::Server.guess("example.com").host).to eq(com)
  180. expect(Whois::Server.guess("example.com.foo").host).to eq(comfoo)
  181. expect(Whois::Server.guess("example.foo.com").host).to eq(foocom)
  182. end
  183. end
  184. end
  185. context "when the input is an asn16" do
  186. it "lookups definitions and returns the adapter" do
  187. with_definitions do
  188. Whois::Server.define(:asn16, "0 65535", "whois.test")
  189. expect(Whois::Server.guess("AS65535")).to eq(Whois::Server.factory(:asn16, "0 65535", "whois.test"))
  190. end
  191. end
  192. it "raises if definition is not found" do
  193. with_definitions do
  194. Whois::Server.define(:asn16, "0 60000", "whois.test")
  195. expect {
  196. Whois::Server.guess("AS65535")
  197. }.to raise_error(Whois::AllocationUnknown)
  198. end
  199. end
  200. end
  201. context "when the input is an asn32" do
  202. it "lookups definitions and returns the adapter" do
  203. with_definitions do
  204. Whois::Server.define(:asn32, "65536 394239", "whois.test")
  205. expect(Whois::Server.guess("AS65536")).to eq(Whois::Server.factory(:asn32, "65536 394239", "whois.test"))
  206. end
  207. end
  208. it "raises if definition is not found" do
  209. with_definitions do
  210. Whois::Server.define(:asn32, "65536 131071", "whois.test")
  211. expect {
  212. Whois::Server.guess("AS200000")
  213. }.to raise_error(Whois::AllocationUnknown)
  214. end
  215. end
  216. end
  217. context "when the input is a ipv4" do
  218. it "lookups definitions and returns the adapter" do
  219. with_definitions do
  220. Whois::Server.define(:ipv4, "192.168.1.0/10", "whois.test")
  221. expect(Whois::Server.find_for_ip("192.168.1.1")).to eq(Whois::Server.factory(:ipv4, "192.168.1.0/10", "whois.test"))
  222. end
  223. end
  224. it "raises if definition is not found" do
  225. with_definitions do
  226. Whois::Server.define(:ipv4, "192.168.1.0/10", "whois.test")
  227. expect {
  228. Whois::Server.guess("192.192.0.1")
  229. }.to raise_error(Whois::AllocationUnknown)
  230. end
  231. end
  232. end
  233. context "when the input is a ipv6" do
  234. it "lookups definitions and returns the adapter" do
  235. with_definitions do
  236. Whois::Server.define(:ipv6, "2001:0200::/23", "whois.test")
  237. expect(Whois::Server.guess("2001:0200::1")).to eq(Whois::Server.factory(:ipv6, "2001:0200::/23", "whois.test"))
  238. end
  239. end
  240. it "raises if definition is not found" do
  241. with_definitions do
  242. Whois::Server.define(:ipv6, "::1", "whois.test")
  243. expect {
  244. Whois::Server.guess("2002:0300::1")
  245. }.to raise_error(Whois::AllocationUnknown)
  246. end
  247. end
  248. it "recognizes ipv4 compatibility mode" do
  249. with_definitions do
  250. Whois::Server.define(:ipv6, "::192.168.1.1", "whois.test")
  251. expect(Whois::Server.guess("::192.168.1.1")).to eq(Whois::Server.factory(:ipv6, "::192.168.1.1", "whois.test"))
  252. end
  253. end
  254. it "rescues IPAddr ArgumentError", issue: "weppos/whois#174" do
  255. with_definitions do
  256. expect {
  257. Whois::Server.guess("f53")
  258. }.to raise_error(Whois::AllocationUnknown)
  259. end
  260. end
  261. end
  262. end
  263. end