cache_buster_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe CacheBuster do
  4. subject { described_class.new(secret_header: secret_header, secret: secret, http_method: http_method) }
  5. let(:secret_header) { nil }
  6. let(:secret) { nil }
  7. let(:http_method) { nil }
  8. let(:purge_url) { 'https://example.com/test_purge' }
  9. describe '#bust' do
  10. shared_examples 'makes_request' do
  11. it 'makes an HTTP purging request' do
  12. method = http_method&.to_sym || :get
  13. stub_request(method, purge_url).to_return(status: 200)
  14. subject.bust(purge_url)
  15. test_request = a_request(method, purge_url)
  16. test_request = test_request.with(headers: { secret_header => secret }) if secret && secret_header
  17. expect(test_request).to have_been_made.once
  18. end
  19. end
  20. context 'when using default options' do
  21. around do |example|
  22. # Disables the CacheBuster.new deprecation warning about default arguments.
  23. # Remove this `silence` block when default arg support is removed from CacheBuster
  24. Rails.application.deprecators[:mastodon].silence do
  25. example.run
  26. end
  27. end
  28. include_examples 'makes_request'
  29. end
  30. context 'when specifying a secret header' do
  31. let(:secret_header) { 'X-Purge-Secret' }
  32. let(:secret) { SecureRandom.hex(20) }
  33. include_examples 'makes_request'
  34. end
  35. context 'when specifying a PURGE method' do
  36. let(:http_method) { 'purge' }
  37. context 'when not using headers' do
  38. include_examples 'makes_request'
  39. end
  40. context 'when specifying a secret header' do
  41. let(:secret_header) { 'X-Purge-Secret' }
  42. let(:secret) { SecureRandom.hex(20) }
  43. include_examples 'makes_request'
  44. end
  45. end
  46. end
  47. end