Repository: puma/puma
File: test/test_puma_server.rb (Ruby)

Code before:
    server_run do |env|
      body = lambda do |stream|
        stream.write("Hello World")
        stream.close
      end

      [200, {}, body]
    end

    data = send_http_and_read "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"

    assert_equal "Hello World", data.split("\r\n\r\n", 2).last
  end

  def test_request_logs_with_custom_logger
    conf = Puma::Configuration.new { |c| c.load 'test/config/custom_logger.rb' }
    conf.load

    server_run(custom_logger: conf.options[:custom_logger], log_requests: true) do |env|
      [200, {}, [env["SERVER_NAME"], "\n", env["SERVER_PORT"]]]
    end

    data = send_http_and_read "GET / HTTP/1.0\r\nHost: 123.123.123.123:456\r\n\r\n"
    assert_match(%r!Custom logging: 127\.0\.0\.1.*GET / HTTP/1\.0.*Host: 123\.123\.123\.123:456!, data)
    data = send_http_and_read "GET / HTTP/1.1\r\nHost: 123.123.123.123\r\n\r\n"
    assert_match(%r!Custom logging: 127\.0\.0\.1.*GET / HTTP/1\.1.*Host: 123\.123\.123\.123!, data)
  end

  def test_file_body
    random_bytes = SecureRandom.random_bytes(4096 * 32)

    tf = tempfile_create("test_file_body", random_bytes)

    server_run { |env| [200, {}, tf] }

    data = +''
    skt = send_http("GET / HTTP/1.1\r\nHost: [::ffff:127.0.0.1]:#{@port}\r\n\r\n")
    data << skt.sysread(65_536) while skt.wait_readable(0.1)

    ary = data.split("\r\n\r\n", 2)

    assert_equal random_bytes.bytesize, ary.last.bytesize
    assert_equal random_bytes, ary.last
  ensure
    tf.close
  end

  def test_file_to_path
    random_bytes = SecureRandom.random_bytes(4096 * 32)

    tf = tempfile_create("test_file_to_path", random_bytes)

Review comment (suggestion):
`send_http_and_read` reads the HTTP response, so you wont see the request logging there

I think the test in `test/test_integration_single.rb` should be sufficient 

Code after:
    server_run do |env|
      body = lambda do |stream|
        stream.write("Hello World")
        stream.close
      end

      [200, {}, body]
    end

    data = send_http_and_read "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"

    assert_equal "Hello World", data.split("\r\n\r\n", 2).last
  end

  def test_file_body
    random_bytes = SecureRandom.random_bytes(4096 * 32)

    tf = tempfile_create("test_file_body", random_bytes)

    server_run { |env| [200, {}, tf] }

    data = +''
    skt = send_http("GET / HTTP/1.1\r\nHost: [::ffff:127.0.0.1]:#{@port}\r\n\r\n")
    data << skt.sysread(65_536) while skt.wait_readable(0.1)

    ary = data.split("\r\n\r\n", 2)

    assert_equal random_bytes.bytesize, ary.last.bytesize
    assert_equal random_bytes, ary.last
  ensure
    tf.close
  end

  def test_file_to_path
    random_bytes = SecureRandom.random_bytes(4096 * 32)

    tf = tempfile_create("test_file_to_path", random_bytes)
    path = tf.path

    obj = Object.new
    obj.singleton_class.send(:define_method, :to_path) { path }
    obj.singleton_class.send(:define_method, :each) { path } # dummy, method needs to exist

    server_run { |env| [200, {}, obj] }

    data = +''
    skt = send_http("GET / HTTP/1.1\r\nHost: [::ffff:127.0.0.1]:#{@port}\r\n\r\n")
    data << skt.sysread(65_536) while skt.wait_readable(0.1)
    ary = data.split("\r\n\r\n", 2)

    assert_equal random_bytes.bytesize, ary.last.bytesize