raw_body_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <cstdio>
4 #include <fstream>
5 #include <string>
6 
7 #include <cpr/cpr.h>
8 #include <cpr/multipart.h>
9 
10 #include "server.h"
11 
12 using namespace cpr;
13 
14 static Server* server = new Server();
15 auto base = server->GetBaseUrl();
16 
17 TEST(BodyPostTests, DefaultUrlEncodedPostTest) {
18  auto url = Url{base + "/url_post.html"};
19  auto response = cpr::Post(url, Body{"x=5"});
20  auto expected_text = std::string{"{\n"
21  " \"x\": 5\n"
22  "}"};
23  EXPECT_EQ(expected_text, response.text);
24  EXPECT_EQ(url, response.url);
25  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
26  EXPECT_EQ(201, response.status_code);
27  EXPECT_EQ(ErrorCode::OK, response.error.code);
28 }
29 
30 TEST(BodyPostTests, TextUrlEncodedPostTest) {
31  auto url = Url{base + "/url_post.html"};
32  auto response = cpr::Post(url, Body{"x=hello world!!~"});
33  auto expected_text = std::string{"{\n"
34  " \"x\": hello world!!~\n"
35  "}"};
36  EXPECT_EQ(expected_text, response.text);
37  EXPECT_EQ(url, response.url);
38  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
39  EXPECT_EQ(201, response.status_code);
40  EXPECT_EQ(ErrorCode::OK, response.error.code);
41 }
42 
43 TEST(BodyPostTests, TextUrlEncodedNoCopyPostTest) {
44  auto url = Url{base + "/url_post.html"};
45  auto body = Body{"x=hello world!!~"};
46  // body lives through the lifetime of Post, so it doesn't need to be copied
47  auto response = cpr::Post(url, body);
48  auto expected_text = std::string{"{\n"
49  " \"x\": hello world!!~\n"
50  "}"};
51  EXPECT_EQ(expected_text, response.text);
52  EXPECT_EQ(url, response.url);
53  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
54  EXPECT_EQ(201, response.status_code);
55  EXPECT_EQ(ErrorCode::OK, response.error.code);
56 }
57 
58 TEST(BodyPostTests, UrlEncodedManyPostTest) {
59  auto url = Url{base + "/url_post.html"};
60  auto response = cpr::Post(url, Body{"x=5&y=13"});
61  auto expected_text = std::string{"{\n"
62  " \"x\": 5,\n"
63  " \"y\": 13,\n"
64  " \"sum\": 18\n"
65  "}"};
66  EXPECT_EQ(expected_text, response.text);
67  EXPECT_EQ(url, response.url);
68  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
69  EXPECT_EQ(201, response.status_code);
70  EXPECT_EQ(ErrorCode::OK, response.error.code);
71 }
72 
73 TEST(BodyPostTests, CustomHeaderNumberPostTest) {
74  auto url = Url{base + "/json_post.html"};
75  auto response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "application/json"}});
76  auto expected_text = std::string{"{\"x\":5}"};
77  EXPECT_EQ(expected_text, response.text);
78  EXPECT_EQ(url, response.url);
79  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
80  EXPECT_EQ(201, response.status_code);
81  EXPECT_EQ(ErrorCode::OK, response.error.code);
82 }
83 
84 TEST(BodyPostTests, CustomHeaderTextPostTest) {
85  auto url = Url{base + "/json_post.html"};
86  auto response = cpr::Post(url, Body{"{\"x\":\"hello world!!~\"}"},
87  Header{{"Content-Type", "application/json"}});
88  auto expected_text = std::string{"{\"x\":\"hello world!!~\"}"};
89  EXPECT_EQ(expected_text, response.text);
90  EXPECT_EQ(url, response.url);
91  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
92  EXPECT_EQ(201, response.status_code);
93  EXPECT_EQ(ErrorCode::OK, response.error.code);
94 }
95 
96 TEST(BodyPostTests, CustomWrongHeaderPostTest) {
97  auto url = Url{base + "/json_post.html"};
98  auto response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "text/plain"}});
99  auto expected_text = std::string{"Unsupported Media Type"};
100  EXPECT_EQ(expected_text, response.text);
101  EXPECT_EQ(url, response.url);
102  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
103  EXPECT_EQ(415, response.status_code);
104  EXPECT_EQ(ErrorCode::OK, response.error.code);
105 }
106 
107 TEST(BodyPostTests, UrlPostBadHostTest) {
108  auto url = Url{"http://bad_host/"};
109  auto response = cpr::Post(url, Body{"hello=world"});
110  EXPECT_EQ(std::string{}, response.text);
111  EXPECT_EQ(url, response.url);
112  EXPECT_EQ(std::string{}, response.header["content-type"]);
113  EXPECT_EQ(0, response.status_code);
114  EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
115 }
116 
117 TEST(BodyPostTests, StringMoveBodyTest) {
118  auto url = Url{base + "/url_post.html"};
119  auto response = cpr::Post(url, Body{std::string{"x=5"}});
120  auto expected_text = std::string{"{\n"
121  " \"x\": 5\n"
122  "}"};
123  EXPECT_EQ(expected_text, response.text);
124  EXPECT_EQ(url, response.url);
125  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
126  EXPECT_EQ(201, response.status_code);
127  EXPECT_EQ(ErrorCode::OK, response.error.code);
128 }
129 
130 int main(int argc, char** argv) {
131  ::testing::InitGoogleTest(&argc, argv);
133  return RUN_ALL_TESTS();
134 }
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1342
TEST(BodyPostTests, DefaultUrlEncodedPostTest)
Definition: server.h:12
std::string Url
Definition: cprtypes.h:14
Definition: body.h:13
::std::string string
Definition: gtest-port.h:1129
static const void * body(MD5_CTX *ctx, const void *data, unsigned long size)
Definition: md5.c:281
int main(int argc, char **argv)
Url GetBaseUrl()
Definition: server.cpp:625
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
Definition: auth.cpp:3
static Server * server
auto base
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
Response Post(Ts &&...ts)
Definition: api.h:61


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