about summary refs log tree commit homepage
path: root/test/test_server.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-29 00:02:56 +0000
committerEric Wong <e@80x24.org>2013-10-29 02:37:05 +0000
commitfd5844b2baef6c1acaca9ca1eca0509e3a4f840d (patch)
tree2e9d746f699da0458903b4b54227a3574f774cb1 /test/test_server.rb
parent556f8ae6f629df51ff41c1918de40e2bb531159e (diff)
downloadyahns-fd5844b2baef6c1acaca9ca1eca0509e3a4f840d.tar.gz
This was documented (incorrectly) and not implemented for either
the master/worker or single process cases.  Implement and test
all (with mocks, so not fully-tested).
Diffstat (limited to 'test/test_server.rb')
-rw-r--r--test/test_server.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/test_server.rb b/test/test_server.rb
index e49c8ea..96dc546 100644
--- a/test/test_server.rb
+++ b/test/test_server.rb
@@ -460,4 +460,66 @@ class TestServer < Testcase
     quit_wait(pid)
     FileUtils.rm_rf(tmpdir)
   end
+
+  module MockSwitchUser
+    def self.included(cls)
+      cls.__send__(:remove_method, :switch_user)
+      cls.__send__(:alias_method, :switch_user, :mock_switch_user)
+    end
+
+    def mock_switch_user(user, group = nil)
+      $yahns_user = [ $$, user, group ]
+    end
+  end
+
+  def test_user_no_workers
+    refute defined?($yahns_user), "$yahns_user global should be undefined"
+    err = @err
+    cfg = Yahns::Config.new
+    host, port = @srv.addr[3], @srv.addr[1]
+    cfg.instance_eval do
+      ru = lambda do |_|
+        b = $yahns_user.inspect
+        [ 200, {'Content-Length'=>b.size.to_s }, [b] ]
+      end
+      GTL.synchronize { app(:rack, ru) { listen "#{host}:#{port}" } }
+      user "nobody"
+      stderr_path err.path
+    end
+    pid = mkserver(cfg) { Yahns::Server.__send__(:include, MockSwitchUser) }
+    expect = [ pid, "nobody", nil ].inspect
+    run_client(host, port) { |res| assert_equal expect, res.body }
+    refute defined?($yahns_user), "$yahns_user global should be undefined"
+  ensure
+    quit_wait(pid)
+  end
+
+  def test_user_workers
+    refute defined?($yahns_user), "$yahns_user global should be undefined"
+    err = @err
+    cfg = Yahns::Config.new
+    host, port = @srv.addr[3], @srv.addr[1]
+    cfg.instance_eval do
+      ru = lambda do |_|
+        b = $yahns_user.inspect
+        [ 200, {'Content-Length'=>b.size.to_s, 'X-Pid' => "#$$" }, [b] ]
+      end
+      GTL.synchronize { app(:rack, ru) { listen "#{host}:#{port}" } }
+      user "nobody"
+      stderr_path err.path
+      worker_processes 1
+    end
+    pid = mkserver(cfg) { Yahns::Server.__send__(:include, MockSwitchUser) }
+    run_client(host, port) do |res|
+      worker_pid = res["X-Pid"].to_i
+      assert_operator worker_pid, :>, 0
+      refute_equal pid, worker_pid
+      refute_equal $$, worker_pid
+      expect = [ worker_pid, "nobody", nil ].inspect
+      assert_equal expect, res.body
+    end
+    refute defined?($yahns_user), "$yahns_user global should be undefined"
+  ensure
+    quit_wait(pid)
+  end
 end