From 8a03b5c5772036338d25239ac3be6cfde1f3d34c Mon Sep 17 00:00:00 2001 From: Brian Henderson Date: Sun, 8 Oct 2017 22:37:58 -0700 Subject: [PATCH] Add tests for #select_backend_config --- spec/invoker/power/url_rewriter_spec.rb | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/spec/invoker/power/url_rewriter_spec.rb b/spec/invoker/power/url_rewriter_spec.rb index cef8ed3..0347c74 100644 --- a/spec/invoker/power/url_rewriter_spec.rb +++ b/spec/invoker/power/url_rewriter_spec.rb @@ -46,6 +46,19 @@ expect(match[0]).to eq("hello-world") end + it "should match lots of dots" do + match = rewriter.extract_host_from_domain("a.b.c.d.dev") + expect(match).to_not be_empty + + expect(match[0]).to eq("a.b.c.d") + expect(match[1]).to eq("d") + end + + it "should not match foo.local" do + match = rewriter.extract_host_from_domain("foo.local") + expect(match).to be_empty + end + context 'user sets up a custom top level domain' do before(:all) do @original_invoker_config = Invoker.config @@ -66,5 +79,51 @@ Invoker.config = @original_invoker_config end end + + context '#select_backend_config' do + before(:all) do + @original_invoker_config = Invoker.dns_cache + @processes = [ + { label: 'foo', port: 1 }, + { label: 'api.foo', port: 2 }, + { label: 'bar.foo', port: 3 }, + ].map{ |p| OpenStruct.new(p) } + Invoker.config.stubs(:processes).returns(@processes) + Invoker.dns_cache = Invoker::DNSCache.new(nil) + end + + before do + def rewriter.dns_check(*args) + socket = StringIO.new + Invoker::IPC::DnsCheckCommand.new(socket).run_command(Invoker::IPC::Message::DnsCheck.new(*args)) + socket.rewind + Invoker::IPC.message_from_io socket + end + end + + after(:all) do + Invoker.dns_cache = @original_invoker_config + end + + it 'matches foo.dev' do + match = rewriter.select_backend_config('foo.dev') + expect(match.port).to eql(1) + end + + it 'matches api.foo.dev' do + match = rewriter.select_backend_config('api.foo.dev') + expect(match.port).to eql(2) + end + + it 'matches bar.foo.dev' do + match = rewriter.select_backend_config('bar.foo.dev') + expect(match.port).to eql(3) + end + + it 'matches baz.foo.dev' do + match = rewriter.select_backend_config('baz.foo.dev') + expect(match.port).to eql(1) + end + end end end