Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add cctest for native URL class #12042

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@
'<(OBJ_PATH)/node.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/node_buffer.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/node_i18n.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/node_url.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/debug-agent.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/util.<(OBJ_SUFFIX)',
'<(OBJ_PATH)/string_bytes.<(OBJ_SUFFIX)',
Expand All @@ -637,6 +638,7 @@

'sources': [
'test/cctest/test_util.cc',
'test/cctest/test_url.cc'
],

'sources!': [
Expand Down
2 changes: 1 addition & 1 deletion src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ enum url_error_cb_args {
#define XX(name) name,
ERR_ARGS(XX)
#undef XX
} url_error_cb_args;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when node_url.h is included in the cctest/test_url.h, a compile error occurs because the url_error_cb_args is defined multiple times. I had to make similar changes to the other structs defined in this file. It would likely be good to refactor the node_url.h file a bit if we intend for it to be generally includable in more than just node_url.cc.


static inline bool IsSpecial(std::string scheme) {
#define XX(name, _) if (scheme == name) return true;
Expand Down
69 changes: 69 additions & 0 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "node_url.h"
#include "node_i18n.h"

#include "gtest/gtest.h"

using node::url::URL;

class URLTest : public ::testing::Test {
protected:
void SetUp() override {
#if defined(NODE_HAVE_I18N_SUPPORT)
std::string icu_data_dir;
node::i18n::InitializeICUDirectory(icu_data_dir);
#endif
}

void TearDown() override {}
};

TEST_F(URLTest, Simple) {
URL simple("https://example.org:81/a/b/c?query#fragment");

EXPECT_EQ(simple.protocol(), "https:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.port(), 81);
EXPECT_EQ(simple.path(), "/a/b/c");
EXPECT_EQ(simple.query(), "query");
EXPECT_EQ(simple.fragment(), "fragment");
}

TEST_F(URLTest, Simple2) {
const char* input = "https://example.org:81/a/b/c?query#fragment";
URL simple(input, strlen(input));

EXPECT_EQ(simple.protocol(), "https:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.port(), 81);
EXPECT_EQ(simple.path(), "/a/b/c");
EXPECT_EQ(simple.query(), "query");
EXPECT_EQ(simple.fragment(), "fragment");
}

TEST_F(URLTest, Base1) {
URL base("http://example.org/foo/bar");
URL simple("../baz", &base);

EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}

TEST_F(URLTest, Base2) {
URL simple("../baz", "http://example.org/foo/bar");

EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}

TEST_F(URLTest, Base3) {
const char* input = "../baz";
const char* base = "http://example.org/foo/bar";

URL simple(input, strlen(input), base, strlen(base));

EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}