Skip to content
Merged
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
18 changes: 1 addition & 17 deletions src/robots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Rep

Robots::Robots(const std::string& content): agents_(), sitemaps_(), default_(agents_["*"])
{
std::string agent_name("");
std::string agent_name("*");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be nice to distinguish between normal declared * agents and top-level undeclared agents for the purposes of reporting site issues.

std::istringstream input(content);
if (content.compare(0, 3, "\xEF\xBB\xBF") == 0)
{
Expand Down Expand Up @@ -103,30 +103,14 @@ namespace Rep
}
else if (key.compare("disallow") == 0)
{
if (agent_name.empty())
{
throw std::invalid_argument(
"Need User-Agent before Disallow");
}
current->second.disallow(value);
}
else if (key.compare("allow") == 0)
{
if (agent_name.empty())
{
throw std::invalid_argument(
"Need User-Agent before Allow");
}
current->second.allow(value);
}
else if (key.compare("crawl-delay") == 0)
{
if (agent_name.empty())
{
throw std::invalid_argument(
"Need User-Agent before Crawl-Delay");
}

try
{
current->second.delay(std::stof(value));
Expand Down
25 changes: 10 additions & 15 deletions test/test-robots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

#include "robots.h"

TEST(RobotsTest, DisallowFirst)
TEST(RobotsTest, NoLeadingUserAgent)
{
// Disallow must be preceeded by a User-Agent line
ASSERT_THROW(Rep::Robots("Disallow: /path"), std::invalid_argument);
}

TEST(RobotsTest, AllowFirst)
{
// Allow must be preceeded by a User-Agent line
ASSERT_THROW(Rep::Robots("Allow: /path"), std::invalid_argument);
}

TEST(RobotsTest, CrawlDelayFirst)
{
// Crawl-delay must be preceeded by a User-Agent line
ASSERT_THROW(Rep::Robots("Crawl-delay: 1"), std::invalid_argument);
// Assumed to be the default user agent
std::string content =
"Disallow: /path\n"
"Allow: /path/exception\n"
"Crawl-delay: 5.2\n";
Rep::Robots robot(content);
EXPECT_TRUE(robot.allowed("/path/exception", "agent"));
EXPECT_FALSE(robot.allowed("/path", "agent"));
EXPECT_NEAR(robot.agent("agent").delay(), 5.2, 0.000001);
}

TEST(RobotsTest, WellFormedCrawlDelay)
Expand Down