Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def build_request_headers(caller, if_none_match)
}

headers['forest_caller'] = caller.to_json if caller
headers['If-None-Match'] = %("#{if_none_match}") if if_none_match
headers['If-None-Match'] = if_none_match if if_none_match
headers
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module Utils
rpc_client.fetch_schema('/rpc/schema', if_none_match: 'abc123')

expect(faraday_connection).to have_received(:send) do |_method, _endpoint, _payload, headers|
expect(headers['If-None-Match']).to eq('"abc123"')
expect(headers['If-None-Match']).to eq('abc123')
end
end

Expand All @@ -136,17 +136,17 @@ module Utils
end

context 'with ETag in response' do
let(:response_headers) { { 'ETag' => '"etag123"' } }
let(:response_headers) { { 'ETag' => 'etag123' } }

it 'extracts ETag from response headers and strips quotes' do
it 'extracts ETag from response headers' do
result = rpc_client.fetch_schema('/rpc/schema')

expect(result.etag).to eq('etag123')
end
end

context 'with lowercase etag header' do
let(:response_headers) { { 'etag' => '"lowercase-etag"' } }
let(:response_headers) { { 'etag' => 'lowercase-etag' } }

it 'extracts etag from lowercase header' do
result = rpc_client.fetch_schema('/rpc/schema')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def handle_request(args)
'ETag matches, returning 304 Not Modified'
)
return { status: HTTP_NOT_MODIFIED, content: nil,
headers: { 'ETag' => quote_etag(agent.cached_schema_hash) } }
headers: { 'ETag' => agent.cached_schema_hash } }
end

# Get schema from cache (or build from datasource if not cached)
Expand All @@ -34,7 +34,7 @@ def handle_request(args)
{
status: HTTP_OK,
content: schema,
headers: { 'ETag' => quote_etag(etag) }
headers: { 'ETag' => etag }
}
end

Expand All @@ -45,26 +45,11 @@ def extract_if_none_match(args)
return nil unless request

# Get If-None-Match header (works for both Rails and Sinatra)
etag = if request.respond_to?(:get_header)
request.get_header('HTTP_IF_NONE_MATCH')
elsif request.respond_to?(:env)
request.env['HTTP_IF_NONE_MATCH']
end

# Strip quotes from ETag value if present
unquote_etag(etag)
end

def quote_etag(etag)
return nil unless etag

%("#{etag}")
end

def unquote_etag(etag)
return nil unless etag

etag.gsub(/\A"?|"?\z/, '')
if request.respond_to?(:get_header)
request.get_header('HTTP_IF_NONE_MATCH')
elsif request.respond_to?(:env)
request.env['HTTP_IF_NONE_MATCH']
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ module Routes

expect(result[:status]).to eq(200)
expect(result[:content]).to eq(cached_schema)
expect(result[:headers]['ETag']).to eq(%("#{cached_hash}"))
expect(result[:headers]['ETag']).to eq(cached_hash)
end

context 'when client provides matching If-None-Match header' do
let(:mock_request) do
instance_double(::Rack::Request, get_header: %("#{cached_hash}"))
instance_double(::Rack::Request, get_header: cached_hash)
end

before do
Expand All @@ -55,7 +55,7 @@ module Routes

expect(result[:status]).to eq(304)
expect(result[:content]).to be_nil
expect(result[:headers]['ETag']).to eq(%("#{cached_hash}"))
expect(result[:headers]['ETag']).to eq(cached_hash)
end

it 'logs debug message' do
Expand All @@ -67,15 +67,15 @@ module Routes

context 'when client provides non-matching If-None-Match header' do
let(:mock_request) do
instance_double(::Rack::Request, get_header: '"different_hash"')
instance_double(::Rack::Request, get_header: 'different_hash')
end

it 'returns the full schema with ETag header' do
result = route.handle_request({ request: mock_request })

expect(result[:status]).to eq(200)
expect(result[:content]).to eq(cached_schema)
expect(result[:headers]['ETag']).to eq(%("#{cached_hash}"))
expect(result[:headers]['ETag']).to eq(cached_hash)
end
end

Expand All @@ -97,7 +97,7 @@ module Routes
# Sinatra-style: responds to env but not get_header
# Using a Struct to simulate Sinatra request behavior
hash_value = cached_hash
Struct.new(:env).new({ 'HTTP_IF_NONE_MATCH' => %("#{hash_value}") })
Struct.new(:env).new({ 'HTTP_IF_NONE_MATCH' => hash_value })
end

it 'extracts If-None-Match from env and returns 304 when hash matches' do
Expand Down
Loading