cache_buster_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. include_examples 'makes_request'
  22. end
  23. context 'when specifying a secret header' do
  24. let(:secret_header) { 'X-Purge-Secret' }
  25. let(:secret) { SecureRandom.hex(20) }
  26. include_examples 'makes_request'
  27. end
  28. context 'when specifying a PURGE method' do
  29. let(:http_method) { 'purge' }
  30. context 'when not using headers' do
  31. include_examples 'makes_request'
  32. end
  33. context 'when specifying a secret header' do
  34. let(:secret_header) { 'X-Purge-Secret' }
  35. let(:secret) { SecureRandom.hex(20) }
  36. include_examples 'makes_request'
  37. end
  38. end
  39. end
  40. end