Sha256: 917ab0c6b4f9b6686c09a5a8880596068e43a73dffb9103715f2ed4ad19c1f4c

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

require 'sinatra/base'

class HttpTestServer < Sinatra::Base
  get "/method" do
    text 200, "GET"
  end

  post "/method" do
    text 200, "POST"
  end

  get "/status/:response_status" do
    if params[:response_status] =~ /\A\d+\z/
      status = params[:response_status]
      message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || "Unknown"
      text status.to_i, "#{status} #{message}"
    else
      text 500, "Invalid Status"
    end
  end

  get "/sleep/:seconds" do
    sleep params[:seconds].to_f
    $stderr.puts "sleeping #{params[:seconds]}"
    text 200, "Woke up after #{params.inspect} seconds"
  end

  get '/basic_auth' do
    requires_basic_auth!('admin', 'admin')
    "Welcome, authenticated client"
  end

  get '/basic_auth_without_password' do
    requires_basic_auth!('justadmin', '')
    "Welcome, authenticated client"
  end

  get '/basic_auth_without_user' do
    requires_basic_auth!('', 'justpassword')
    "Welcome, authenticated client"
  end

  get '/return_next_path_segment/:something' do
    params[:something]
  end

  def text(response_code, body, headers ={})
    [response_code, { "Content-Type" => "text/plain" }.merge(headers), body]
  end

  helpers do
    def requires_basic_auth!(user, pass)
      unless authorized?(user, pass)
        response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
        throw(:halt, [401, "Not authorized\n"])
      end
    end

    def authorized?(user, pass)
      @auth ||=  Rack::Auth::Basic::Request.new(request.env)
      @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [user, pass]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
litmus_paper-0.7.2 spec/support/http_test_server.rb
litmus_paper-0.7.1 spec/support/http_test_server.rb
litmus_paper-0.7.0 spec/support/http_test_server.rb
litmus_paper-0.6.3 spec/support/http_test_server.rb
litmus_paper-0.6.2 spec/support/http_test_server.rb
litmus_paper-0.6.1 spec/support/http_test_server.rb
litmus_paper-0.6.0 spec/support/http_test_server.rb