Ejemplo n.º 1
0
 @Test
 public void testGetCookie() throws Exception {
   DefaultHttpRequest nettyRequest =
       new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
   String cookie1Name = "PREF";
   String cookie1Value = "ID=a95756377b78e75e:FF=0:TM=1392709628:LM=1392709628:S=a5mOVvTB7DBkexgi";
   String cookie1Domain = ".google.com";
   String cookie1Path = "/";
   String cookie1Header =
       cookie1Name
           + '='
           + cookie1Value
           + "; expires=Thu, 18-Feb-2016 07:47:08 GMT; path="
           + cookie1Path
           + "; domain="
           + cookie1Domain;
   nettyRequest.headers().add(HttpHeaders.Names.COOKIE, cookie1Header);
   HttpServerRequest<ByteBuf> request =
       new HttpServerRequest<ByteBuf>(nettyRequest, PublishSubject.<ByteBuf>create());
   Map<String, Set<Cookie>> cookies = request.getCookies();
   Assert.assertEquals("Unexpected number of cookies.", 1, cookies.size());
   Set<Cookie> cookies1 = cookies.get(cookie1Name);
   Assert.assertNotNull("No cookie found with name: " + cookie1Name, cookies1);
   Assert.assertEquals(
       "Unexpected number of cookies with name: " + cookie1Name, 1, cookies1.size());
   Cookie cookie = cookies1.iterator().next();
   Assert.assertEquals("Unexpected cookie name.", cookie1Name, cookie.getName());
   Assert.assertEquals("Unexpected cookie path.", cookie1Path, cookie.getPath());
 }
Ejemplo n.º 2
0
 private void route(HttpServerRequest request, List<PatternBinding> bindings) {
   for (PatternBinding binding : bindings) {
     Matcher m = binding.pattern.matcher(request.path);
     if (m.matches()) {
       Map<String, String> params = new HashMap<>(m.groupCount());
       if (binding.paramNames != null) {
         // Named params
         for (String param : binding.paramNames) {
           params.put(param, m.group(param));
         }
       } else {
         // Un-named params
         for (int i = 0; i < m.groupCount(); i++) {
           params.put("param" + i, m.group(i + 1));
         }
       }
       request.getParams().putAll(params);
       binding.handler.handle(request);
       return;
     }
   }
   // If we get here it wasn't routed
   request.response.statusCode = 404;
   request.response.end();
 }