-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ruby 3.3 support and update test suite to use a local server (#216)
* Add test server to avoid external network calls * Update test suite to use the local server to run tests
- Loading branch information
1 parent
156ef96
commit 898599a
Showing
7 changed files
with
172 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'sinatra' | ||
require 'rack/auth/basic' | ||
|
||
helpers do | ||
def protected! | ||
return if authorized? | ||
|
||
response['WWW-Authenticate'] = %(Basic realm="Restricted Area") | ||
throw(:halt, [401, "Unauthorized access\nYou are denied access to this resource."]) | ||
end | ||
|
||
def authorized? | ||
@auth ||= Rack::Auth::Basic::Request.new(request.env) | ||
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == %w[guest guest] | ||
end | ||
end | ||
|
||
get '/' do | ||
"I'm Feeling Grovery" | ||
end | ||
|
||
get '/cookie_renderer' do | ||
cookie_info = request.cookies.map.with_index(1) do |(name, value), i| | ||
"#{i}. #{name} #{value}" | ||
end.join(', ') | ||
|
||
"Request contained #{request.cookies.size} cookies: #{cookie_info}" | ||
end | ||
|
||
get '/auth' do | ||
protected! | ||
'Your browser made it!' | ||
end | ||
|
||
get '/headers' do | ||
headers = | ||
request. | ||
env. | ||
select { |k, _v| k.start_with?('HTTP_') } | ||
headers_info = | ||
headers. | ||
map.with_index(1) { |(k, v), i| "#{i}. #{k.sub(/^HTTP_/, '').downcase.tr('_', '-')} #{v}" }. | ||
join(', ') | ||
|
||
"Request contained #{headers.size} headers: #{headers_info}" | ||
end | ||
|
||
get '/cat' do | ||
'<html><body><img src="/cat.png" alt="Cat"></body></html>' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'childprocess' | ||
require 'net/http' | ||
require 'logger' | ||
|
||
# | ||
# Simple control interface for the TestApp Sinatra server which hosts the various HTTP interfaces required to | ||
# test Grover | ||
# | ||
class TestServer | ||
class << self | ||
attr_reader :server | ||
|
||
def start | ||
@server ||= new # rubocop:disable Naming/MemoizedInstanceVariableName | ||
end | ||
|
||
def stop | ||
@server.stop | ||
@server = nil | ||
end | ||
end | ||
|
||
attr_reader :process, :read_io, :server_url, :logger | ||
|
||
SERVER_URL = 'http://localhost:4567' | ||
|
||
def initialize | ||
@logger = Logger.new($stdout) | ||
start | ||
logging_thread | ||
wait_until_started | ||
end | ||
|
||
def stop | ||
@process.stop | ||
@read_io.close | ||
@logging_thread.kill | ||
end | ||
|
||
private | ||
|
||
def start | ||
@process = ChildProcess.build('ruby', File.join(__dir__, 'test_app.rb')) | ||
@read_io, write_io = IO.pipe | ||
@process.io.stdout = write_io | ||
@process.io.stderr = write_io | ||
@process.start | ||
write_io.close | ||
end | ||
|
||
def logging_thread | ||
@logging_thread ||= Thread.new do | ||
loop { print @read_io.readpartial(8192) } | ||
rescue EOFError | ||
logger.warn 'Server process has terminated' | ||
end | ||
end | ||
|
||
def server_running? | ||
response = Net::HTTP.get_response(URI(SERVER_URL)) | ||
response.is_a?(Net::HTTPSuccess) | ||
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH | ||
logger.warn 'Server not running' | ||
false | ||
end | ||
|
||
def wait_until_started(max_attempts = 20) | ||
max_attempts.times do | ||
return if server_running? | ||
|
||
sleep 0.5 | ||
end | ||
raise 'Server failed to start' | ||
end | ||
end |