当前位置: 首页>>代码示例>>C++>>正文


C++ HttpHeader::addField方法代码示例

本文整理汇总了C++中HttpHeader::addField方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpHeader::addField方法的具体用法?C++ HttpHeader::addField怎么用?C++ HttpHeader::addField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpHeader的用法示例。


在下文中一共展示了HttpHeader::addField方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: runCookieTest

static void runCookieTest(TestRunner& tr)
{
   tr.group("Http Cookie");

   tr.test("parse Set-Cookie header");
   {
      HttpHeader header;
      header.addField("Set-Cookie",
         "cookie1_name=cookie1_value; max-age=0; path=/");
      header.addField("Set-Cookie",
         "cookie2_name=cookie2_value; max-age=0; path=/; secure");
      header.addField("Set-Cookie",
         "cookie3_name=cookie3_value; max-age=0; path=/; secure");
      header.addField("Set-Cookie",
         "cookie4_name=cookie4_value; max-age=0; path=/moo");

      CookieJar jar;
      jar.readCookies(&header, CookieJar::Server);

      Cookie cookie1 = jar.getCookie("cookie1_name");
      Cookie cookie2 = jar.getCookie("cookie2_name");
      Cookie cookie3 = jar.getCookie("cookie3_name");
      Cookie cookie4 = jar.getCookie("cookie4_name");
      Cookie cookie5 = jar.getCookie("cookie5_name");

      assert(!cookie1.isNull());
      assert(!cookie2.isNull());
      assert(!cookie3.isNull());
      assert(!cookie4.isNull());
      assert(cookie5.isNull());

      assertStrCmp(cookie1["name"]->getString(), "cookie1_name");
      assertStrCmp(cookie2["name"]->getString(), "cookie2_name");
      assertStrCmp(cookie3["name"]->getString(), "cookie3_name");
      assertStrCmp(cookie4["name"]->getString(), "cookie4_name");

      assertStrCmp(cookie1["value"]->getString(), "cookie1_value");
      assertStrCmp(cookie2["value"]->getString(), "cookie2_value");
      assertStrCmp(cookie3["value"]->getString(), "cookie3_value");
      assertStrCmp(cookie4["value"]->getString(), "cookie4_value");

      assertStrCmp(cookie1["path"]->getString(), "/");
      assertStrCmp(cookie2["path"]->getString(), "/");
      assertStrCmp(cookie3["path"]->getString(), "/");
      assertStrCmp(cookie4["path"]->getString(), "/moo");

      assert(!cookie1["secure"]->getBoolean());
      assert(cookie2["secure"]->getBoolean());
      assert(cookie3["secure"]->getBoolean());
      assert(!cookie4["secure"]->getBoolean());
   }
   tr.passIfNoException();

   tr.test("overwrite Set-Cookie header");
   {
      HttpHeader header;
      header.addField(
         "Set-Cookie",
         "cookie1_name=cookie1_value; max-age=30; path=/");
      header.addField(
         "Set-Cookie",
         "cookie2_name=cookie2_value; max-age=30; path=/; secure");
      header.addField(
         "Set-Cookie",
         "cookie3_name=cookie3_value; max-age=30; path=/");

      CookieJar jar;
      jar.deleteCookie("cookie2_name", false);
      jar.setCookie("cookie4_name", "cookie4_value", 0, true, true);
      jar.writeCookies(&header, CookieJar::Server, true);

      string cookies;

      header.getField("Set-Cookie", cookies, 0);
      assertStrCmp(cookies.c_str(),
         "cookie2_name=; max-age=0; path=/");

      header.getField("Set-Cookie", cookies, 1);
      assertStrCmp(cookies.c_str(),
         "cookie4_name=cookie4_value; max-age=0; path=/; secure; HttpOnly");
   }
   tr.passIfNoException();

   tr.test("extend Set-Cookie header");
   {
      HttpHeader header;
      header.setField("Set-Cookie",
         "cookie1_name=cookie1_value; max-age=0; path=/, "
         "cookie2_name=cookie2_value; max-age=0; path=/; secure, "
         "cookie3_name=cookie3_value; max-age=0; path=/");

      CookieJar jar;
      jar.setCookie("cookie4_name", "cookie4_value", 0, true, false);
      jar.deleteCookie("cookie5_name", true);
      jar.writeCookies(&header, CookieJar::Server, false);

      string cookies;

      header.getField("Set-Cookie", cookies, 0);
      assertStrCmp(cookies.c_str(),
//.........这里部分代码省略.........
开发者ID:bsletten,项目名称:monarch,代码行数:101,代码来源:test-http.cpp


注:本文中的HttpHeader::addField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。