-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.test.cpp
More file actions
132 lines (104 loc) · 3.17 KB
/
Copy pathHttp.test.cpp
File metadata and controls
132 lines (104 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <list>
#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include "Http.h"
#include "HttpChunked.h"
using namespace CppUnit;
using namespace std;
class HTTPTest : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(HTTPTest);
CPPUNIT_TEST(simpleGet);
CPPUNIT_TEST(simplePOST);
CPPUNIT_TEST(ReadHeaders);
CPPUNIT_TEST(GetWithHeaders);
CPPUNIT_TEST(GetChunked);
CPPUNIT_TEST(MapToQuery);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void);
void tearDown(void);
private:
void simpleGet(void);
void simplePOST(void);
void ReadHeaders(void);
void GetWithHeaders(void);
void GetChunked(void);
void MapToQuery(void);
};
void HTTPTest::setUp(void){
}
void HTTPTest::tearDown(void){
}
void HTTPTest::simpleGet(void){
string resp = Http::get("https://httpbin.org/get");
cout<<'\n'<<resp<<'\n';
CPPUNIT_ASSERT(!resp.empty());
}
void HTTPTest::simplePOST(void){
string resp = Http::post("https://httpbin.org/post", {{"foo","oof"}, {"nain", "nian"}});
cout<<'\n'<<resp<<'\n';
CPPUNIT_ASSERT(!resp.empty());
}
void HTTPTest::MapToQuery(void){
map<string, string> params;
params["foo"]="oof";
params["nain"]="nian";
CPPUNIT_ASSERT(Http::mapToURL(params)=="foo=oof&nain=nian");
}
void HTTPTest::ReadHeaders(void){
string headers;
string resp = Http::get("https://httpbin.org/get", {}, &headers);
cout<<'\n'<<headers<<'\n';
CPPUNIT_ASSERT(!headers.empty());
}
void HTTPTest::GetWithHeaders(void){
map<string, string> headerMap;
string header;
headerMap["Authorization"] = "Bearer hashed";
string resp = Http::get("https://httpbin.org/headers", headerMap, &header);
cout<<'\n'<<resp<<'\n';
CPPUNIT_ASSERT(!header.empty());
}
void HTTPTest::GetChunked(void){
Http::HttpChunked request{
[](string data){
}
};
map<string, string> headerMap;
request.start("https://httpbin.org/stream/20");
CPPUNIT_ASSERT(true);
}
CPPUNIT_TEST_SUITE_REGISTRATION( HTTPTest );
int main(int argc, char* argv[])
{
CPPUNIT_NS::TestResult testresult;
// register listener for collecting the test-results
CPPUNIT_NS::TestResultCollector collectedresults;
testresult.addListener (&collectedresults);
// register listener for per-test progress output
CPPUNIT_NS::BriefTestProgressListener progress;
testresult.addListener (&progress);
// insert test-suite at test-runner by registry
CPPUNIT_NS::TestRunner testrunner;
testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
testrunner.run(testresult);
// output results in compiler-format
CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
compileroutputter.write ();
ofstream xmlFileOut("Http.test.xml");
XmlOutputter xmlOut(&collectedresults, xmlFileOut);
xmlOut.write();
// return 0 if tests were successful
return collectedresults.wasSuccessful() ? 0 : 1;
}