Sha256: 68652e2378435769c0824430cdffa57dce9caf8346ec533e544a21addc77009f

Contents?: true

Size: 1004 Bytes

Versions: 95

Compression:

Stored size: 1004 Bytes

Contents

require "tempfile"
require "thread"
require "webrick"

module Vagrant
  module Spec
    module Server
      # Starts a temporary web server that serves a document root.
      # This knows how to serve proper mime types for JSON so it can
      # be used for box metadata and such.
      #
      # @param [String] path The document root
      def with_web_server(path, **opts)
        tf = Tempfile.new("vagrant")
        tf.close

        opts[:json_type] ||= "application/json"

        mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
        mime_types.store "json", opts[:json_type]

        port   = opts[:port] || 3838
        server = WEBrick::HTTPServer.new(
          AccessLog: [],
          Logger: WEBrick::Log.new(tf.path, 7),
          Port: port,
          DocumentRoot: path.dirname.to_s,
          MimeTypes: mime_types)
        thr = Thread.new { server.start }
        yield port
      ensure
        server.shutdown rescue nil
        thr.join rescue nil
      end
    end
  end
end

Version data entries

95 entries across 28 versions & 1 rubygems

Version Path
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-spec-9bba7e122837/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/bundler/gems/vagrant-spec-03d88fe24677/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-abfc34474d12/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-spec-1d09951eaef3/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-b6b56c8b8135/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/bundler/gems/vagrant-spec-b6b56c8b8135/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.6.0/bundler/gems/vagrant-spec-abfc34474d12/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-148cb63d6d06/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-93c587f3c98a/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-148cb63d6d06/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-spec-1d09951eaef3/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-b6b56c8b8135/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-spec-9bba7e122837/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/bundler/gems/vagrant-spec-03d88fe24677/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-93c587f3c98a/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-spec-abfc34474d12/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/2.6.0/bundler/gems/vagrant-spec-abfc34474d12/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/bundler/gems/vagrant-spec-b6b56c8b8135/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-spec-9bba7e122837/lib/vagrant-spec/server.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.6.0/bundler/gems/vagrant-spec-abfc34474d12/lib/vagrant-spec/server.rb