Skip to content

Commit

Permalink
Merge branch 'main' into sean/jspecify
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis authored Dec 18, 2024
2 parents 649bbf4 + 6c6c812 commit febcbfc
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ Maven:
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
</dependency>
</dependencies>
```

Gradle:
```groovy
dependencies {
implementation 'org.asynchttpclient:async-http-client:3.0.0'
implementation 'org.asynchttpclient:async-http-client:3.0.1'
}
```

Expand Down
6 changes: 3 additions & 3 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client-project</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand All @@ -31,8 +31,8 @@
<javaModuleName>org.asynchttpclient.client</javaModuleName>

<jetty.version>11.0.24</jetty.version>
<tomcat.version>10.1.30</tomcat.version>
<commons-io.version>2.16.1</commons-io.version>
<tomcat.version>10.1.34</tomcat.version>
<commons-io.version>2.18.0</commons-io.version>
<mockito.version>4.11.0</mockito.version>
<hamcrest.version>3.0</hamcrest.version>
<kerby.version>2.1.0</kerby.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> h
if (!cookies.isEmpty()) {
RequestBuilder requestBuilder = request.toBuilder();
for (Cookie cookie : cookies) {
requestBuilder.addOrReplaceCookie(cookie);
requestBuilder.addCookieIfUnset(cookie);
}
request = requestBuilder.build();
}
Expand Down
21 changes: 18 additions & 3 deletions client/src/main/java/org/asynchttpclient/RequestBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ public T addCookie(Cookie cookie) {
* @return this
*/
public T addOrReplaceCookie(Cookie cookie) {
return maybeAddOrReplaceCookie(cookie, true);
}

/**
* Add a cookie based on its name, if it does not exist yet. Cookies that
* are already set will be ignored.
*
* @param cookie the new cookie
* @return this
*/
public T addCookieIfUnset(Cookie cookie) {
return maybeAddOrReplaceCookie(cookie, false);
}

private T maybeAddOrReplaceCookie(Cookie cookie, boolean allowReplace) {
String cookieKey = cookie.name();
boolean replace = false;
int index = 0;
Expand All @@ -335,10 +350,10 @@ public T addOrReplaceCookie(Cookie cookie) {

index++;
}
if (replace) {
cookies.set(index, cookie);
} else {
if (!replace) {
cookies.add(cookie);
} else if (allowReplace) {
cookies.set(index, cookie);
}
return asDerivedType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,8 @@ public boolean exitAfterHandlingRedirect(Channel channel, NettyResponseFuture<?>
CookieStore cookieStore = config.getCookieStore();
if (cookieStore != null) {
// Update request's cookies assuming that cookie store is already updated by Interceptors
List<Cookie> cookies = cookieStore.get(newUri);
if (!cookies.isEmpty()) {
for (Cookie cookie : cookies) {
requestBuilder.addOrReplaceCookie(cookie);
}
for (Cookie cookie : cookieStore.get(newUri)) {
requestBuilder.addCookieIfUnset(cookie);
}
}

Expand Down
34 changes: 34 additions & 0 deletions client/src/test/java/org/asynchttpclient/RequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,40 @@ public void testAddOrReplaceCookies() {
assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3");
}

@RepeatedIfExceptionsTest(repeats = 5)
public void testAddIfUnsetCookies() {
RequestBuilder requestBuilder = new RequestBuilder();
Cookie cookie = new DefaultCookie("name", "value");
cookie.setDomain("google.com");
cookie.setPath("/");
cookie.setMaxAge(1000);
cookie.setSecure(true);
cookie.setHttpOnly(true);
requestBuilder.addCookieIfUnset(cookie);
assertEquals(requestBuilder.cookies.size(), 1, "cookies size should be 1 after adding one cookie");
assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match");

Cookie cookie2 = new DefaultCookie("name", "value");
cookie2.setDomain("google2.com");
cookie2.setPath("/path");
cookie2.setMaxAge(1001);
cookie2.setSecure(false);
cookie2.setHttpOnly(false);

requestBuilder.addCookieIfUnset(cookie2);
assertEquals(requestBuilder.cookies.size(), 1, "cookies size should remain 1 as we just ignored cookie2 because of a cookie with same name");
assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match");

Cookie cookie3 = new DefaultCookie("name2", "value");
cookie3.setDomain("google.com");
cookie3.setPath("/");
cookie3.setMaxAge(1000);
cookie3.setSecure(true);
cookie3.setHttpOnly(true);
requestBuilder.addCookieIfUnset(cookie3);
assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3");
}

@RepeatedIfExceptionsTest(repeats = 5)
public void testSettingQueryParamsBeforeUrlShouldNotProduceNPE() {
RequestBuilder requestBuilder = new RequestBuilder();
Expand Down
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client-project</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<packaging>pom</packaging>

<name>AHC/Project</name>
Expand All @@ -45,11 +45,11 @@
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<netty.version>4.1.114.Final</netty.version>
<netty.version>4.1.115.Final</netty.version>
<netty.iouring>0.0.25.Final</netty.iouring>
<brotli4j.version>1.17.0</brotli4j.version>
<brotli4j.version>1.18.0</brotli4j.version>
<slf4j.version>2.0.16</slf4j.version>
<zstd-jni.version>1.5.6-7</zstd-jni.version>
<zstd-jni.version>1.5.6-8</zstd-jni.version>
<activation.version>2.0.1</activation.version>
<logback.version>1.5.12</logback.version>
<jetbrains-annotations.version>26.0.1</jetbrains-annotations.version>
Expand Down Expand Up @@ -337,7 +337,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.2</version>
<configuration>
<argLine>
@{argLine} --add-exports java.base/jdk.internal.misc=ALL-UNNAMED
Expand Down Expand Up @@ -368,10 +368,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
Expand Down

0 comments on commit febcbfc

Please sign in to comment.