From 928d673a59363d72213e2022df89e91eda999289 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 7 Jun 2024 01:32:06 +0900 Subject: [PATCH 1/2] Add tests for multiple cookies. --- lib/rack/conform/application.rb | 3 ++- test/rack/conform/cookies.rb | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/rack/conform/application.rb b/lib/rack/conform/application.rb index 715bc55..0094107 100644 --- a/lib/rack/conform/application.rb +++ b/lib/rack/conform/application.rb @@ -41,7 +41,8 @@ def test_echo(env) end def test_cookies(env) - cookies = JSON.parse(env['rack.input'].read) + request = Rack::Request.new(env) + cookies = request.cookies Rack::Response.new.tap do |response| cookies.each do |key, value| diff --git a/test/rack/conform/cookies.rb b/test/rack/conform/cookies.rb index 661dc29..5d8dfe9 100644 --- a/test/rack/conform/cookies.rb +++ b/test/rack/conform/cookies.rb @@ -11,7 +11,7 @@ def body(headers) end it 'can respond with a single cookie' do - response = client.get("/cookies", {}, body({'a' => 1})) + response = client.get("/cookies", [['cookie', 'a=1']]) expect(response.status).to be == 200 expect(response.headers).to have_keys( @@ -21,8 +21,19 @@ def body(headers) response&.finish end -it 'can respond with multiple cookies' do - response = client.get("/cookies", {}, body({'a' => 1, 'b' => 2})) +it 'can respond with multiple combined cookies' do + response = client.get("/cookies", [['cookie', 'a=1;b=2']]) + + expect(response.status).to be == 200 + expect(response.headers).to have_keys( + 'set-cookie' => be == ["a=1", "b=2"] + ) +ensure + response&.finish +end + +it 'can respond with multiple cookie headers' do + response = client.get("/cookies", [['cookie', 'a=1'], ['cookie', 'b=2']]) expect(response.status).to be == 200 expect(response.headers).to have_keys( From 248a2d275573e51e2918853f3ee2f9bb83d848a5 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 7 Jun 2024 01:45:59 +0900 Subject: [PATCH 2/2] Also check the initial cookies header value. --- lib/rack/conform/application.rb | 4 ++++ test/rack/conform/cookies.rb | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/rack/conform/application.rb b/lib/rack/conform/application.rb index 0094107..541970c 100644 --- a/lib/rack/conform/application.rb +++ b/lib/rack/conform/application.rb @@ -45,6 +45,10 @@ def test_cookies(env) cookies = request.cookies Rack::Response.new.tap do |response| + # Hex encode non-printable characters: + value = env['HTTP_COOKIE'].gsub(/[^[:print:]]/, &:ord) + response.add_header('x-http-cookie', value) + cookies.each do |key, value| response.set_cookie(key, value) end diff --git a/test/rack/conform/cookies.rb b/test/rack/conform/cookies.rb index 5d8dfe9..b0092f2 100644 --- a/test/rack/conform/cookies.rb +++ b/test/rack/conform/cookies.rb @@ -15,6 +15,7 @@ def body(headers) expect(response.status).to be == 200 expect(response.headers).to have_keys( + 'x-http-cookie' => be == ['a=1'], 'set-cookie' => be == ['a=1'] ) ensure @@ -26,6 +27,7 @@ def body(headers) expect(response.status).to be == 200 expect(response.headers).to have_keys( + 'x-http-cookie' => be == ['a=1;b=2'], 'set-cookie' => be == ["a=1", "b=2"] ) ensure @@ -37,6 +39,7 @@ def body(headers) expect(response.status).to be == 200 expect(response.headers).to have_keys( + 'x-http-cookie' => be == ['a=1;b=2'], 'set-cookie' => be == ['a=1', 'b=2'] ) ensure