mirror of
				https://github.com/HoneyryderChuck/httpx.git
				synced 2025-11-04 00:01:41 -05:00 
			
		
		
		
	The reference for a request verb is now the string which is used everywhere else, instead of the symbol corresponding to it. This was an artifact from the import from httprb, and there is no advantage in it, since these strings are frozen in most use cases, and the transformations from symbol to strings being performed everywhere are prooof that keeping the atom isn't really bringing any benefit.
		
			
				
	
	
		
			112 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
# frozen_string_literal: true
 | 
						|
 | 
						|
require_relative "test_helper"
 | 
						|
require "httpx/plugins/response_cache/store"
 | 
						|
 | 
						|
class ResponseCacheStoreTest < Minitest::Test
 | 
						|
  include HTTPX
 | 
						|
 | 
						|
  def test_store_cache
 | 
						|
    request = request_class.new("GET", "http://example.com/")
 | 
						|
    response = cached_response(request)
 | 
						|
 | 
						|
    assert store.lookup(request) == response
 | 
						|
    assert store.cached?(request)
 | 
						|
 | 
						|
    request2 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    assert store.lookup(request2) == response
 | 
						|
 | 
						|
    request3 = request_class.new("POST", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    assert store.lookup(request3).nil?
 | 
						|
  end
 | 
						|
 | 
						|
  def test_store_error_status
 | 
						|
    request = request_class.new("GET", "http://example.com/")
 | 
						|
    _response = cached_response(request, status: 404)
 | 
						|
    assert !store.cached?(request)
 | 
						|
 | 
						|
    _response = cached_response(request, status: 410)
 | 
						|
    assert store.cached?(request)
 | 
						|
  end
 | 
						|
 | 
						|
  def test_store_no_store
 | 
						|
    request = request_class.new("GET", "http://example.com/")
 | 
						|
    _response = cached_response(request, extra_headers: { "cache-control" => "private, no-store" })
 | 
						|
    assert !store.cached?(request)
 | 
						|
  end
 | 
						|
 | 
						|
  def test_store_maxage
 | 
						|
    request = request_class.new("GET", "http://example.com/")
 | 
						|
    response = cached_response(request, extra_headers: { "cache-control" => "max-age=2" })
 | 
						|
    assert store.lookup(request) == response
 | 
						|
    sleep(3)
 | 
						|
    assert store.lookup(request).nil?
 | 
						|
 | 
						|
    request2 = request_class.new("GET", "http://example2.com/")
 | 
						|
    _response2 = cached_response(request2, extra_headers: { "cache-control" => "no-cache, max-age=2" })
 | 
						|
    assert store.lookup(request2).nil?
 | 
						|
  end
 | 
						|
 | 
						|
  def test_store_expires
 | 
						|
    request = request_class.new("GET", "http://example.com/")
 | 
						|
    response = cached_response(request, extra_headers: { "expires" => (Time.now + 2).httpdate })
 | 
						|
    assert store.lookup(request) == response
 | 
						|
    sleep(3)
 | 
						|
    assert store.lookup(request).nil?
 | 
						|
 | 
						|
    request2 = request_class.new("GET", "http://example2.com/")
 | 
						|
    _response2 = cached_response(request2, extra_headers: { "cache-control" => "no-cache", "expires" => (Time.now + 2).httpdate })
 | 
						|
    assert store.lookup(request2).nil?
 | 
						|
  end
 | 
						|
 | 
						|
  def test_prepare_vary
 | 
						|
    request = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    cached_response(request, extra_headers: { "vary" => "Accept" })
 | 
						|
 | 
						|
    request2 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/html" })
 | 
						|
    store.prepare(request2)
 | 
						|
    assert !request2.headers.key?("if-none-match")
 | 
						|
    request3 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    store.prepare(request3)
 | 
						|
    assert request3.headers.key?("if-none-match")
 | 
						|
    request4 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain", "user-agent" => "Linux Bowser" })
 | 
						|
    store.prepare(request4)
 | 
						|
    assert request4.headers.key?("if-none-match")
 | 
						|
  end
 | 
						|
 | 
						|
  def test_prepare_vary_asterisk
 | 
						|
    request = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    cached_response(request, extra_headers: { "vary" => "*" })
 | 
						|
 | 
						|
    request2 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/html" })
 | 
						|
    store.prepare(request2)
 | 
						|
    assert !request2.headers.key?("if-none-match")
 | 
						|
    request3 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain" })
 | 
						|
    store.prepare(request3)
 | 
						|
    assert request3.headers.key?("if-none-match")
 | 
						|
    request4 = request_class.new("GET", "http://example.com/", headers: { "accept" => "text/plain", "user-agent" => "Linux Bowser" })
 | 
						|
    store.prepare(request4)
 | 
						|
    assert !request4.headers.key?("if-none-match")
 | 
						|
  end
 | 
						|
 | 
						|
  private
 | 
						|
 | 
						|
  def request_class
 | 
						|
    @request_class ||= HTTPX.plugin(:response_cache).class.default_options.request_class
 | 
						|
  end
 | 
						|
 | 
						|
  def response_class
 | 
						|
    @response_class ||= HTTPX.plugin(:response_cache).class.default_options.response_class
 | 
						|
  end
 | 
						|
 | 
						|
  def store
 | 
						|
    @store ||= Plugins::ResponseCache::Store.new
 | 
						|
  end
 | 
						|
 | 
						|
  def cached_response(request, status: 200, extra_headers: {})
 | 
						|
    response = response_class.new(request, status, "2.0", { "date" => Time.now.httpdate, "etag" => "ETAG" }.merge(extra_headers))
 | 
						|
    store.cache(request, response)
 | 
						|
    response
 | 
						|
  end
 | 
						|
end
 |