From 001ad1c290a89e6d3c7e4d66283197bd003f367a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 13 Mar 2015 23:46:43 +0000 Subject: extras/proxy_pass: support Unix domain sockets as backends Of course, some users will prefer to bind HTTP application servers to Unix domain sockets for better isolation and (maybe) better performance. --- test/test_extras_proxy_pass.rb | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'test') diff --git a/test/test_extras_proxy_pass.rb b/test/test_extras_proxy_pass.rb index 8842683..5bbce73 100644 --- a/test/test_extras_proxy_pass.rb +++ b/test/test_extras_proxy_pass.rb @@ -1,6 +1,7 @@ # Copyright (C) 2015 all contributors # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) require_relative 'server_helper' +require 'json' class TestExtrasProxyPass < Testcase ENV["N"].to_i > 1 and parallelize_me! @@ -34,6 +35,74 @@ class TestExtrasProxyPass < Testcase server_helper_teardown end + def test_unix_socket_no_path + tmpdir = Dir.mktmpdir + unix_path = "#{tmpdir}/proxy_pass.sock" + unix_srv = UNIXServer.new(unix_path) + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + host2, port2 = @srv2.addr[3], @srv2.addr[1] + pid = mkserver(cfg) do + @srv.autoclose = @srv2.autoclose = false + ENV["YAHNS_FD"] = "#{@srv.fileno},#{@srv2.fileno}" + $LOAD_PATH.unshift "#{Dir.pwd}/extras" + require 'proxy_pass' + cfg.instance_eval do + app(:rack, ProxyPass.new("unix:#{unix_path}:/$fullpath")) do + listen "#{host}:#{port}" + end + app(:rack, ProxyPass.new("unix:#{unix_path}:/foo$fullpath")) do + listen "#{host2}:#{port2}" + end + stderr_path err.path + end + end + + pid2 = mkserver(cfg, unix_srv) do + @srv.close + @srv2.close + cfg.instance_eval do + rapp = lambda do |env| + body = env.to_json + hdr = { + 'Content-Length' => body.bytesize.to_s, + 'Content-Type' => 'application/json', + } + [ 200, hdr, [ body ] ] + end + app(:rack, rapp) { listen unix_path } + stderr_path err.path + end + end + Net::HTTP.start(host, port) do |http| + res = http.request(Net::HTTP::Get.new('/f00')) + assert_equal 200, res.code.to_i + body = JSON.parse(res.body) + assert_equal '/f00', body['PATH_INFO'] + + res = http.request(Net::HTTP::Get.new('/f00foo')) + assert_equal 200, res.code.to_i + body = JSON.parse(res.body) + assert_equal '/f00foo', body['PATH_INFO'] + end + + Net::HTTP.start(host2, port2) do |http| + res = http.request(Net::HTTP::Get.new('/Foo')) + assert_equal 200, res.code.to_i + body = JSON.parse(res.body) + assert_equal '/foo/Foo', body['PATH_INFO'] + + res = http.request(Net::HTTP::Get.new('/Foofoo')) + assert_equal 200, res.code.to_i + body = JSON.parse(res.body) + assert_equal '/foo/Foofoo', body['PATH_INFO'] + end + ensure + quit_wait(pid) + quit_wait(pid2) + unix_srv.close if unix_srv + FileUtils.rm_rf(tmpdir) if tmpdir + end + def test_proxy_pass err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] host2, port2 = @srv2.addr[3], @srv2.addr[1] -- cgit v1.2.3-24-ge0c7