Skip to content

Commit

Permalink
Incremental fix 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 a63c953 commit 681639e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -47,6 +48,7 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -302,7 +304,7 @@ public String getContextPath() {
*/
@Override
public Cookie[] getCookies() {
return b.request.getCookies();
return isNotNoOps() ? b.request.getCookies() : b.cookies.toArray(new Cookie[]{});
}

/**
Expand Down Expand Up @@ -922,6 +924,7 @@ public final static class Builder {
private int localPort = 0;
private boolean dispatchRequestAsynchronously;
private boolean destroyable = true;
private List<Cookie> cookies = new ArrayList<Cookie>();

private String contextPath = "";
private String serverName = "";
Expand All @@ -941,6 +944,11 @@ public Builder headers(Map<String, String> headers) {
return this;
}

public Builder cookies(List<Cookie> cookies) {
this.cookies = cookies;
return this;
}

public Builder dispatchRequestAsynchronously(boolean dispatchRequestAsynchronously) {
this.dispatchRequestAsynchronously = dispatchRequestAsynchronously;
return this;
Expand Down
93 changes: 93 additions & 0 deletions modules/cpr/src/test/java/org/atmosphere/cpr/CookieTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.atmosphere.cpr;


import org.atmosphere.container.BlockingIOCometSupport;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

public class CookieTest {

private AtmosphereFramework framework;
private AtmosphereConfig config;
private AsynchronousProcessor processor;
private final AtmosphereHandler handler = mock(AtmosphereHandler.class);

@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setAsyncSupport(new BlockingIOCometSupport(framework.getAtmosphereConfig()));
framework.init(new ServletConfig() {
@Override
public String getServletName() {
return "void";
}

@Override
public ServletContext getServletContext() {
return mock(ServletContext.class);
}

@Override
public String getInitParameter(String name) {
return null;
}

@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
});
config = framework.getAtmosphereConfig();
}


@Test
public void basicHandlerTest() throws IOException, ServletException, ExecutionException, InterruptedException {
final AtomicReference<Cookie> cValue = new AtomicReference<Cookie>();
final AtomicReference<AtmosphereResource> r = new AtomicReference<AtmosphereResource>();

framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

@Override
public void onRequest(AtmosphereResource resource) throws IOException {
r.set(resource);
resource.getBroadcaster().addAtmosphereResource(resource);
}

@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
Cookie[] c = event.getResource().getRequest().getCookies();
cValue.set(c[0]);
}

@Override
public void destroy() {
}
});
List<Cookie> c = new ArrayList<Cookie>();
c.add(new Cookie("yo", "man"));

AtmosphereRequest request = new AtmosphereRequest.Builder().cookies(c).pathInfo("/a").build();
framework.doCometSupport(request, AtmosphereResponse.create());

r.get().getBroadcaster().broadcast("yo").get();
assertNotNull(cValue.get());
assertEquals(c.get(0).getName(), cValue.get().getName());
assertEquals(c.get(0).getValue(), cValue.get().getValue());
}
}

0 comments on commit 681639e

Please sign in to comment.