async_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 #include <vector>
5 
6 #include <cpr/cpr.h>
7 
8 #include "server.h"
9 
10 
11 static Server* server = new Server();
12 auto base = server->GetBaseUrl();
13 
14 TEST(UrlEncodedPostTests, AsyncGetTest) {
15  auto url = Url{base + "/hello.html"};
16  auto future = cpr::GetAsync(url);
17  auto expected_text = std::string{"Hello world!"};
18  auto response = future.get();
19  EXPECT_EQ(expected_text, response.text);
20  EXPECT_EQ(url, response.url);
21  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
22  EXPECT_EQ(200, response.status_code);
23 }
24 
25 TEST(UrlEncodedPostTests, AsyncGetMultipleTest) {
26  auto url = Url{base + "/hello.html"};
27  std::vector<AsyncResponse> responses;
28  for (int i = 0; i < 10; ++i) {
29  responses.emplace_back(cpr::GetAsync(url));
30  }
31  for (auto& future : responses) {
32  auto expected_text = std::string{"Hello world!"};
33  auto response = future.get();
34  EXPECT_EQ(expected_text, response.text);
35  EXPECT_EQ(url, response.url);
36  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
37  EXPECT_EQ(200, response.status_code);
38  }
39 }
40 
41 TEST(UrlEncodedPostTests, AsyncGetMultipleReflectTest) {
42  auto url = Url{base + "/hello.html"};
43  std::vector<AsyncResponse> responses;
44  for (int i = 0; i < 100; ++i) {
45  auto p = Parameters{{"key", std::to_string(i)}};
46  responses.emplace_back(cpr::GetAsync(url, p));
47  }
48  int i = 0;
49  for (auto& future : responses) {
50  auto expected_text = std::string{"Hello world!"};
51  auto response = future.get();
52  EXPECT_EQ(expected_text, response.text);
53  auto expected_url = Url{url + "?key=" + std::to_string(i)};
54  EXPECT_EQ(expected_url, response.url);
55  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
56  EXPECT_EQ(200, response.status_code);
57  ++i;
58  }
59 }
60 
61 int main(int argc, char** argv) {
62  ::testing::InitGoogleTest(&argc, argv);
64  return RUN_ALL_TESTS();
65 }
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1342
Definition: server.h:12
std::string Url
Definition: cprtypes.h:14
::std::string string
Definition: gtest-port.h:1129
const char ** p
Definition: unit1394.c:76
TEST(UrlEncodedPostTests, AsyncGetTest)
Definition: async_tests.cpp:14
unsigned int i
Definition: unit1303.c:79
AsyncResponse GetAsync(Ts...ts)
Definition: api.h:46
int main(int argc, char **argv)
Definition: async_tests.cpp:61
Url GetBaseUrl()
Definition: server.cpp:625
static Server * server
Definition: async_tests.cpp:11
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
auto base
Definition: async_tests.cpp:12


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08