yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
blob 513e574c522fec26b318b2ec6991b874f9f117db 2037 bytes (raw)
name: test/test_extras_proxy_pass.rb 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
# Copyright (C) 2015 all contributors <yahns-public@yhbt.net>
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'

class TestExtrasProxyPass < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper

  class ProxiedApp
    def call(env)
      h = [ %w(Content-Length 3), %w(Content-Type text/plain) ]
      case env['REQUEST_METHOD']
      when 'GET'
        [ 200, h, [ "hi\n"] ]
      when 'HEAD'
        [ 200, h, [] ]
      when 'PUT'
        buf = env['rack.input'].read
        [ 201, {
          'Content-Length' => buf.bytesize.to_s,
          'Content-Type' => 'text/plain',
          }, [ buf ] ]
      end
    end
  end

  def setup
    @srv2 = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
    server_helper_setup
  end

  def teardown
    @srv2.close if defined?(@srv2) && !@srv2.closed?
    server_helper_teardown
  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]
    pid = mkserver(cfg) do
      $LOAD_PATH.unshift "#{Dir.pwd}/extras"
      require 'proxy_pass'
      @srv2.close
      cfg.instance_eval do
        app(:rack, ProxyPass.new("http://#{host2}:#{port2}/")) do
          listen "#{host}:#{port}"
        end
        stderr_path err.path
      end
    end

    pid2 = mkserver(cfg, @srv2) do
      @srv.close
      cfg.instance_eval do
        app(:rack, ProxiedApp.new) do
          listen "#{host2}:#{port2}"
        end
        stderr_path err.path
      end
    end

    Net::HTTP.start(host, port) do |http|
      res = http.request(Net::HTTP::Get.new('/'))
      assert_equal 200, res.code.to_i
      n = res.body.bytesize
      assert_operator n, :>, 1
      res = http.request(Net::HTTP::Head.new('/'))
      assert_equal 200, res.code.to_i
      assert_equal n, res['Content-Length'].to_i
      assert_nil res.body

      res = http.put(Net::HTTP::Put.new('/'))
    end
  ensure
    quit_wait pid
    quit_wait pid2
  end
end

debug log:

solving 513e574 ...
found 513e574 in https://yhbt.net/yahns.git/

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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).