Sha256: 88c1246b6827244e98c4de77cb0a7d1ed93be8c2953cc631ae4dc19008077e61

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require "test_helper"
require File.dirname(__FILE__) + "../../../lib/roger/rack/roger"

module Roger
  # Test Roger Server test
  class ServerTest < ::Test::Unit::TestCase
    def setup
      @project = Project.new(File.dirname(__FILE__) + "/../../project", rogerfile_path: false)
      @server = Server.new(@project)
      @host = "127.0.0.1"
    end

    def test_port_free
      port = 5192

      # Port is free
      assert @server.send(:port_free?, @host, port)

      s = TCPServer.new(@host, port)

      # Port is not free
      assert !@server.send(:port_free?, @host, port)
    ensure
      s.close
    end

    def test_free_port_for_host_above
      port = 9000

      # Make sure something is running on port
      begin
        s = TCPServer.new(@host, port)
      rescue SocketError, Errno::EADDRINUSE # rubocop:disable all
        # Something already must be running on port
      end

      next_port = @server.send(:free_port_for_host_above, @host, port)
      assert next_port > port

    ensure
      s.close
    end

    # Test to see if env["roger.project"] is set
    def test_env_roger_project_is_set
      test = Class.new do
        def initialize(_app)
        end

        def call(env)
          [200, {}, [env["roger.project"].object_id.to_s]]
        end
      end

      @server.use test

      request = ::Rack::MockRequest.new(@server.send(:application))

      # This is a bit of a clunky comparison but it suffices for now
      assert_equal @project.object_id.to_s, request.get("/").body
    end

    def test_application_options
      @server.application_options = { test_option: true }

      test = Class.new do
        def initialize(app)
          @app = app
        end

        def call(_env)
          [200, {}, [@app.options[:test_option].to_s]]
        end
      end

      @server.use test

      request = ::Rack::MockRequest.new(@server.send(:application))

      # This is a bit of a clunky comparison but it suffices for now
      assert_equal "true", request.get("/").body
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roger-1.8.0 test/unit/server_test.rb