libtasks Documentation  1.6
test_http_sender.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #include "test_http_sender.h"
9 
10 #include <iostream>
11 #include <tasks/net/acceptor.h>
12 
13 int g_status_code = 0;
14 std::string g_content_type;
15 std::atomic<bool> g_done(false);
16 
17 bool test_handler::handle_response(std::shared_ptr<tasks::net::http_response> response) {
18  g_status_code = response->status_code();
19  g_content_type = response->header("Content-Type");
20  return false;
21 }
22 
23 bool test_handler_keepalive::handle_response(std::shared_ptr<tasks::net::http_response> response) {
24  g_status_code = response->status_code();
25  g_content_type = response->header("Content-Type");
26  g_done = true;
27  return true;
28 }
29 
31  auto* sender = new tasks::net::http_sender<test_handler>();
32 
33  // Notify us when the tasks is finished
34  sender->on_finish([] {
35  g_done = true;
36  });
37 
38  // Connect to remote
39  bool send_ok = true;
40  std::string error;
41  try {
42  sender->send(std::make_shared<tasks::net::http_request>("localhost", "/", 18080));
43  } catch (tasks::net::socket_exception& e) {
44  send_ok = false;
45  error = e.what();
46  }
47  CPPUNIT_ASSERT_MESSAGE(std::string("send error: ") + error, send_ok);
48 
49  // wait for the response
50  while (!g_done) {
51  std::this_thread::sleep_for(std::chrono::seconds(1));
52  }
53 
54  // Check returned data
55  CPPUNIT_ASSERT_MESSAGE(std::string("g_status_code = ") + std::to_string(g_status_code), g_status_code == 404);
56  CPPUNIT_ASSERT_MESSAGE(std::string("g_content_type = ") + g_content_type, g_content_type == "text/html");
57 }
58 
60  // Second run
62  auto request = std::make_shared<tasks::net::http_request>("localhost", "/", 18080);
63 
64  g_status_code = 0;
65  g_content_type = "";
66  g_done = false;
67  bool send_ok = true;
68  std::string error = "";
69  try {
70  sender->send(request);
71  } catch (tasks::net::socket_exception& e) {
72  send_ok = false;
73  error = e.message();
74  }
75  CPPUNIT_ASSERT_MESSAGE(std::string("send error: ") + error, send_ok);
76 
77  while (!g_done) {
78  std::this_thread::sleep_for(std::chrono::seconds(1));
79  }
80 
81  CPPUNIT_ASSERT_MESSAGE(std::string("g_status_code = ") + std::to_string(g_status_code), g_status_code == 404);
82  CPPUNIT_ASSERT_MESSAGE(std::string("g_content_type = ") + g_content_type, g_content_type == "text/html");
83 
84  // 3rd run, same sender, same request
85  g_status_code = 0;
86  g_content_type = "";
87  g_done = false;
88  send_ok = true;
89  error = "";
90  try {
91  sender->send(request);
92  } catch (tasks::net::socket_exception& e) {
93  send_ok = false;
94  error = e.message();
95  }
96  CPPUNIT_ASSERT_MESSAGE(std::string("send error: ") + error, send_ok);
97 
98  while (!g_done) {
99  std::this_thread::sleep_for(std::chrono::seconds(1));
100  }
101 
102  CPPUNIT_ASSERT_MESSAGE(std::string("g_status_code = ") + std::to_string(g_status_code), g_status_code == 404);
103  CPPUNIT_ASSERT_MESSAGE(std::string("g_content_type = ") + g_content_type, g_content_type == "text/html");
104 
105  // the sender will not get deleted due to keep alive, so we do that now.
106  sender->finish();
107 }
108 
110  // create acceptor that is just closing the socket
111  auto srv = new tasks::net::acceptor<test_http_close_handler>(18081);
112  tasks::dispatcher::instance()->add_event_task(srv);
113 
114  auto* sender = new tasks::net::http_sender<test_handler>();
115 
116  sender->on_finish([sender] {
117  CPPUNIT_ASSERT_MESSAGE(std::string("error: ") + sender->error_message(),
118  sender->error_code() == tasks::tasks_error::SOCKET_NOCON ||
119  sender->error_code() == tasks::tasks_error::SOCKET_WRITE ||
120  sender->error_code() == tasks::tasks_error::SOCKET_READ);
121  g_done = true;
122  });
123 
125  std::string error_str = "";
126  g_done = false;
127  try {
128  sender->send(std::make_shared<tasks::net::http_request>("localhost", "/", 18081));
129  } catch (tasks::tasks_exception& e) {
130  error = e.error_code();
131  error_str = e.message();
132  }
133  CPPUNIT_ASSERT_MESSAGE(std::string("send error: ") + error_str, error == tasks::tasks_error::UNSET);
134 
135  while (!g_done) {
136  std::this_thread::sleep_for(std::chrono::seconds(1));
137  }
138 
139  tasks::dispatcher::instance()->remove_event_task(srv);
140 }
141 
142 
143 
144 
145 
146 
static std::shared_ptr< dispatcher > instance()
Definition: dispatcher.h:75
Error on sendto sys call.
bool handle_response(std::shared_ptr< tasks::net::http_response > response)
std::string g_content_type
Tasks execption class.
bool handle_response(std::shared_ptr< tasks::net::http_response > response)
Definition: http_client.cpp:16
void on_finish(finish_func_worker_t f)
Definition: task.h:56
int g_status_code
const std::string & message() const noexcept
Return the error message.
std::atomic< bool > g_done(false)
const char * what() const noexcept
Return the error message.
Error when reading from a disconnected socket.
tasks_error error_code() const noexcept
Return the error code.
Error on recvfrom sys call.