ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

120 lines
3.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include <cppcms/service.h>
  9. #include <cppcms/application.h>
  10. #include <cppcms/applications_pool.h>
  11. #include <cppcms/http_request.h>
  12. #include <cppcms/http_cookie.h>
  13. #include <cppcms/http_response.h>
  14. #include <cppcms/http_context.h>
  15. #include <cppcms/json.h>
  16. #include <iostream>
  17. #include "client.h"
  18. #include "test.h"
  19. class unit_test : public cppcms::application {
  20. public:
  21. unit_test(cppcms::service &s) : cppcms::application(s)
  22. {
  23. }
  24. virtual void main(std::string /*test*/)
  25. {
  26. response().set_cookie(cppcms::http::cookie("normal","token"));
  27. response().set_cookie(cppcms::http::cookie("utf","\xD7\xA9\xD7\x9C\xD7\x95\xD7\x9D \xD7\xA9\xD7\x9C\xD7\x95\xD7\x9D"));
  28. typedef cppcms::http::request::cookies_type cookies_type;
  29. cookies_type cookies=request().cookies();
  30. for(cookies_type::iterator cookie=cookies.begin();cookie!=cookies.end();cookie++) {
  31. response().out()<<cookie->second.name()<<':'<<cookie->second.value()<<'\n';
  32. }
  33. }
  34. };
  35. void basic_test()
  36. {
  37. cppcms::http::cookie c("a","b");
  38. {
  39. std::ostringstream ss;
  40. ss << c;
  41. TEST(ss.str()=="Set-Cookie:a=b; Version=1");
  42. }
  43. {
  44. c.httponly(true);
  45. std::ostringstream ss;
  46. ss << c;
  47. TEST(ss.str()=="Set-Cookie:a=b; HttpOnly; Version=1");
  48. }
  49. {
  50. c.samesite_none(true);
  51. std::ostringstream ss;
  52. ss << c;
  53. TEST(ss.str()=="Set-Cookie:a=b; HttpOnly; SameSite=None; Version=1");
  54. }
  55. {
  56. c.samesite_lax(true);
  57. std::ostringstream ss;
  58. ss << c;
  59. TEST(ss.str()=="Set-Cookie:a=b; HttpOnly; SameSite=Lax; Version=1");
  60. }
  61. {
  62. c.samesite_strict(true);
  63. std::ostringstream ss;
  64. ss << c;
  65. TEST(ss.str()=="Set-Cookie:a=b; HttpOnly; SameSite=Strict; Version=1");
  66. }
  67. {
  68. c.httponly(false);
  69. c.samesite_strict(false);
  70. std::ostringstream ss;
  71. ss << c;
  72. TEST(ss.str()=="Set-Cookie:a=b; Version=1");
  73. }
  74. {
  75. c.max_age(10);
  76. std::ostringstream ss;
  77. ss << c;
  78. TEST(ss.str()=="Set-Cookie:a=b; Max-Age=10; Version=1");
  79. }
  80. {
  81. c.expires(1);
  82. std::ostringstream ss;
  83. ss << c;
  84. TEST(ss.str()=="Set-Cookie:a=b; Max-Age=10; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Version=1");
  85. }
  86. {
  87. std::ostringstream ss;
  88. c.browser_age();
  89. ss << c;
  90. TEST(ss.str()=="Set-Cookie:a=b; Version=1");
  91. }
  92. {
  93. c.expires(1);
  94. std::ostringstream ss;
  95. ss << c;
  96. TEST(ss.str()=="Set-Cookie:a=b; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Version=1");
  97. }
  98. }
  99. int main(int argc,char **argv)
  100. {
  101. try {
  102. basic_test();
  103. cppcms::service srv(argc,argv);
  104. srv.applications_pool().mount( cppcms::create_pool<unit_test>());
  105. srv.after_fork(submitter(srv));
  106. srv.run();
  107. }
  108. catch(std::exception const &e) {
  109. std::cerr << e.what() << std::endl;
  110. return EXIT_FAILURE;
  111. }
  112. return run_ok ? EXIT_SUCCESS : EXIT_FAILURE;
  113. }