123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756 |
- #include <gtest/gtest.h>
- #include <array>
- #include <cstdio>
- #include <fstream>
- #include <string>
- #include <cpr/cpr.h>
- #include <cpr/multipart.h>
- #include "httpServer.hpp"
- using namespace cpr;
- static HttpServer* server = new HttpServer();
- TEST(UrlEncodedPostTests, UrlPostSingleTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}});
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, UrlPostAddPayloadPair) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "1"}};
- payload.Add({"y", "2"});
- Response response = cpr::Post(url, payload);
- std::string expected_text{
- "{\n"
- " \"x\": 1,\n"
- " \"y\": 2,\n"
- " \"sum\": 3\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- }
- TEST(UrlEncodedPostTests, UrlPostPayloadIteratorTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::vector<Pair> payloadData;
- payloadData.emplace_back("x", "1");
- payloadData.emplace_back("y", "2");
- Response response = cpr::Post(url, Payload(payloadData.begin(), payloadData.end()));
- std::string expected_text{
- "{\n"
- " \"x\": 1,\n"
- " \"y\": 2,\n"
- " \"sum\": 3\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- }
- TEST(UrlEncodedPostTests, UrlPostEncodeTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = cpr::Post(url, Payload{{"x", "hello world!!~"}});
- std::string expected_text{
- "{\n"
- " \"x\": hello world!!~\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, UrlPostEncodeNoCopyTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "hello world!!~"}};
- // payload lives through the lifetime of Post, so it doesn't need to be copied
- Response response = cpr::Post(url, payload);
- std::string expected_text{
- "{\n"
- " \"x\": hello world!!~\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, UrlPostManyTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}, {"y", "13"}});
- std::string expected_text{
- "{\n"
- " \"x\": 5,\n"
- " \"y\": 13,\n"
- " \"sum\": 18\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, UrlPostBadHostTest) {
- Url url{"http://bad_host/"};
- Response response = cpr::Post(url, Payload{{"hello", "world"}});
- EXPECT_EQ(std::string{}, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{}, response.header["content-type"]);
- EXPECT_EQ(0, response.status_code);
- EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostSingleTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", 5}});
- std::string expected_text{
- "{\n"
- " \"x\": \"5\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileTest) {
- std::string filename{"test_file"};
- std::string content{"hello world"};
- std::ofstream test_file;
- test_file.open(filename);
- test_file << content;
- test_file.close();
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", File{filename}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- content +
- "\"\n"
- "}"};
- std::remove(filename.c_str());
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostMultipleFilesTestLvalue) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- std::string filename1{"file1"};
- std::string content1{"apple"};
- std::ofstream file1;
- file1.open(filename1);
- file1 << content1;
- file1.close();
- std::string filename2{"file2"};
- std::string content2{"banana"};
- std::ofstream file2;
- file2.open(filename2);
- file2 << content2;
- file2.close();
- File singleFile{"file1"};
- File singleFileWithOverridedFilename{"file1", "applefile"};
- Files multipleFiles{"file1", "file2"};
- Files multipleFilesWithOverridedFilename{
- File{"file1", "applefile"},
- File{"file2", "bananafile"},
- };
- {
- Response response = cpr::Post(url, Multipart{{"files", singleFile}});
- std::string expected_text{
- "{\n"
- " \"files\": \"file1=" +
- content1 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", singleFileWithOverridedFilename}});
- std::string expected_text{
- "{\n"
- " \"files\": \"applefile=" +
- content1 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", multipleFiles}});
- std::string expected_text{
- "{\n"
- " \"files\": \"file1=" +
- content1 +
- "\",\n"
- " \"files\": \"file2=" +
- content2 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", multipleFilesWithOverridedFilename}});
- std::string expected_text{
- "{\n"
- " \"files\": \"applefile=" +
- content1 +
- "\",\n"
- " \"files\": \"bananafile=" +
- content2 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- std::remove(filename1.c_str());
- std::remove(filename2.c_str());
- }
- TEST(UrlEncodedPostTests, FormPostMultipleFilesTestRvalue) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- std::string filename1{"file1"};
- std::string content1{"apple"};
- std::ofstream file1;
- file1.open(filename1);
- file1 << content1;
- file1.close();
- std::string filename2{"file2"};
- std::string content2{"banana"};
- std::ofstream file2;
- file2.open(filename2);
- file2 << content2;
- file2.close();
- {
- Response response = cpr::Post(url, Multipart{{"files", File{"file1"}}});
- std::string expected_text{
- "{\n"
- " \"files\": \"file1=" +
- content1 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", File{"file1", "applefile"}}});
- std::string expected_text{
- "{\n"
- " \"files\": \"applefile=" +
- content1 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", Files{"file1", "file2"}}});
- std::string expected_text{
- "{\n"
- " \"files\": \"file1=" +
- content1 +
- "\",\n"
- " \"files\": \"file2=" +
- content2 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = cpr::Post(url, Multipart{{"files", Files{
- File{"file1", "applefile"},
- File{"file2", "bananafile"},
- }}});
- std::string expected_text{
- "{\n"
- " \"files\": \"applefile=" +
- content1 +
- "\",\n"
- " \"files\": \"bananafile=" +
- content2 + "\"\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- std::remove(filename1.c_str());
- std::remove(filename2.c_str());
- }
- TEST(UrlEncodedPostTests, FormPostFileTestWithOverridedFilename) {
- std::string filename{"test_file"};
- std::string overided_filename{"overided_filename"};
- std::string content{"hello world"};
- std::ofstream test_file;
- test_file.open(filename);
- test_file << content;
- test_file.close();
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", File{filename, overided_filename}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"" +
- overided_filename + "=" + content +
- "\"\n"
- "}"};
- std::remove(filename.c_str());
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileNoCopyTest) {
- std::string filename{"./test_file"};
- std::string content{"hello world"};
- std::ofstream test_file;
- test_file.open(filename);
- test_file << content;
- test_file.close();
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Multipart multipart{{"x", File{filename}}};
- Response response = cpr::Post(url, multipart);
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- content +
- "\"\n"
- "}"};
- std::remove(filename.c_str());
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileNoCopyTestWithOverridedFilename) {
- std::string filename{"test_file"};
- std::string overrided_filename{"overided_filename"};
- std::string content{"hello world"};
- std::ofstream test_file;
- test_file.open(filename);
- test_file << content;
- test_file.close();
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Multipart multipart{{"x", File{filename, overrided_filename}}};
- Response response = cpr::Post(url, multipart);
- std::string expected_text{
- "{\n"
- " \"x\": \"" +
- overrided_filename + "=" + content +
- "\"\n"
- "}"};
- std::remove(filename.c_str());
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, TimeoutPostTest) {
- Url url{server->GetBaseUrl() + "/json_post.html"};
- std::string body{R"({"RegisterObject": {"DeviceID": "65010000005030000001"}})"};
- cpr::Response response = cpr::Post(url, cpr::Header{{"Content-Type", "application/json"}}, cpr::Body{body}, cpr::ConnectTimeout{3000}, cpr::Timeout{3000});
- std::string expected_text{R"({"RegisterObject": {"DeviceID": "65010000005030000001"}})"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferTest) {
- std::string content{"hello world"};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- content +
- "\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferNoCopyTest) {
- std::string content{"hello world"};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Multipart multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}};
- Response response = cpr::Post(url, multipart);
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- content +
- "\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferPointerTest) {
- const char* content = "hello world";
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", Buffer{content, 11 + content, "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- std::string(content) +
- "\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferArrayTest) {
- const char content[] = "hello world";
- Url url{server->GetBaseUrl() + "/form_post.html"};
- // We subtract 1 from std::end() because we don't want to include the terminating null
- Response response = cpr::Post(url, Multipart{{"x", Buffer{std::begin(content), std::end(content) - 1, "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=" +
- std::string(content) +
- "\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferVectorTest) {
- std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=hello world\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostFileBufferStdArrayTest) {
- std::array<unsigned char, 11> content{{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=hello world\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostBufferRvalueTest) {
- std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=hello world\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, ReflectPostBufferLvalueTest) {
- std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Buffer buff{content.begin(), content.end(), "test_file"};
- Response response = cpr::Post(url, Multipart{{"x", buff}});
- std::string expected_text{
- "{\n"
- " \"x\": \"test_file=hello world\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostManyTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", 5}, {"y", 13}});
- std::string expected_text{
- "{\n"
- " \"x\": \"5\",\n"
- " \"y\": \"13\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostManyNoCopyTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Multipart multipart{{"x", 5}, {"y", 13}};
- Response response = cpr::Post(url, multipart);
- std::string expected_text{
- "{\n"
- " \"x\": \"5\",\n"
- " \"y\": \"13\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostContentTypeTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url, Multipart{{"x", 5, "application/number"}});
- std::string expected_text{
- "{\n"
- " \"x\": \"5\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, FormPostContentTypeLValueTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Multipart multipart{{"x", 5, "application/number"}};
- Response response = cpr::Post(url, multipart);
- std::string expected_text{
- "{\n"
- " \"x\": \"5\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, UrlPostAsyncSingleTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::vector<AsyncResponse> responses;
- for (size_t i = 0; i < 10; ++i) {
- responses.emplace_back(cpr::PostAsync(url, payload));
- }
- for (cpr::AsyncResponse& future_response : responses) {
- cpr::Response response = future_response.get();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(UrlEncodedPostTests, UrlReflectTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}});
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UrlEncodedPostTests, PostWithNoBodyTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Response response = cpr::Post(url);
- std::string expected_text{"{\n}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- static std::string getTimestamp() {
- char buf[1000];
- time_t now = time(0);
- struct tm* tm = gmtime(&now);
- strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S GMT", tm);
- return buf;
- }
- TEST(UrlEncodedPostTests, PostReflectTest) {
- std::string uri = server->GetBaseUrl() + "/post_reflect.html";
- std::string body = R"({"property1": "value1"})";
- std::string contentType = "application/json";
- std::string signature = "x-ms-date: something";
- std::string logType = "LoggingTest";
- std::string date = getTimestamp();
- Response response = cpr::Post(cpr::Url(uri), cpr::Header{{"content-type", contentType}, {"Authorization", signature}, {"log-type", logType}, {"x-ms-date", date}, {"content-length", std::to_string(body.length())}}, cpr::Body(body));
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(body, response.text);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(signature, response.header["Authorization"]);
- EXPECT_EQ(logType, response.header["log-type"]);
- EXPECT_EQ(date, response.header["x-ms-date"]);
- EXPECT_EQ(std::to_string(body.length()), response.header["content-length"]);
- }
- TEST(UrlEncodedPostTests, PostReflectPayloadTest) {
- std::string uri = server->GetBaseUrl() + "/header_reflect.html";
- cpr::Payload payload = cpr::Payload{{"email", ""}, {"password", ""}, {"devicetoken", ""}};
- cpr::Response response = cpr::Post(cpr::Url(uri), cpr::Timeout{10000}, payload);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(200, response.status_code);
- }
- TEST(UrlEncodedPostTests, InjectMultipleHeadersTest) {
- std::string uri = server->GetBaseUrl() + "/post_reflect.html";
- std::string key_1 = "key_1";
- std::string val_1 = "value_1";
- std::string key_2 = "key_2";
- std::string val_2 = "value_2";
- cpr::Response response = cpr::Post(cpr::Url{uri}, cpr::Header{{key_1, val_1}}, cpr::Header{{key_2, val_2}});
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(val_1, response.header[key_1]);
- EXPECT_EQ(val_2, response.header[key_2]);
- }
- TEST(UrlEncodedPostTests, PostBodyWithFile) {
- std::string filename{"test_file"};
- std::string expected_text(R"({"property1": "value1"})");
- std::ofstream test_file;
- test_file.open(filename);
- test_file << expected_text;
- test_file.close();
- Url url{server->GetBaseUrl() + "/post_reflect.html"};
- cpr::Response response = Post(url, cpr::Header({{"Content-Type", "application/octet-stream"}}), cpr::Body(File("test_file")));
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- }
- TEST(UrlEncodedPostTests, PostBodyWithBuffer) {
- Url url{server->GetBaseUrl() + "/post_reflect.html"};
- std::string expected_text(R"({"property1": "value1"})");
- cpr::Response response = Post(url, cpr::Header({{"Content-Type", "application/octet-stream"}}), cpr::Body(Buffer{expected_text.begin(), expected_text.end(), "test_file"}));
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PostRedirectTests, TempRedirectTest) {
- Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PostRedirectTests, TempRedirectNoneTest) {
- Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));
- EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
- EXPECT_EQ(405, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PostRedirectTests, PermRedirectTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PostRedirectTests, PermRedirectNoneTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));
- EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
- EXPECT_EQ(405, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
- ::testing::AddGlobalTestEnvironment(server);
- return RUN_ALL_TESTS();
- }
|