|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 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 | +#if defined(__MINGW32__) || defined(__CYGWIN32__) |
| 22 | +#define _WINDOWS |
| 23 | +#undef _WIN32_WINNT |
| 24 | +#define _WIN32_WINNT 0x600 |
| 25 | +#include <winsock2.h> |
| 26 | +#include <ws2tcpip.h> |
| 27 | +#else |
| 28 | +#include <arpa/inet.h> |
| 29 | +#endif |
| 30 | + |
| 31 | +#include "littletest.hpp" |
| 32 | +#include <curl/curl.h> |
| 33 | +#include <netinet/in.h> |
| 34 | +#include <sys/socket.h> |
| 35 | +#include "httpserver.hpp" |
| 36 | +#include <unistd.h> |
| 37 | +#include <signal.h> |
| 38 | + |
| 39 | +using namespace std; |
| 40 | +using namespace httpserver; |
| 41 | + |
| 42 | +size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) |
| 43 | +{ |
| 44 | + s->append((char*) ptr, size*nmemb); |
| 45 | + return size*nmemb; |
| 46 | +} |
| 47 | + |
| 48 | +static int counter = 0; |
| 49 | + |
| 50 | +ssize_t test_callback (char* buf, size_t max) |
| 51 | +{ |
| 52 | + if (counter == 2) |
| 53 | + { |
| 54 | + return -1; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + memset(buf, 0, max); |
| 59 | + strcat(buf, "test"); |
| 60 | + counter++; |
| 61 | + return strlen(buf); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class deferred_resource : public httpserver::http_resource |
| 66 | +{ |
| 67 | + public: |
| 68 | + const httpserver::http_response render_GET(const httpserver::http_request& req) |
| 69 | + { |
| 70 | + return httpserver::http_response_builder("cycle callback response").deferred_response(test_callback); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +LT_BEGIN_SUITE(deferred_suite) |
| 75 | + webserver* ws; |
| 76 | + |
| 77 | + void set_up() |
| 78 | + { |
| 79 | + ws = new webserver(create_webserver(8080)); |
| 80 | + ws->start(false); |
| 81 | + } |
| 82 | + |
| 83 | + void tear_down() |
| 84 | + { |
| 85 | + ws->stop(); |
| 86 | + delete ws; |
| 87 | + } |
| 88 | +LT_END_SUITE(deferred_suite) |
| 89 | + |
| 90 | +LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response) |
| 91 | + deferred_resource* resource = new deferred_resource(); |
| 92 | + ws->register_resource("base", resource); |
| 93 | + curl_global_init(CURL_GLOBAL_ALL); |
| 94 | + |
| 95 | + std::string s; |
| 96 | + CURL *curl = curl_easy_init(); |
| 97 | + CURLcode res; |
| 98 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 99 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 100 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 101 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 102 | + res = curl_easy_perform(curl); |
| 103 | + LT_ASSERT_EQ(res, 0); |
| 104 | + LT_CHECK_EQ(s, "testtest"); |
| 105 | + curl_easy_cleanup(curl); |
| 106 | +LT_END_AUTO_TEST(deferred_response) |
| 107 | + |
| 108 | +LT_BEGIN_AUTO_TEST_ENV() |
| 109 | + AUTORUN_TESTS() |
| 110 | +LT_END_AUTO_TEST_ENV() |
0 commit comments