diff --git a/Gemfile b/Gemfile index 90353c7..aae9cad 100644 --- a/Gemfile +++ b/Gemfile @@ -5,17 +5,17 @@ source "https://rubygems.org" gemspec gem "capybara", "3.40.0" +gem "mutex_m", "0.2.0" gem "puma", "6.4.3" gem "pusher", "2.0.3" gem "rake", "13.2.1" gem "rspec", "3.13.0" -gem "rubocop", "1.66.1" +gem "rubocop", "1.68.0" gem "rubocop-capybara", "2.21.0" gem "rubocop-performance", "1.22.1" gem "rubocop-rake", "0.6.0" -gem "rubocop-rspec", "3.0.5" -gem "selenium-webdriver", "4.25.0" -gem "sinatra", "3.2.0" +gem "rubocop-rspec", "3.2.0" +gem "selenium-webdriver", "4.26.0" gem "yard", "0.9.37" group :test do diff --git a/spec/support/application.rb b/spec/support/application.rb index c1d0e6a..ad47da7 100644 --- a/spec/support/application.rb +++ b/spec/support/application.rb @@ -1,35 +1,61 @@ # frozen_string_literal: true -require "sinatra" -require "tilt/erb" - -module Sinatra - class Application - set(:root, proc { File.join(File.dirname(__FILE__), "application") }) - set(:views, proc { File.join(root, "views") }) - - disable :logging - - get "/" do - erb :index - end - - post "/pusher/auth" do - channel = Pusher[params[:channel_name]] - response = channel.authenticate(params[:socket_id], channel_data) - - MultiJson.dump(response) - end - - protected - - def channel_data - return unless /^presence-/.match?(params[:channel_name]) - - { - user_id: params[:socket_id], - user_info: { name: "Alan Turing" } - } +require "rack" + +module PusherFake + module Testing + class Application + def call(env) + request = Rack::Request.new(env) + + case request.path + when "/" + index + when "/pusher/auth" + authenticate(request.params) + when %r{\A/javascripts} + asset(request.path) + else + [404, {}, []] + end + end + + private + + def asset(path) + headers = { "content-type" => "text/javascript" } + root = File.join(File.dirname(__FILE__), "application") + body = File.read(File.join(root, "public", path)) + + [200, headers, [body]] + end + + def authenticate(params) + channel = Pusher[params["channel_name"]] + response = channel.authenticate(params["socket_id"], channel_data(params)) + headers = { "Content-Type" => "application/json" } + + [200, headers, [MultiJson.dump(response)]] + end + + def channel_data(params) + return unless /^presence-/.match?(params["channel_name"]) + + { + user_id: params["socket_id"], + user_info: { name: "Alan Turing" } + } + end + + def index + headers = { "content-type" => "text/html" } + root = File.join(File.dirname(__FILE__), "application") + template = File.read(File.join(root, "views", "index.erb")) + erb = ERB.new(template) + body = erb.result(binding) + + [200, headers, [body]] + end end end end diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index f9e5a94..1f17f38 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -2,6 +2,6 @@ require "capybara/rspec" -Capybara.app = Sinatra::Application +Capybara.app = PusherFake::Testing::Application.new Capybara.server = :puma, { Silent: true } Capybara.default_driver = :selenium_chrome_headless