Skip to content

Commit 51b007a

Browse files
committed
test: add cctest for native URL class
PR-URL: #12042 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent 1005b1d commit 51b007a

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@
611611
'<(OBJ_PATH)/node.<(OBJ_SUFFIX)',
612612
'<(OBJ_PATH)/node_buffer.<(OBJ_SUFFIX)',
613613
'<(OBJ_PATH)/node_i18n.<(OBJ_SUFFIX)',
614+
'<(OBJ_PATH)/node_url.<(OBJ_SUFFIX)',
614615
'<(OBJ_PATH)/debug-agent.<(OBJ_SUFFIX)',
615616
'<(OBJ_PATH)/util.<(OBJ_SUFFIX)',
616617
'<(OBJ_PATH)/string_bytes.<(OBJ_SUFFIX)',
@@ -637,6 +638,7 @@
637638

638639
'sources': [
639640
'test/cctest/test_util.cc',
641+
'test/cctest/test_url.cc'
640642
],
641643

642644
'sources!': [

src/node_url.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ enum url_error_cb_args {
492492
#define XX(name) name,
493493
ERR_ARGS(XX)
494494
#undef XX
495-
} url_error_cb_args;
495+
};
496496

497497
static inline bool IsSpecial(std::string scheme) {
498498
#define XX(name, _) if (scheme == name) return true;

test/cctest/test_url.cc

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "node_url.h"
2+
#include "node_i18n.h"
3+
4+
#include "gtest/gtest.h"
5+
6+
using node::url::URL;
7+
8+
class URLTest : public ::testing::Test {
9+
protected:
10+
void SetUp() override {
11+
#if defined(NODE_HAVE_I18N_SUPPORT)
12+
std::string icu_data_dir;
13+
node::i18n::InitializeICUDirectory(icu_data_dir);
14+
#endif
15+
}
16+
17+
void TearDown() override {}
18+
};
19+
20+
TEST_F(URLTest, Simple) {
21+
URL simple("https://example.org:81/a/b/c?query#fragment");
22+
23+
EXPECT_EQ(simple.protocol(), "https:");
24+
EXPECT_EQ(simple.host(), "example.org");
25+
EXPECT_EQ(simple.port(), 81);
26+
EXPECT_EQ(simple.path(), "/a/b/c");
27+
EXPECT_EQ(simple.query(), "query");
28+
EXPECT_EQ(simple.fragment(), "fragment");
29+
}
30+
31+
TEST_F(URLTest, Simple2) {
32+
const char* input = "https://example.org:81/a/b/c?query#fragment";
33+
URL simple(input, strlen(input));
34+
35+
EXPECT_EQ(simple.protocol(), "https:");
36+
EXPECT_EQ(simple.host(), "example.org");
37+
EXPECT_EQ(simple.port(), 81);
38+
EXPECT_EQ(simple.path(), "/a/b/c");
39+
EXPECT_EQ(simple.query(), "query");
40+
EXPECT_EQ(simple.fragment(), "fragment");
41+
}
42+
43+
TEST_F(URLTest, Base1) {
44+
URL base("http://example.org/foo/bar");
45+
URL simple("../baz", &base);
46+
47+
EXPECT_EQ(simple.protocol(), "http:");
48+
EXPECT_EQ(simple.host(), "example.org");
49+
EXPECT_EQ(simple.path(), "/baz");
50+
}
51+
52+
TEST_F(URLTest, Base2) {
53+
URL simple("../baz", "http://example.org/foo/bar");
54+
55+
EXPECT_EQ(simple.protocol(), "http:");
56+
EXPECT_EQ(simple.host(), "example.org");
57+
EXPECT_EQ(simple.path(), "/baz");
58+
}
59+
60+
TEST_F(URLTest, Base3) {
61+
const char* input = "../baz";
62+
const char* base = "http://example.org/foo/bar";
63+
64+
URL simple(input, strlen(input), base, strlen(base));
65+
66+
EXPECT_EQ(simple.protocol(), "http:");
67+
EXPECT_EQ(simple.host(), "example.org");
68+
EXPECT_EQ(simple.path(), "/baz");
69+
}

0 commit comments

Comments
 (0)