require File.expand_path('../../../../../spec_helper', __FILE__)
require 'net/http'
require File.expand_path('../fixtures/http_server', __FILE__)

describe "Net::HTTP#send_request" do
  before :each do
    NetHTTPSpecs.start_server
    @http = Net::HTTP.start("localhost", NetHTTPSpecs.port)

    # HEAD is special so handled separately
    @methods = %w[
      GET POST PUT DELETE
      OPTIONS
      PROPFIND PROPPATCH LOCK UNLOCK
    ]
  end

  after :each do
    @http.finish if @http.started?
    NetHTTPSpecs.stop_server
  end

  # TODO: Does only work with GET and POST requests
  describe "when passed type, path" do
    it "sends a HTTP Request of the passed type to the passed path" do
      response = @http.send_request("HEAD", "/request")
      response.body.should be_nil

      @methods.each do |method|
        response = @http.send_request(method, "/request")
        response.body.should == "Request type: #{method}"
      end
    end
  end

  describe "when passed type, path, body" do
    it "sends a HTTP Request with the passed body" do
      response = @http.send_request("HEAD", "/request/body", "test=test")
      response.body.should be_nil

      @methods.each do |method|
        response = @http.send_request(method, "/request/body", "test=test")
        response.body.should == "test=test"
      end
    end
  end

  describe "when passed type, path, body, headers" do
    it "sends a HTTP Request with the passed headers" do
      referer = 'https://www.ruby-lang.org/'.freeze

      response = @http.send_request("HEAD", "/request/header", "test=test", "referer" => referer)
      response.body.should be_nil

      @methods.each do |method|
        response = @http.send_request(method, "/request/header", "test=test", "referer" => referer)
        response.body.should include('"referer"=>["' + referer + '"]')
      end
    end
  end
end
