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 os
20Test.Summary = '''
21
22'''
23Test.SkipUnless(Condition.PluginExists('cookie_remap.so'))
24Test.ContinueOnFail = True
25Test.testName = "cookie_remap: cookie exists"
26
27# Define default ATS
28ts = Test.MakeATSProcess("ts")
29
30# First server is run during first test and
31# second server is run during second test
32server = Test.MakeOriginServer("server", ip='127.0.0.10')
33
34request_header = {"headers": "GET /cookieexists HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
35                  "timestamp": "1469733493.993", "body": ""}
36# expected response from the origin server
37response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
38
39# add response to the server dictionary
40server.addResponse("sessionfile.log", request_header, response_header)
41
42server2 = Test.MakeOriginServer("server2", ip='127.0.0.11')
43request_header2 = {"headers": "GET /cookiedoesntexist HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
44                   "timestamp": "1469733493.993", "body": ""}
45# expected response from the origin server
46response_header2 = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
47
48# add response to the server dictionary
49server2.addResponse("sessionfile.log", request_header2, response_header2)
50
51# Setup the remap configuration
52config_path = os.path.join(Test.TestDirectory, "configs/existsconfig.txt")
53with open(config_path, 'r') as config_file:
54    config1 = config_file.read()
55
56ts.Disk.records_config.update({
57    'proxy.config.diags.debug.enabled': 1,
58    'proxy.config.diags.debug.tags': 'cookie_remap.*|http.*|dns.*',
59})
60
61config1 = config1.replace("$PORT", str(server.Variables.Port))
62config1 = config1.replace("$ALTPORT", str(server2.Variables.Port))
63
64ts.Disk.File(ts.Variables.CONFIGDIR + "/existsconfig.txt", exists=False, id="config1")
65ts.Disk.config1.WriteOn(config1)
66
67ts.Disk.remap_config.AddLine(
68    'map http://www.example.com/magic http://shouldnothit.com @plugin=cookie_remap.so @pparam=config/existsconfig.txt'
69)
70
71# Positive test case that remaps because cookie exists
72tr = Test.AddTestRun("cookie fpbeta exists")
73tr.Processes.Default.Command = '''
74curl \
75--proxy 127.0.0.1:{0} \
76"http://www.example.com/magic" \
77-H"Cookie: fpbeta=fdkfkdfkdfkdkdfkdfk" \
78-H "Proxy-Connection: keep-alive" \
79--verbose \
80'''.format(ts.Variables.port)
81tr.Processes.Default.ReturnCode = 0
82tr.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port))
83tr.Processes.Default.StartBefore(Test.Processes.ts)
84tr.StillRunningAfter = ts
85
86server.Streams.All = "gold/existscookie.gold"
87
88# Negative test case that doesn't remap because cookie doesn't exist
89tr = Test.AddTestRun("cooke fpbeta doesnt exist")
90tr.Processes.Default.Command = '''
91curl \
92--proxy 127.0.0.1:{0} \
93"http://www.example.com/magic" \
94-H"Cookie: breadcrumb=etc" \
95-H "Proxy-Connection: keep-alive" \
96--verbose \
97'''.format(ts.Variables.port)
98tr.Processes.Default.ReturnCode = 0
99tr.Processes.Default.StartBefore(server2, ready=When.PortOpen(server2.Variables.Port))
100tr.StillRunningAfter = ts
101
102server2.Streams.All = "gold/doesntexistcookie.gold"
103