/
url_test.cpp
116 lines (96 loc) · 3.22 KB
/
url_test.cpp
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
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <NetworkKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(assert, line) cout << "ASSERT() failed at line " << line << ": " << #assert << endl
#define ASSERT(assertion) { if (!(assertion)) { REPORT(assertion, __LINE__ ); } }
using namespace std;
typedef struct
{
const char* url;
struct
{
const char* protocol;
const char* userName;
const char* password;
const char* host;
int16 port;
const char* path;
const char* request;
const char* fragment;
} expected;
} ExplodeTest;
const char* kTestLength[] =
{
"http://user:pass@www.foo.com:80/path?query#fragment",
"http://user:pass@www.foo.com:80/path?query#",
"http://user:pass@www.foo.com:80/path?query",
"http://user:pass@www.foo.com:80/path?",
"http://user:pass@www.foo.com:80/path",
"http://user:pass@www.foo.com:80/",
"http://user:pass@www.foo.com",
"http://user:pass@",
"http://www.foo.com",
"http://",
"http:"
};
const ExplodeTest kTestExplode[] =
// Url
// Protocol User Password Hostname Port Path Request Fragment
// -------- --------- --------- --------- ---- ---------- ---------- ------------
{
{ "http://user:pass@host:80/path?query#fragment",
{ "http", "user", "pass", "host", 80, "/path", "query", "fragment" } },
{ "http://www.host.tld/path?query#fragment",
{ "http", "", "", "www.host.tld",0, "/path", "query", "fragment" } }
};
// Test if rebuild url are the same length of parsed one
void lengthTest()
{
int8 testIndex;
BUrl testUrl;
for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++)
{
testUrl.SetUrlString(kTestLength[testIndex]);
cout << "`" << kTestLength[testIndex] << "' vs `" << testUrl.UrlString() << "'" << endl;
ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString()));
}
}
void explodeImplodeTest()
{
uint8 testIndex;
BUrl testUrl;
for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
{
testUrl.SetUrlString(kTestExplode[testIndex].url);
ASSERT(BString(kTestExplode[testIndex].url) == BString(testUrl.UrlString()));
ASSERT(BString(kTestExplode[testIndex].expected.protocol) == BString(testUrl.Protocol()));
ASSERT(BString(kTestExplode[testIndex].expected.userName) == BString(testUrl.UserName()));
ASSERT(BString(kTestExplode[testIndex].expected.password) == BString(testUrl.Password()));
ASSERT(BString(kTestExplode[testIndex].expected.host) == BString(testUrl.Host()));
ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port());
ASSERT(BString(kTestExplode[testIndex].expected.path) == BString(testUrl.Path()));
ASSERT(BString(kTestExplode[testIndex].expected.request) == BString(testUrl.Request()));
ASSERT(BString(kTestExplode[testIndex].expected.fragment) == BString(testUrl.Fragment()));
}
}
using std::cout;
using std::endl;
int
main(int, char**)
{
cout << "Running lengthTest:" << endl;
lengthTest();
cout << endl;
cout << "Running explodeImplodeTest:" << endl;
explodeImplodeTest();
cout << endl;
BUrl test = "lol";
cout << "Path: " << test.HasPath() << " -> " << test.Path() << endl;
cout << "Rebuild: " << test.UrlString() << endl;
return EXIT_SUCCESS;
}