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
19Test.Summary = '''
20Test combo_handler plugin
21'''
22
23# Skip if plugin not present.
24#
25Test.SkipUnless(
26    Condition.PluginExists('combo_handler.so'),
27)
28
29# Function to generate a unique data file path (in the top level of the test's run directory),  put data (in string 'data') into
30# the file, and return the file name.
31#
32_data_file__file_count = 0
33
34
35def data_file(data):
36    global _data_file__file_count
37    file_path = Test.RunDirectory + "/tcp_client_in_{}".format(_data_file__file_count)
38    _data_file__file_count += 1
39    with open(file_path, "x") as f:
40        f.write(data)
41    return file_path
42
43# Function to return command (string) to run tcp_client.py tool.  'host' 'port', and 'file_path' are the parameters to tcp_client.
44#
45
46
47def tcp_client_cmd(host, port, file_path):
48    return "python3 {}/tcp_client.py {} {} {}".format(Test.Variables.AtsTestToolsDir, host, port, file_path)
49
50# Function to return command (string) to run tcp_client.py tool.  'host' and 'port' are the first two parameters to tcp_client.
51# 'data' is the data to put in the data file input to tcp_client.
52#
53
54
55def tcp_client(host, port, data):
56    return tcp_client_cmd(host, port, data_file(data))
57
58
59server = Test.MakeOriginServer("server")
60
61
62def add_server_obj(content_type, path):
63    request_header = {
64        "headers": "GET " + path + " HTTP/1.1\r\n" +
65        "Host: just.any.thing\r\n\r\n",
66        "timestamp": "1469733493.993",
67        "body": ""
68    }
69    response_header = {
70        "headers": "HTTP/1.1 200 OK\r\n" +
71        "Connection: close\r\n" +
72        'Etag: "359670651"\r\n' +
73        "Cache-Control: public, max-age=31536000\r\n" +
74        "Accept-Ranges: bytes\r\n" +
75        "Content-Type: " + content_type + "\r\n" +
76        "\r\n",
77        "timestamp": "1469733493.993",
78        "body": "Content for " + path + "\n"
79    }
80    server.addResponse("sessionfile.log", request_header, response_header)
81
82
83add_server_obj("text/css ; charset=utf-8", "/obj1")
84add_server_obj("text/javascript", "/sub/obj2")
85add_server_obj("text/argh", "/obj3")
86add_server_obj("application/javascript", "/obj4")
87
88ts = Test.MakeATSProcess("ts")
89
90ts.Disk.records_config.update({
91    'proxy.config.diags.debug.enabled': 1,
92    'proxy.config.diags.debug.tags': 'http|combo_handler',
93})
94
95ts.Disk.plugin_config.AddLine("combo_handler.so - - - ctwl.txt")
96
97ts.Disk.remap_config.AddLine(
98    'map http://xyz/ http://127.0.0.1/ @plugin=combo_handler.so'
99)
100ts.Disk.remap_config.AddLine(
101    'map http://localhost/127.0.0.1/ http://127.0.0.1:{}/'.format(server.Variables.Port)
102)
103ts.Disk.remap_config.AddLine(
104    'map http://localhost/sub/ http://127.0.0.1:{}/sub/'.format(server.Variables.Port)
105)
106
107ts.Disk.File(ts.Variables.CONFIGDIR + "/ctwl.txt", id="ctwl_cfg", typename="ats:config")
108ts.Disk.ctwl_cfg.AddLine("# test  ")
109ts.Disk.ctwl_cfg.AddLine("")
110ts.Disk.ctwl_cfg.AddLine("		text/javascript  # test side comment")
111ts.Disk.ctwl_cfg.AddLine("  application/javascript")
112ts.Disk.ctwl_cfg.AddLine("text/css")
113
114tr = Test.AddTestRun()
115tr.Processes.Default.StartBefore(ts)
116tr.Processes.Default.StartBefore(server)
117tr.Processes.Default.Command = "echo start stuff"
118tr.Processes.Default.ReturnCode = 0
119
120tr = Test.AddTestRun()
121tr.Processes.Default.Command = tcp_client("127.0.0.1", ts.Variables.port,
122                                          "GET /admin/v1/combo?obj1&sub:obj2&obj3 HTTP/1.1\n" +
123                                          "Host: xyz\n" +
124                                          "Connection: close\n" +
125                                          "\n"
126                                          )
127tr.Processes.Default.ReturnCode = 0
128f = tr.Disk.File("_output/1-tr-Default/stream.all.txt")
129f.Content = "combo_handler_files/tr1.gold"
130
131tr = Test.AddTestRun()
132tr.Processes.Default.Command = tcp_client("127.0.0.1", ts.Variables.port,
133                                          "GET /admin/v1/combo?obj1&sub:obj2&obj4 HTTP/1.1\n" +
134                                          "Host: xyz\n" +
135                                          "Connection: close\n" +
136                                          "\n"
137                                          )
138tr.Processes.Default.ReturnCode = 0
139f = tr.Disk.File("_output/2-tr-Default/stream.all.txt")
140f.Content = "combo_handler_files/tr2.gold"
141
142ts.Disk.diags_log.Content = Testers.ContainsExpression("ERROR", "Some tests are failure tests")
143