yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <yahns-public@yhbt.net>
To: yahns-public@yhbt.net
Subject: [PATCH] test_rack_env: additional test for Rack environment
Date: Wed, 26 Apr 2017 22:45:05 +0000	[thread overview]
Message-ID: <20170426224505.3757-1-yahns-public@yhbt.net> (raw)

Since there'll be some changes to accomodate the new parser,
ensure we prepare the Rack environment correctly.
---
 test/test_rack_env.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 test/test_rack_env.rb

diff --git a/test/test_rack_env.rb b/test/test_rack_env.rb
new file mode 100644
index 0000000..d337511
--- /dev/null
+++ b/test/test_rack_env.rb
@@ -0,0 +1,58 @@
+# Copyright (C) 2017 all contributors <yahns-public@yhbt.net>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+# frozen_string_literal: true
+require_relative 'server_helper'
+require 'rack'
+
+class TestRackEnv < Testcase
+  ENV["N"].to_i > 1 and parallelize_me!
+  include ServerHelper
+  alias setup server_helper_setup
+  alias teardown server_helper_teardown
+
+  def test_rack_env_logger
+    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+    cfg.instance_eval do
+      stderr_path err.path
+      GTL.synchronize do
+        app = Rack::Builder.new do
+          use Rack::Lint # ensure Lint passes
+          run(lambda do |env|
+            logger = env['rack.logger']
+            %w(SERVER_NAME SERVER_PORT rack.url_scheme).each do |k|
+              logger.info("#{k}=#{env[k].inspect}")
+            end
+            [ 200, [ %w(Content-Length 3), %w(Content Type text/plain)],
+             [ "OK\n" ] ]
+          end)
+        end
+        app(:rack, app.to_app) { listen "#{host}:#{port}" }
+      end
+    end
+    pid = mkserver(cfg)
+    Net::HTTP.start(host, port) do |http|
+      res = http.request(Net::HTTP::Get.new("/"))
+      assert_equal 200, res.code.to_i
+      assert_equal "OK\n", res.body
+      txt = File.read(err.path)
+      assert_match %r{\srack\.url_scheme=#{Regexp.escape('http'.inspect)}\s}s,
+                   txt
+      assert_match %r{\sSERVER_NAME=#{Regexp.escape(host.inspect)}\s}s, txt
+      assert_match %r{\sSERVER_PORT=#{Regexp.escape(port.to_s.inspect)}\s}s, txt
+    end
+    err.truncate 0
+    err.rewind
+    c = TCPSocket.new(host, port)
+    c.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
+    assert_match %r{\r\nOK\n\z}s, c.read
+    txt = File.read(err.path)
+    assert_match %r{\srack\.url_scheme=#{Regexp.escape('http'.inspect)}\s}s,
+                 txt
+    assert_match %r{\sSERVER_NAME=#{Regexp.escape('example.com'.inspect)}\s}s,
+                txt
+    assert_match %r{\sSERVER_PORT=#{Regexp.escape('80'.inspect)}\s}s, txt
+  ensure
+    c.close if c
+    quit_wait(pid)
+  end
+end
-- 
EW


                 reply	other threads:[~2017-04-26 22:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170426224505.3757-1-yahns-public@yhbt.net \
    --to=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).