1'''
2'''
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#      http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing, software
14#  distributed under the License is distributed on an "AS IS" BASIS,
15#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16#  See the License for the specific language governing permissions and
17#  limitations under the License.
18
19import time
20
21Test.Summary = '''
22cache_range_requests X-CRR-IMS plugin test
23'''
24
25# Test description:
26# Preload the cache with the entire asset to be range requested.
27# Reload remap rule with cache_range_requests plugin
28# Request content through the cache_range_requests plugin
29
30Test.SkipUnless(
31    Condition.PluginExists('cache_range_requests.so'),
32    Condition.PluginExists('xdebug.so'),
33)
34Test.ContinueOnFail = False
35Test.testName = "cache_range_requests_ims"
36
37# Define and configure ATS
38ts = Test.MakeATSProcess("ts", command="traffic_server")
39
40# Define and configure origin server
41server = Test.MakeOriginServer("server")
42
43# default root
44req_chk = {"headers":
45           "GET / HTTP/1.1\r\n" +
46           "Host: www.example.com\r\n" +
47           "uuid: none\r\n" +
48           "\r\n",
49           "timestamp": "1469733493.993",
50           "body": ""
51           }
52
53res_chk = {"headers":
54           "HTTP/1.1 200 OK\r\n" +
55           "Connection: close\r\n" +
56           "\r\n",
57           "timestamp": "1469733493.993",
58           "body": ""
59           }
60
61server.addResponse("sessionlog.json", req_chk, res_chk)
62
63body = "lets go surfin now"
64bodylen = len(body)
65
66req_full = {"headers":
67            "GET /path HTTP/1.1\r\n" +
68            "Host: www.example.com\r\n" +
69            "Accept: */*\r\n" +
70            "Range: bytes=0-\r\n" +
71            "\r\n",
72            "timestamp": "1469733493.993",
73            "body": ""
74            }
75
76res_full = {"headers":
77            "HTTP/1.1 206 Partial Content\r\n" +
78            "Accept-Ranges: bytes\r\n" +
79            "Cache-Control: max-age=500\r\n" +
80            "Content-Range: bytes 0-{0}/{0}\r\n".format(bodylen) +
81            "Connection: close\r\n" +
82            'Etag: "772102f4-56f4bc1e6d417"\r\n' +
83            "\r\n",
84            "timestamp": "1469733493.993",
85            "body": body
86            }
87
88server.addResponse("sessionlog.json", req_full, res_full)
89
90# cache range requests plugin remap
91ts.Disk.remap_config.AddLine(
92    'map http://www.example.com http://127.0.0.1:{}'.format(server.Variables.Port) +
93    ' @plugin=cache_range_requests.so @pparam=--consider-ims',
94)
95
96# cache debug
97ts.Disk.plugin_config.AddLine('xdebug.so')
98
99# minimal configuration
100ts.Disk.records_config.update({
101    'proxy.config.diags.debug.enabled': 1,
102    'proxy.config.diags.debug.tags': 'cache_range_requests',
103})
104
105curl_and_args = 'curl -s -D /dev/stdout -o /dev/stderr -x localhost:{} -H "x-debug: x-cache"'.format(ts.Variables.port)
106
107# 0 Test - Fetch whole asset into cache
108tr = Test.AddTestRun("0- range cache load")
109ps = tr.Processes.Default
110ps.StartBefore(server, ready=When.PortOpen(server.Variables.Port))
111ps.StartBefore(Test.Processes.ts)
112ps.Command = curl_and_args + ' http://www.example.com/path -r 0-'
113ps.ReturnCode = 0
114ps.Streams.stdout.Content = Testers.ContainsExpression("X-Cache: miss", "expected cache miss for load")
115tr.StillRunningAfter = ts
116
117
118# set up the IMS date field (go in the future) RFC 2616
119futuretime = time.time() + 100  # seconds
120futurestr = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(futuretime))
121
122# test inner range
123# 1 Test - Fetch range into cache
124tr = Test.AddTestRun("0- cache hit check")
125ps = tr.Processes.Default
126ps.Command = curl_and_args + ' http://www.example.com/path -r 0-'.format(futurestr)
127ps.ReturnCode = 0
128ps.Streams.stdout.Content = Testers.ContainsExpression("X-Cache: hit", "expected cache hit")
129tr.StillRunningAfter = ts
130
131# 2 Test - Ensure X-CRR-IMS header results in hit-stale
132tr = Test.AddTestRun("0- range X-CRR-IMS check")
133ps = tr.Processes.Default
134ps.Command = curl_and_args + ' http://www.example.com/path -r 0- -H "X-CRR-IMS: {}"'.format(futurestr)
135ps.ReturnCode = 0
136ps.Streams.stdout.Content = Testers.ContainsExpression("X-Cache: hit-stale", "expected cache hit-stale")
137tr.StillRunningAfter = ts
138