Sub topic 9
Testing your rules
Assume you already understand HTTP requests, the User-Agent header, and how a web server serves /robots.txt. You should also be able to edit a text file and run basic Python and curl commands.
A robots.txt test has two parts:
- Check how a parser matches each crawler name and URL.
- Check that the live server returns the exact file you intended.
Testing both parts catches different errors. A local parser can reveal a bad rule. A live request can reveal a wrong location, redirect, content type, deployment mistake, or cached version.
Test representative URLs
Do not test only the site root. Include a public page, a protected directory, and a path that shares a prefix with a protected directory.
For example, suppose the policy blocks drafts but allows public articles:
User-agent: GPTBot
Disallow: /drafts/
User-agent: OAI-SearchBot
Allow: /
User-agent: *
Allow: /The expected results are:
| Crawler | URL | Expected result |
|---|---|---|
| GPTBot | `/drafts/plan.html` | blocked |
| GPTBot | `/articles/guide.html` | allowed |
| OAI-SearchBot | `/drafts/plan.html` | allowed |
| SomeOtherBot | `/articles/guide.html` | allowed |
The slash matters. Disallow: /drafts/ matches URLs below that directory, such as /drafts/plan.html. It does not match /drafts.html.
Use a parser before deployment
Python includes urllib.robotparser, which can evaluate a draft rule set. This is useful for repeatable checks:
from urllib.robotparser import RobotFileParser
rules = """\
User-agent: GPTBot
Disallow: /drafts/
User-agent: OAI-SearchBot
Allow: /
User-agent: *
Allow: /
"""
parser = RobotFileParser()
parser.parse(rules.splitlines())
tests = [
("GPTBot", "https://example.com/drafts/plan.html", False),
("GPTBot", "https://example.com/articles/guide.html", True),
("OAI-SearchBot", "https://example.com/drafts/plan.html", True),
("SomeOtherBot", "https://example.com/articles/guide.html", True),
]
for agent, url, expected in tests:
actual = parser.can_fetch(agent, url)
status = "PASS" if actual == expected else "FAIL"
print(f"{status}: {agent} {url} -> {actual}")Test the exact tokens documented by each crawler. Also test a generic agent, because the User-agent: * group controls clients without a more specific group.
A useful test matrix includes:
- Each policy-specific user agent.
- A generic, unknown user agent.
- An allowed URL.
- A blocked URL.
- A URL at the boundary of a rule, such as
/drafts.html. - A URL with a query string, such as
/drafts/plan.html?preview=1.
The parser checks rule matching. It does not prove that a crawler will obey the result.
Verify the deployed file
A robots.txt file applies only when served at the host root:
curl -sI https://example.com/robots.txt | head -n 5
curl -s https://example.com/robots.txtThe response should normally have a successful status, and the body should contain the rules you reviewed. Check the host, scheme, redirects, and casing. https://example.com/robots.txt and https://www.example.com/robots.txt can be different files because they are different hosts.
You can send a test user agent while checking the response:
curl -sS -A 'GPTBot' https://example.com/robots.txt
curl -sS -A 'OAI-SearchBot' https://example.com/robots.txtThese commands only retrieve the file. They do not make the server enforce the rules, and they do not make a crawler fetch a page.
Follow the testing path
Interpret failures
If a parser test fails, inspect group selection and path spelling first. A crawler-specific group can take precedence over the wildcard group, so do not assume that User-agent: * supplies fallback rules for a crawler that has its own group.
If the local tests pass but the live check fails, inspect deployment routing, redirects, proxies, and CDN caching. If the file is correct but logs show unexpected traffic, remember that robots.txt is advisory. A crawler may ignore it or spoof its user-agent.
Exercise
Your local test says GPTBot is blocked from /drafts/, but curl https://example.com/robots.txt shows an older file without that rule. What should you check first?
Show answer
Check that the file was deployed to the correct host and root path, then inspect redirects and caching before treating the crawler behavior as a rule-matching problem.