|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2021 Alexander Dahl |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <microhttpd.h> |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | +#include <memory> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "httpserver.hpp" |
| 29 | +#include "littletest.hpp" |
| 30 | + |
| 31 | +using std::shared_ptr; |
| 32 | +using std::sort; |
| 33 | +using std::string; |
| 34 | +using std::vector; |
| 35 | + |
| 36 | +using httpserver::http_request; |
| 37 | +using httpserver::http_resource; |
| 38 | +using httpserver::http_response; |
| 39 | +using httpserver::string_response; |
| 40 | + |
| 41 | +class simple_resource : public http_resource { |
| 42 | + public: |
| 43 | + const shared_ptr<http_response> render_GET(const http_request&) { |
| 44 | + return shared_ptr<string_response>(new string_response("OK")); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +LT_BEGIN_SUITE(http_resource_suite) |
| 49 | + void set_up() { |
| 50 | + } |
| 51 | + |
| 52 | + void tear_down() { |
| 53 | + } |
| 54 | +LT_END_SUITE(http_resource_suite) |
| 55 | + |
| 56 | +LT_BEGIN_AUTO_TEST(http_resource_suite, disallow_all_methods) |
| 57 | + simple_resource sr; |
| 58 | + sr.disallow_all(); |
| 59 | + auto allowed_methods = sr.get_allowed_methods(); |
| 60 | + LT_CHECK_EQ(allowed_methods.size(), 0); |
| 61 | +LT_END_AUTO_TEST(disallow_all_methods) |
| 62 | + |
| 63 | +LT_BEGIN_AUTO_TEST(http_resource_suite, allow_some_methods) |
| 64 | + simple_resource sr; |
| 65 | + sr.disallow_all(); |
| 66 | + sr.set_allowing(MHD_HTTP_METHOD_GET, true); |
| 67 | + sr.set_allowing(MHD_HTTP_METHOD_POST, true); |
| 68 | + auto allowed_methods = sr.get_allowed_methods(); |
| 69 | + LT_CHECK_EQ(allowed_methods.size(), 2); |
| 70 | + // elements in http_resource::method_state are sorted (std::map) |
| 71 | + vector<string> some_methods{MHD_HTTP_METHOD_GET, MHD_HTTP_METHOD_POST}; |
| 72 | + sort(some_methods.begin(), some_methods.end()); |
| 73 | + LT_CHECK_COLLECTIONS_EQ(allowed_methods.cbegin(), allowed_methods.cend(), |
| 74 | + some_methods.cbegin()) |
| 75 | +LT_END_AUTO_TEST(allow_some_methods) |
| 76 | + |
| 77 | +LT_BEGIN_AUTO_TEST(http_resource_suite, allow_all_methods) |
| 78 | + simple_resource sr; |
| 79 | + sr.allow_all(); |
| 80 | + auto allowed_methods = sr.get_allowed_methods(); |
| 81 | + // elements in http_resource::method_state are sorted (std::map) |
| 82 | + vector<string> all_methods{MHD_HTTP_METHOD_GET, MHD_HTTP_METHOD_POST, |
| 83 | + MHD_HTTP_METHOD_PUT, MHD_HTTP_METHOD_HEAD, MHD_HTTP_METHOD_DELETE, |
| 84 | + MHD_HTTP_METHOD_TRACE, MHD_HTTP_METHOD_CONNECT, |
| 85 | + MHD_HTTP_METHOD_OPTIONS, MHD_HTTP_METHOD_PATCH}; |
| 86 | + sort(all_methods.begin(), all_methods.end()); |
| 87 | + LT_CHECK_COLLECTIONS_EQ(allowed_methods.cbegin(), allowed_methods.cend(), |
| 88 | + all_methods.cbegin()) |
| 89 | +LT_END_AUTO_TEST(allow_all_methods) |
| 90 | + |
| 91 | +LT_BEGIN_AUTO_TEST_ENV() |
| 92 | + AUTORUN_TESTS() |
| 93 | +LT_END_AUTO_TEST_ENV() |
0 commit comments