Skip to content

Commit

Permalink
Test Puma::Acme::Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
danbernier committed Jul 26, 2024
1 parent 8e35a62 commit 8b4e627
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions puma-acme.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'minitest', '~> 5.14'
s.add_development_dependency 'minitest-mock_expectations', '~> 1.2'
s.add_development_dependency 'r509', '~> 1.0'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'vcr', '~> 6.1'
s.add_development_dependency 'webmock', '~> 3.19'
Expand Down
62 changes: 62 additions & 0 deletions test/puma/acme/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require_relative '../.././test_helper'
require "rack/test"

module Puma
module Acme
class MiddlewareTest < Minitest::Test
include ::Rack::Test::Methods

class App
def call(env)
req = ::Rack::Request.new(env)
[200, {}, ['request not handled by middleware']]
end
end

def test_it_returns_the_manager_answer_value_when_present
get '/.well-known/acme-challenge/good-token'
assert_equal '42', last_response.body
assert_equal 'text/plain;charset=utf-8', last_response.headers['content-type']
end

def test_it_passes_through_if_manager_provides_no_answer
get '/.well-known/acme-challenge/unknown-token'
assert_equal 'request not handled by middleware', last_response.body
end

def test_it_passes_unrecognized_requests_through
get '/'
assert_equal 'request not handled by middleware', last_response.body

get '/any-other/path'
assert_equal 'request not handled by middleware', last_response.body
end

class FakeManager
class Answer
def initialize(value)
@value = value
end
attr_reader :value
end

def answer(type:, token:)
if token == 'good-token'
Answer.new('42')
else
nil
end
end
end

def app
Puma::Acme::Middleware.new(
App.new,
manager: FakeManager.new
)
end
end
end
end

0 comments on commit 8b4e627

Please sign in to comment.