Skip to content

Commit

Permalink
Use sessionRequest for wrapping HTTP stream instead of original Request.
Browse files Browse the repository at this point in the history
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 committed Sep 25, 2024
1 parent 04fd45d commit fbb3e12
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 fbb3e12

Please sign in to comment.