Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Add SERVER_NAME and SERVER_PORT key to rack env. #18

Merged
merged 2 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/reel/rack/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def route_request(request)
:input => request.body.to_s,
"REMOTE_ADDR" => request.remote_addr
}.merge(convert_headers(request.headers))


normalize_env(options)

status, headers, body = app.call ::Rack::MockRequest.env_for(request.url, options)

if body.respond_to? :each
Expand Down Expand Up @@ -78,6 +80,26 @@ def convert_headers(headers)
}]
end

# Copied from lib/puma/server.rb
def normalize_env(env)
if host = env["HTTP_HOST"]
if colon = host.index(":")
env["SERVER_NAME"] = host[0, colon]
env["SERVER_PORT"] = host[colon+1, host.bytesize]
else
env["SERVER_NAME"] = host
env["SERVER_PORT"] = default_server_port(env)
end
else
env["SERVER_NAME"] = "localhost"
env["SERVER_PORT"] = default_server_port(env)
end
end

def default_server_port(env)
env['HTTP_X_FORWARDED_PROTO'] == 'https' ? 443 : 80
end

def status_symbol(status)
if status.is_a?(Fixnum)
Http::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym
Expand Down
20 changes: 18 additions & 2 deletions spec/reel/rack/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
let(:body) { "hello world" }
let(:uri) { URI("http://#{host}:#{port}/") }
let(:http) { Net::HTTP.new(uri.host, uri.port) }
let(:rack_app) { proc { [200, headers, [body]] } }

subject do
app = proc { [200, headers, [body]] }
described_class.new(Rack::Lint.new(Rack::Head.new(app)), :Host => host, :Port => port)
described_class.new(Rack::Lint.new(Rack::Head.new(rack_app)), :Host => host, :Port => port)
end

before do
Expand Down Expand Up @@ -53,4 +53,20 @@
expect(http.send_request('GET', uri.path, 'test')['transfer-encoding']).to eq 'chunked'
end
end

context "works with Rack::Builder" do
let(:rack_app) {
headers = {"Content-Type" => "text/plain"}
Rack::Builder.app {
map("/path1") { run proc { [200, headers.merge("Content-Length"=>"5"), ["path1"]] } }
run proc { [200, headers.merge("Content-Length"=>"3"), ["any"]] }
}
}

it "routes the requests both exact path and any path" do
expect(http.send_request('GET', "/path1", 'test').body).to eq "path1"
expect(http.send_request('GET', "/", 'test').body).to eq "any"
expect(http.send_request('GET', "/path2", 'test').body).to eq "any"
end
end
end