Skip to content

Commit 0e96c1c

Browse files
authored
Merge pull request #48 from gordonbondon/httponly
Setting samesite and httponly
2 parents 1c52e74 + a72747c commit 0e96c1c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ Sets given values as cookie
572572
* :value `String`
573573
* :domain `String`
574574
* :expires `Integer`
575+
* :samesite `String`
576+
* :httponly `Boolean`
575577

576578
```ruby
577579
browser.cookies.set(name: "stealth", value: "omg", domain: "google.com") # => true

lib/ferrum/cookies.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def path
2323
@attributes["path"]
2424
end
2525

26+
def samesite
27+
@attributes["sameSite"]
28+
end
29+
2630
def size
2731
@attributes["size"]
2832
end
@@ -65,6 +69,9 @@ def set(name: nil, value: nil, **options)
6569
cookie[:value] ||= value
6670
cookie[:domain] ||= default_domain
6771

72+
cookie[:httpOnly] = cookie.delete(:httponly) if cookie.key?(:httponly)
73+
cookie[:sameSite] = cookie.delete(:samesite) if cookie.key?(:samesite)
74+
6875
expires = cookie.delete(:expires).to_i
6976
cookie[:expires] = expires if expires > 0
7077

spec/cookies_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ module Ferrum
2727
end
2828

2929
it "can set cookies with custom settings" do
30-
browser.cookies.set(name: "stealth", value: "omg", path: "/ferrum")
30+
browser.cookies.set(
31+
name: "stealth",
32+
value: "omg",
33+
path: "/ferrum",
34+
httponly: true,
35+
samesite: "None"
36+
)
3137

3238
browser.goto("/get_cookie")
3339
expect(browser.body).to_not include("omg")
@@ -36,6 +42,8 @@ module Ferrum
3642
expect(browser.body).to include("omg")
3743

3844
expect(browser.cookies["stealth"].path).to eq("/ferrum")
45+
expect(browser.cookies["stealth"].httponly?).to be_truthy
46+
expect(browser.cookies["stealth"].samesite).to eq("None")
3947
end
4048

4149
it "can remove a cookie" do

0 commit comments

Comments
 (0)