Sha256: e780c93e4095fe1d81709067a18a11e8b657cc4221186d047df8ac802a646be2

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'test_helper'
require 'rack/test'
require './test/support/file_helper'

describe Rack::PageCaching do
  include Rack::Test::Methods
  include FileHelper

  def app
    path = cache_path
    rack_response = response
    Rack::Builder.new {
      use Rack::PageCaching,
        page_cache_directory: path,
        include_hostname: true
      run lambda { |env| rack_response }
    }.to_app
  end

  describe 'invalid response' do
    before do
      get 'http://www.fakeuri.org/hello', {},
        { 'rack.page_caching.perform_caching' => 'true' }
    end

    let(:response) do
      [
        404,
        { 'Content-Type' => 'text/html' },
        ['Not found']
      ]
    end

    it 'does not create a file' do
      assert_cache_folder_is_empty
    end
  end

  describe 'valid response' do
    before { get 'http://www.fakeuri.org/hello', {}, env }
    let(:env) { {} }
    let(:cache_file) { File.join(cache_path, 'www.fakeuri.org', 'hello.html') }
    let(:response) do
      [
        200,
        { 'Content-Type' => 'text/html' },
        ['Foo Bar']
      ]
    end

    describe 'request caching' do
      let(:env) { { 'rack.page_caching.perform_caching' => 'true' } }

      it 'creates a file after the request' do
        File.exist?(cache_file).must_equal true
        File.read(cache_file).must_equal 'Foo Bar'
      end

      describe 'compression' do
        let(:env) do
          {
            'rack.page_caching.perform_caching' => 'true',
            'rack.page_caching.compression' => Rack::PageCaching::Utils.gzip_level(:best_speed)
          }
        end

        it 'creates a compressed file after the request' do
          cache = File.join(cache_path, 'www.fakeuri.org', 'hello.html.gz')
          File.exist?(cache).must_equal true
          Zlib::GzipReader.open(cache) do |gz|
            gz.read.must_equal 'Foo Bar'
          end
        end
      end
    end

    it 'skips creation of file if caching is not needed' do
      refute File.exist?(cache_file)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-page_caching-0.0.3 test/rack/page_caching_spec.rb
rack-page_caching-0.0.2 test/rack/page_caching_spec.rb
rack-page_caching-0.0.1 test/rack/page_caching_spec.rb