@@ -100,41 +100,42 @@ export default {
100100``` py
101101import random
102102from urllib.parse import urlparse, urlunparse
103- from workers import Response, fetch
103+ from workers import Response, fetch, WorkerEntrypoint
104104
105105NAME = " myExampleWorkersABTest"
106106
107- async def on_fetch (request ):
108- url = urlparse(request.url)
109- # Uncomment below when testing locally
110- # url = url._replace(netloc="example.com") if "localhost" in url.netloc else url
111-
112- # Enable Passthrough to allow direct access to control and test routes.
113- if url.path.startswith(" /control" ) or url.path.startswith(" /test" ):
114- return fetch(urlunparse(url))
115-
116- # Determine which group this requester is in.
117- cookie = request.headers.get(" cookie" )
118-
119- if cookie and f ' { NAME } =control ' in cookie:
120- url = url._replace(path = " /control" + url.path)
121- elif cookie and f ' { NAME } =test ' in cookie:
122- url = url._replace(path = " /test" + url.path)
123- else :
124- # If there is no cookie, this is a new client. Choose a group and set the cookie.
125- group = " test" if random.random() < 0.5 else " control"
126- if group == " control" :
127- url = url._replace(path = " /control" + url.path)
128- else :
129- url = url._replace(path = " /test" + url.path)
130-
131- # Reconstruct response to avoid immutability
132- res = await fetch(urlunparse(url))
133- headers = dict (res.headers)
134- headers[" Set-Cookie" ] = f ' { NAME } = { group} ; path=/ '
135- return Response(res.body, headers = headers)
136-
137- return fetch(urlunparse(url))
107+ class Default (WorkerEntrypoint ):
108+ async def fetch (self , request ):
109+ url = urlparse(request.url)
110+ # Uncomment below when testing locally
111+ # url = url._replace(netloc="example.com") if "localhost" in url.netloc else url
112+
113+ # Enable Passthrough to allow direct access to control and test routes.
114+ if url.path.startswith(" /control" ) or url.path.startswith(" /test" ):
115+ return fetch(urlunparse(url))
116+
117+ # Determine which group this requester is in.
118+ cookie = request.headers.get(" cookie" )
119+
120+ if cookie and f ' { NAME } =control ' in cookie:
121+ url = url._replace(path = " /control" + url.path)
122+ elif cookie and f ' { NAME } =test ' in cookie:
123+ url = url._replace(path = " /test" + url.path)
124+ else :
125+ # If there is no cookie, this is a new client. Choose a group and set the cookie.
126+ group = " test" if random.random() < 0.5 else " control"
127+ if group == " control" :
128+ url = url._replace(path = " /control" + url.path)
129+ else :
130+ url = url._replace(path = " /test" + url.path)
131+
132+ # Reconstruct response to avoid immutability
133+ res = await fetch(urlunparse(url))
134+ headers = dict (res.headers)
135+ headers[" Set-Cookie" ] = f ' { NAME } = { group} ; path=/ '
136+ return Response(res.body, headers = headers)
137+
138+ return fetch(urlunparse(url))
138139```
139140
140141</TabItem > <TabItem label = " Hono" icon = " seti:typescript" >
0 commit comments