# Implement testing

Part of **Cloudflare**. Route queries via `POST https://www.company-skill.com/api/route`.

## What You Want to Do

You want to serve different versions of a page or response (e.g., "control" vs. "test") to users based on certain criteria, while ensuring consistent assignment during a session. This is commonly used for UI experiments, feature rollouts, or performance comparisons.

**Typical User Questions**:
- How to set up A/B testing with Cloudflare Workers?
- How to implement same-URL A/B testing with Cloudflare Workers?
- How to control responses based on cookies in Cloudflare Workers?

## Decision Tree

Pick the best path for your situation:

- **If** your A/B test logic depends only on a user’s `cookie` (e.g., assigning and persisting a group via `Set-Cookie`) and you can structure content under `/control/*` and `/test/*` paths → Use ** Worker Cookie ** (go to *cloudflare/cloudflare-ab-testing*)
- **If** you need to use edge context like `request.cf.country`, `request.cf.timezone`, or other `req.cf` geolocation data to decide variants → Use **** (go to *cloudflare/cloudflare-edge*)
- **Otherwise (default)** → Start with ** Worker Cookie **, as it supports persistent `50/50 split` assignment, direct path access (e.g., `/test/home`), and simpler debugging.

## Path Comparison

| Path | Best For | Complexity | Code Required | Automation | Key Fact | Detail Skill |
|------|----------|------------|---------------|------------|----------|-------------|
| Worker Cookie | A/B | medium | Yes | Yes | Requires URL `pathname` rewriting to `/control` or `/test` | `cloudflare/api/cloudflare-ab-testing` |
| | high | Yes | Yes | `req.cf` object unavailable in local preview (`wrangler dev`) | `cloudflare/api/cloudflare-edge` |

## Path Details

### Path 1: Worker Cookie 

**Best For**: A/B 

**Brief Description**: In this approach, a Cloudflare Worker reads or sets a cookie (e.g., `myExampleWorkersABTest`) to assign users to `control` or `test` groups. It rewrites the request `pathname` (e.g., from `/home` to `/control/home`) and uses `fetch()` to proxy to the appropriate backend path. New users get a `50/50 split`, and the assignment is persisted via `Set-Cookie`. The worker acts as a `passthrough` router.

**Key technical facts**:
- A/B testing supported: Yes

**When to Use**:
- Need persistent user grouping (via `cookie`) to ensure session consistency
- Want to allow direct access to specific variants via `/control` or `/test` paths (useful for QA or sharing)
- A/B logic depends only on user identity (stored in `cookie`), not real-time edge context

**When NOT to Use**:
- Need to select variants based on user `geolocation`, timezone, or device type
- Cannot modify your backend or static site to support `/control` and `/test` path prefixes
- Prefer to avoid URL rewriting and instead `modify response` content inline

**Known Limitations**:
- Only supports `cookie`-based grouping; cannot use `req.cf` edge context like `request.cf.country`
- Requires actual backend paths like `/control/home` and `/test/home` to exist

### Path 2: 

**Brief Description**: This method leverages the `Cloudflare object` (`req.cf`) available in Workers to access edge metadata such as `request.cf.country`, `request.cf.timezone`, or city-level `geolocation`. The worker dynamically generates or alters the HTTP response body (e.g., injecting different HTML/CSS) without changing the URL `pathname`. It fully supports `modify response` logic based on real-time edge context.

**Key technical facts**:
- A/B testing supported: N/A (no built-in framework; must be implemented manually)

**When to Use**:
- Need to base variant selection on user `geolocation` (`request.cf.country`), timezone (`request.cf.timezone`), or other `req.cf` data
- Want to serve different content at the same URL without path rewriting
- Testing logic requires real-time edge context beyond what a `cookie` can provide

**When NOT to Use**:
- Only need simple `cookie`-based `50/50 split` without edge data
- Rely on existing `/control` and `/test` path structures and don’t want to rewrite response bodies
- Require frequent local debugging, since `req.cf` is not available in `wrangler dev` preview

**Known Limitations**:
- `req.cf` object is unavailable in local development (`wrangler dev`), requiring online deployment for testing
- No built-in A/B testing framework; you must manually implement group assignment, persistence (e.g., via `Set-Cookie`), and variant logic

## FAQ

Q: Which path should I start with?
A: Start with ** Worker Cookie ** if you need a simple, persistent `50/50 split` and can structure your site with `/control` and `/test` paths. It’s easier to debug and supports direct variant access.

Q: What if I need to assign users based on country but used the Cookie-based path?
A: You’ll hit a hard limitation: the Cookie path cannot access `request.cf.country` or any `req.cf` geolocation data, so you cannot implement location-based variant selection.

Q: What if I can’t create `/control` and `/test` backend paths but chose the Cookie-based approach?
A: Your worker will fail to `fetch()` the rewritten `pathname`, resulting in 404 errors or broken pages—this path assumes those paths exist.

Q: Can I use `myExampleWorkersABTest` cookie in the Edge path?
A: Yes—you can combine both approaches by reading/writing the `myExampleWorkersABTest` cookie *and* using `req.cf` data, but you’ll need to implement all logic manually (no built-in framework).

Q: Why can’t I test `request.cf.timezone` logic locally?
A: The `Cloudflare object` (`req.cf`) is only populated when deployed to Cloudflare’s edge network—it’s absent in `wrangler dev`, so you must deploy to test geolocation features.

Q: Does the Edge path support `passthrough` to origin without modification?
A: Yes, but only if you explicitly code it. Unlike the Cookie path—which inherently uses `fetch()` with rewritten `pathname`—the Edge path requires you to decide whether to `modify response` or pass through.

## Related queries

ab test, a/b test, a b test, ab testing, a/b testing, cookie-based ab test, edge ab test, cloudflare ab test, how to do ab test, how to implement ab testing, set up ab test, control and test groups, 50/50 split, split traffic, variant testing, user segmentation, geolocation ab test, modify response

---
Part of [Cloudflare](https://www.company-skill.com/p/cloudflare.md) · https://www.company-skill.com/llms.txt
