diff --git a/lib/reel/rack/server.rb b/lib/reel/rack/server.rb index 96fcd05..1e4e484 100644 --- a/lib/reel/rack/server.rb +++ b/lib/reel/rack/server.rb @@ -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 @@ -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 diff --git a/spec/reel/rack/server_spec.rb b/spec/reel/rack/server_spec.rb index 8571726..cfdd405 100644 --- a/spec/reel/rack/server_spec.rb +++ b/spec/reel/rack/server_spec.rb @@ -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 @@ -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