Skip to content

Commit

Permalink
Use sessionRequest for wrapping HTTP stream instead of original Reque…
Browse files Browse the repository at this point in the history
…st. (#12303)

Add SessionHandlerTest case to ensure that flushOnResponseCommit is not broken
in the future.

Signed-off-by: Robert B. Langer <[email protected]>
  • Loading branch information
robbie01 authored Sep 26, 2024
1 parent af3ac05 commit 8aa9c8b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
return false;

SessionRequest sessionRequest = new SessionRequest(request);
addSessionStreamWrapper(request);
addSessionStreamWrapper(sessionRequest);
return sessionRequest.process(next, response, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.session;

import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand All @@ -25,7 +26,9 @@
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Session;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -721,4 +724,70 @@ public void testUriParameterOnly() throws Exception
}
}

@Test
public void testFlushOnResponseCommit() throws Exception
{
// Setup temporary datastore with write-through null cache

File datastoreDir = MavenTestingUtils.getTargetTestingDir("datastore");
IO.delete(datastoreDir);

FileSessionDataStore datastore = new FileSessionDataStore();
datastore.setStoreDir(datastoreDir);

NullSessionCache cache = new NullSessionCache(_sessionHandler);
cache.setSessionDataStore(datastore);
cache.setFlushOnResponseCommit(true);

_sessionHandler.setSessionCache(cache);

_server.start();

LocalConnector.LocalEndPoint endPoint = _connector.connect();
endPoint.addInput("""
GET /create HTTP/1.1
Host: localhost
""");

HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
String setCookie = response.get(HttpHeader.SET_COOKIE);
String id = setCookie.substring(setCookie.indexOf("SESSION_ID=") + 11, setCookie.indexOf("; Path=/"));
String content = response.getContent();
assertThat(content, startsWith("Session="));

endPoint.addInput("""
GET /set/attribute/value HTTP/1.1
Host: localhost
Cookie: SESSION_ID=%s
""".formatted(id));

response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
assertThat(response.get(HttpHeader.SET_COOKIE), nullValue());
content = response.getContent();
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
assertThat(content, containsString("attribute = value"));

// Session should persist through restart
_server.stop();
_server.start();

endPoint.addInput("""
GET /set/attribute/value HTTP/1.1
Host: localhost
Cookie: SESSION_ID=%s
""".formatted(id));

response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
assertThat(response.get(HttpHeader.SET_COOKIE), nullValue());
content = response.getContent();
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
assertThat(content, containsString("attribute = value"));
}

}

0 comments on commit 8aa9c8b

Please sign in to comment.