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

Code before:
      do_test_raise(VALID_REQUEST, 10, 20)
    end
  end

  def test_bad_client
    socket = do_test("GET /test HTTP/BAD", 3)
    assert_match "Bad Request", socket.read
    socket.close
  end

  def test_header_is_too_long
    long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
    assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
      do_test_raise(long, long.length/2, 10)
    end
  end

  def test_file_streamed_request
    body = "a" * (Puma::Const::MAX_BODY * 2)
    long = "GET /test HTTP/1.1\r\nContent-length: #{body.length}\r\nConnection: close\r\n\r\n" + body
    socket = do_test(long, (Puma::Const::CHUNK_SIZE * 2) - 400)
    assert_match "hello", socket.read
    socket.close
  end

  def test_unsupported_method
    socket = do_test("CONNECT www.zedshaw.com:443 HTTP/1.1\r\nConnection: close\r\n\r\n", 100)
    response = socket.read
    assert_match "Not Implemented", response
    socket.close
  end

  private

  def do_test(string, chunk)
    # Do not use instance variables here, because it needs to be thread safe
    socket = TCPSocket.new("127.0.0.1", @port);
    request = StringIO.new(string)
    chunks_out = 0

    while data = request.read(chunk)
      chunks_out += socket.write(data)
      socket.flush
    end
    socket
  end

  def do_test_raise(string, chunk, close_after = nil)
    # Do not use instance variables here, because it needs to be thread safe
    socket = TCPSocket.new("127.0.0.1", @port);
    request = StringIO.new(string)

Review comment (suggestion):
Re: my suggested change above, let's also add one additional test with a nonsense/non-existent HTTP method, like:

```do_test(ASDJLKQW www.zedshaw.com...)```

Code after:
      do_test_raise(VALID_REQUEST, 10, 20)
    end
  end

  def test_bad_client
    socket = do_test("GET /test HTTP/BAD", 3)
    assert_match "Bad Request", socket.read
    socket.close
  end

  def test_header_is_too_long
    long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
    assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
      do_test_raise(long, long.length/2, 10)
    end
  end

  def test_file_streamed_request
    body = "a" * (Puma::Const::MAX_BODY * 2)
    long = "GET /test HTTP/1.1\r\nContent-length: #{body.length}\r\nConnection: close\r\n\r\n" + body
    socket = do_test(long, (Puma::Const::CHUNK_SIZE * 2) - 400)
    assert_match "hello", socket.read
    socket.close
  end

  def test_unsupported_method
    socket = do_test("CONNECT www.zedshaw.com:443 HTTP/1.1\r\nConnection: close\r\n\r\n", 100)
    response = socket.read
    assert_match "Not Implemented", response
    socket.close
  end

  def test_nonexistent_method
    socket = do_test("FOOBARBAZ www.zedshaw.com:443 HTTP/1.1\r\nConnection: close\r\n\r\n", 100)
    response = socket.read
    assert_match "Not Implemented", response
    socket.close
  end

  private

  def do_test(string, chunk)
    # Do not use instance variables here, because it needs to be thread safe
    socket = TCPSocket.new("127.0.0.1", @port);
    request = StringIO.new(string)
    chunks_out = 0

    while data = request.read(chunk)
      chunks_out += socket.write(data)
      socket.flush
    end