Skip to content

Commit

Permalink
Add a test for #370 [runtime] Cookies issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed May 25, 2012
1 parent d9828a6 commit 2e05cec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
Expand Down Expand Up @@ -1356,4 +1357,73 @@ public Object onCompleted(Response r) throws Exception {
}
}

@Test(timeOut = 60000, enabled = true)
public void testCookie() {
logger.info("{}: running test: testCookie", getClass().getSimpleName());

final CountDownLatch latch = new CountDownLatch(2);
final CountDownLatch suspended = new CountDownLatch(1);
final AtomicReference<Cookie> cookie = new AtomicReference<Cookie>();
atmoServlet.framework().addAtmosphereHandler(ROOT, new AbstractHttpAtmosphereHandler() {

AtomicBoolean b = new AtomicBoolean(false);

public void onRequest(AtmosphereResource event) throws IOException {
if (!b.getAndSet(true)) {
try {
event.suspend();
} finally {
suspended.countDown();
}
} else {
event.getBroadcaster().broadcast("foo");
}
}

public void onStateChange(AtmosphereResourceEvent event) throws IOException {
if (event.isResuming()) {
return;
}

cookie.set(event.getResource().getRequest().getCookies()[0]);

try {
event.getResource().getResponse().flushBuffer();
event.getResource().resume();
} finally {
latch.countDown();
}
}
}, BroadcasterFactory.getDefault().get(DefaultBroadcaster.class, "suspend"));

AsyncHttpClient c = new AsyncHttpClient();
try {
c.prepareGet(urlTarget).addCookie(new com.ning.http.client.Cookie("", "yo", "man", "", -1, false)).execute(new AsyncCompletionHandler<String>() {

@Override
public String onCompleted(Response response) throws Exception {
latch.countDown();
return null;
}
});

suspended.await(20, TimeUnit.SECONDS);
Response r = c.prepareGet(urlTarget).execute().get();

try {
latch.await(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}

assertNotNull(r);
assertEquals(r.getStatusCode(), 200);
assertEquals(cookie.get().getName(), "yo");
} catch (Exception e) {
logger.error("test failed", e);
fail(e.getMessage());
}
c.close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;


public class BlockingIOCometSupportTest extends BaseTest {
protected Server server;
protected Context root;
Expand Down Expand Up @@ -99,6 +98,4 @@ public void unsetAtmosphereHandler() throws Exception {
server.stop();
server = null;
}


}

0 comments on commit 2e05cec

Please sign in to comment.