1'''
2Test cached responses and requests with bodies
3'''
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#      http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing, software
15#  distributed under the License is distributed on an "AS IS" BASIS,
16#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17#  See the License for the specific language governing permissions and
18#  limitations under the License.
19
20Test.Summary = '''
21Test revalidating cached objects
22'''
23
24testName = "RevalidateCacheObject"
25Test.ContinueOnFail = True
26
27# Set up Origin server
28# request_header is from ATS to origin; response from Origin to ATS
29# lookup_key is to make unique response in origin for header "UID" that will pass in ATS request
30server = Test.MakeOriginServer("server", lookup_key="{%UID}")
31# Initial request
32request_header = {"headers": "GET / HTTP/1.1\r\nHost: www.example.com\r\nUID: Fill\r\n\r\n",
33                  "timestamp": "1469733493.993", "body": ""}
34response_header = {
35    "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\nLast-Modified: Tue, 08 May 2018 15:49:41 GMT\r\nCache-Control: max-age=1\r\n\r\n",
36    "timestamp": "1469733493.993",
37    "body": "xxx"}
38server.addResponse("sessionlog.json", request_header, response_header)
39# IMS revalidation request
40request_IMS_header = {
41    "headers": "GET / HTTP/1.1\r\nUID: IMS\r\nIf-Modified-Since: Tue, 08 May 2018 15:49:41 GMT\r\nHost: www.example.com\r\n\r\n",
42    "timestamp": "1469733493.993",
43    "body": ""}
44response_IMS_header = {"headers": "HTTP/1.1 304 Not Modified\r\nConnection: close\r\nCache-Control: max-age=1\r\n\r\n",
45                       "timestamp": "1469733493.993", "body": None}
46server.addResponse("sessionlog.json", request_IMS_header, response_IMS_header)
47
48# EtagFill
49request_etagfill_header = {"headers": "GET /etag HTTP/1.1\r\nHost: www.example.com\r\nUID: EtagFill\r\n\r\n",
50                           "timestamp": "1469733493.993", "body": None}
51response_etagfill_header = {
52    "headers": "HTTP/1.1 200 OK\r\nETag: myetag\r\nConnection: close\r\nCache-Control: max-age=1\r\n\r\n",
53    "timestamp": "1469733493.993",
54    "body": "xxx"}
55server.addResponse("sessionlog.json", request_etagfill_header, response_etagfill_header)
56# INM revalidation
57request_INM_header = {"headers": "GET /etag HTTP/1.1\r\nUID: INM\r\nIf-None-Match: myetag\r\nHost: www.example.com\r\n\r\n",
58                      "timestamp": "1469733493.993", "body": None}
59response_INM_header = {
60    "headers": "HTTP/1.1 304 Not Modified\r\nConnection: close\r\nETag: myetag\r\nCache-Control: max-age=1\r\n\r\n",
61    "timestamp": "1469733493.993",
62    "body": None}
63server.addResponse("sessionlog.json", request_INM_header, response_INM_header)
64
65# object changed to 0 byte
66request_noBody_header = {"headers": "GET / HTTP/1.1\r\nUID: noBody\r\nHost: www.example.com\r\n\r\n",
67                         "timestamp": "1469733493.993", "body": ""}
68response_noBody_header = {
69    "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\nCache-Control: max-age=3\r\n\r\n",
70    "timestamp": "1469733493.993",
71    "body": ""}
72server.addResponse("sessionlog.json", request_noBody_header, response_noBody_header)
73
74# etag object now is a 404. Yeah, 404s don't usually have Cache-Control, but, ATS's default is to cache 404s for a while.
75request_etagfill_header = {"headers": "GET /etag HTTP/1.1\r\nHost: www.example.com\r\nUID: EtagError\r\n\r\n",
76                           "timestamp": "1469733493.993", "body": None}
77response_etagfill_header = {
78    "headers": "HTTP/1.1 404 Not Found\r\nConnection: close\r\nContent-Length: 0\r\nCache-Control: max-age=3\r\n\r\n",
79    "timestamp": "1469733493.993",
80    "body": ""}
81server.addResponse("sessionlog.json", request_etagfill_header, response_etagfill_header)
82
83# ATS Configuration
84ts = Test.MakeATSProcess("ts")
85ts.Disk.plugin_config.AddLine('xdebug.so')
86ts.Disk.records_config.update({
87    'proxy.config.diags.debug.enabled': 1,
88    'proxy.config.diags.debug.tags': 'http',
89    'proxy.config.http.response_via_str': 3,
90})
91
92ts.Disk.remap_config.AddLine(
93    'map / http://127.0.0.1:{0}'.format(server.Variables.Port)
94)
95
96# Test 0 - Fill a 3 byte object with Last-Modified time into cache.
97tr = Test.AddTestRun()
98tr.Processes.Default.StartBefore(server)
99tr.Processes.Default.StartBefore(ts)
100tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: Fill" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/'.format(
101    ts.Variables.port)
102tr.Processes.Default.ReturnCode = 0
103tr.Processes.Default.Streams.stdout = "cache_and_req_body-miss.gold"
104tr.StillRunningAfter = ts
105tr.StillRunningAfter = server
106
107# Test 1 - Once it goes stale, fetch it again. We expect Origin to get IMS
108# request, and serve a 304. We expect ATS to refresh the object, and give
109# a 200 to user
110tr = Test.AddTestRun()
111tr.DelayStart = 2
112tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: IMS" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/'.format(
113    ts.Variables.port)
114tr.Processes.Default.ReturnCode = 0
115tr.Processes.Default.Streams.stdout = "cache_and_req_body-hit-stale.gold"
116tr.StillRunningAfter = ts
117tr.StillRunningAfter = server
118
119# Test 2 - Once it goes stale, fetch it via a range request. We expect
120# Origin to get IMS request, and serve a 304. We expect ATS to refresh the
121# object, and give a 206 to user
122tr = Test.AddTestRun()
123tr.DelayStart = 2
124tr.Processes.Default.Command = 'curl --range 0-1 -s -D - -v --ipv4 --http1.1 -H"UID: IMS" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/'.format(
125    ts.Variables.port)
126tr.Processes.Default.ReturnCode = 0
127tr.Processes.Default.Streams.stdout = "cache_and_req_body-hit-stale-206.gold"
128tr.StillRunningAfter = ts
129tr.StillRunningAfter = server
130
131# Test 3 - Fill a new object with an Etag. Not checking the output here.
132tr = Test.AddTestRun()
133tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: EtagFill" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/etag'.format(
134    ts.Variables.port)
135tr.Processes.Default.ReturnCode = 0
136tr.StillRunningAfter = ts
137tr.StillRunningAfter = server
138
139# Test 4 - Once the etag object goes stale, fetch it again. We expect
140# Origin to get INM request, and serve a 304. We expect ATS to refresh the
141# object, and give a 200 to user
142tr = Test.AddTestRun()
143tr.DelayStart = 2
144tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: INM" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/etag'.format(
145    ts.Variables.port)
146tr.Processes.Default.ReturnCode = 0
147tr.Processes.Default.Streams.stdout = "cache_and_req_body-hit-stale-INM.gold"
148tr.StillRunningAfter = ts
149tr.StillRunningAfter = server
150
151# Test 5 - Once the etag object goes stale, fetch it via a range request.
152# We expect Origin to get INM request, and serve a 304. We expect ATS to
153# refresh the object, and give a 206 to user
154tr = Test.AddTestRun()
155tr.DelayStart = 2
156tr.Processes.Default.Command = 'curl --range 0-1 -s -D - -v --ipv4 --http1.1 -H"UID: INM" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/etag'.format(
157    ts.Variables.port)
158tr.Processes.Default.ReturnCode = 0
159tr.Processes.Default.Streams.stdout = "cache_and_req_body-hit-stale-206-etag.gold"
160tr.StillRunningAfter = ts
161tr.StillRunningAfter = server
162
163# Test 6 - The origin changes the initial LMT object to 0 byte. We expect ATS to fetch and serve the new 0 byte object.
164tr = Test.AddTestRun()
165tr.DelayStart = 3
166tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: noBody" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/'.format(
167    ts.Variables.port)
168tr.Processes.Default.ReturnCode = 0
169tr.Processes.Default.Streams.stdout = "cache_and_req_nobody-hit-stale.gold"
170tr.StillRunningAfter = ts
171tr.StillRunningAfter = server
172
173# Test 7 - Fetch the new 0 byte object again when fresh in cache to ensure its still a 0 byte object.
174tr = Test.AddTestRun()
175tr.DelayStart = 3
176tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: noBody" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/'.format(
177    ts.Variables.port)
178tr.Processes.Default.ReturnCode = 0
179tr.Processes.Default.Streams.stdout = "cache_and_req_nobody-hit-stale.gold"
180tr.StillRunningAfter = ts
181tr.StillRunningAfter = server
182
183# Test 8 - The origin changes the etag object to 0 byte 404. We expect ATS to fetch and serve the 404 0 byte object.
184tr = Test.AddTestRun()
185tr.DelayStart = 2
186tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: EtagError" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/etag'.format(
187    ts.Variables.port)
188tr.Processes.Default.ReturnCode = 0
189tr.Processes.Default.Streams.stdout = "cache_and_error_nobody.gold"
190tr.StillRunningAfter = ts
191tr.StillRunningAfter = server
192
193# Test 9 - Fetch the 0 byte etag object again when fresh in cache to ensure its still a 0 byte object
194tr = Test.AddTestRun()
195tr.DelayStart = 2
196tr.Processes.Default.Command = 'curl -s -D - -v --ipv4 --http1.1 -H"UID: EtagError" -H "x-debug: x-cache,x-cache-key,via" -H "Host: www.example.com" http://localhost:{0}/etag'.format(
197    ts.Variables.port)
198tr.Processes.Default.ReturnCode = 0
199tr.Processes.Default.Streams.stdout = "cache_and_error_nobody.gold"
200tr.StillRunningAfter = ts
201tr.StillRunningAfter = server
202