Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 3ebdc76

Browse files
Add httpOnly attribute
1 parent 4d22947 commit 3ebdc76

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/main/java/org/jscookie/AttributesDefinition.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public abstract class AttributesDefinition {
99
abstract String domain();
1010
public abstract AttributesDefinition secure( Boolean secure );
1111
abstract Boolean secure();
12+
public abstract AttributesDefinition httpOnly( Boolean httpOnly );
13+
abstract Boolean httpOnly();
1214
}

src/main/java/org/jscookie/Cookies.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public synchronized void set( String name, String value, AttributesDefinition at
136136
header.append( "; Secure" );
137137
}
138138

139+
Boolean httpOnly = attributes.httpOnly();
140+
if ( Boolean.TRUE.equals( httpOnly ) ) {
141+
header.append( "; HttpOnly" );
142+
}
143+
139144
if ( response.isCommitted() ) {
140145
return;
141146
}
@@ -400,6 +405,7 @@ public static class Attributes extends AttributesDefinition {
400405
private String path;
401406
private String domain;
402407
private Boolean secure;
408+
private Boolean httpOnly;
403409

404410
private Attributes() {}
405411

@@ -447,6 +453,16 @@ public Attributes secure( Boolean secure ) {
447453
return this;
448454
}
449455

456+
@Override
457+
Boolean httpOnly() {
458+
return httpOnly;
459+
}
460+
@Override
461+
public Attributes httpOnly( Boolean httpOnly ) {
462+
this.httpOnly = httpOnly;
463+
return this;
464+
}
465+
450466
private Attributes merge( AttributesDefinition reference ) {
451467
if ( reference.path() != null ) {
452468
path = reference.path();
@@ -460,6 +476,9 @@ private Attributes merge( AttributesDefinition reference ) {
460476
if ( reference.secure() != null ) {
461477
secure = reference.secure();
462478
}
479+
if ( reference.httpOnly() != null ) {
480+
httpOnly = reference.httpOnly();
481+
}
463482
return this;
464483
}
465484
}

src/test/java/org/jscookie/test/unit/CookiesWriteTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@ public void expires_attribute() {
8787
);
8888
Mockito.verify( response ).addHeader( "Set-Cookie", "c=v; Path=/; Expires=Sun, 07 Jun 2015 23:38:46 GMT" );
8989
}
90+
91+
@Test
92+
public void httponly_attribute() {
93+
cookies.set( "c", "v", Attributes.empty()
94+
.httpOnly( true )
95+
);
96+
Mockito.verify( response ).addHeader( "Set-Cookie", "c=v; Path=/; HttpOnly" );
97+
}
9098
}

0 commit comments

Comments
 (0)