1#  Licensed to the Apache Software Foundation (ASF) under one
2#  or more contributor license agreements.  See the NOTICE file
3#  distributed with this work for additional information
4#  regarding copyright ownership.  The ASF licenses this file
5#  to you under the Apache License, Version 2.0 (the
6#  "License"); you may not use this file except in compliance
7#  with the License.  You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11#  Unless required by applicable law or agreed to in writing, software
12#  distributed under the License is distributed on an "AS IS" BASIS,
13#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#  See the License for the specific language governing permissions and
15#  limitations under the License.
16
17import os
18
19
20Test.Summary = '''
21Test TS API.
22'''
23
24Test.SkipUnless(
25    Condition.HasCurlFeature('http2'),
26)
27Test.ContinueOnFail = True
28
29# test_tsapi.so will output test logging to this file.
30Test.Env["OUTPUT_FILE"] = Test.RunDirectory + "/log.txt"
31
32server = Test.MakeOriginServer("server")
33
34request_header = {
35    "headers": "GET / HTTP/1.1\r\nHost: doesnotmatter\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
36response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": "112233"}
37server.addResponse("sessionlog.json", request_header, response_header)
38
39request_header = {
40    "headers": "GET /xYz HTTP/1.1\r\nHost: doesnotmatter\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
41response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": "445566"}
42server.addResponse("sessionlog.json", request_header, response_header)
43
44# Disable the cache to make sure each request is forwarded to the origin
45# server.
46ts = Test.MakeATSProcess("ts", select_ports=True, enable_tls=True, enable_cache=False)
47
48ts.addDefaultSSLFiles()
49
50ts.Disk.records_config.update({
51    'proxy.config.proxy_name': 'Poxy_Proxy',  # This will be the server name.
52    'proxy.config.ssl.server.cert.path': '{0}'.format(ts.Variables.SSLDir),
53    'proxy.config.ssl.server.private_key.path': '{0}'.format(ts.Variables.SSLDir),
54    'proxy.config.url_remap.remap_required': 1,
55    'proxy.config.diags.debug.enabled': 1,
56    'proxy.config.diags.debug.tags': 'http|test_tsapi',
57})
58
59ts.Disk.ssl_multicert_config.AddLine(
60    'dest_ip=* ssl_cert_name=server.pem ssl_key_name=server.key'
61)
62
63rp = os.path.join(Test.TestDirectory, '.libs', 'test_tsapi.so')
64
65ts.Disk.remap_config.AddLine(
66    "map http://myhost.test http://127.0.0.1:{0} @plugin={1} @plugin={1}".format(server.Variables.Port, rp)
67)
68ts.Disk.remap_config.AddLine(
69    "map https://myhost.test:123 http://127.0.0.1:{0} @plugin={1} @plugin={1}".format(server.Variables.Port, rp)
70)
71
72tr = Test.AddTestRun()
73# Probe server port to check if ready.
74tr.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port))
75tr.Processes.Default.StartBefore(Test.Processes.ts)
76#
77tr.Processes.Default.Command = (
78    'curl --verbose --ipv4 --header "Host: mYhOsT.teSt" hTtP://loCalhOst:{}/'.format(ts.Variables.port)
79)
80tr.Processes.Default.ReturnCode = 0
81
82tr = Test.AddTestRun()
83tr.Processes.Default.Command = (
84    'curl --verbose --ipv4 --proxy localhost:{} http://mYhOsT.teSt/xYz'.format(ts.Variables.port)
85)
86tr.Processes.Default.ReturnCode = 0
87
88tr = Test.AddTestRun()
89tr.Processes.Default.Command = (
90    'curl --verbose --ipv4 --http2 --insecure --header ' +
91    '"Host: myhost.test:123" HttPs://LocalHost:{}/'.format(ts.Variables.ssl_port)
92)
93tr.Processes.Default.ReturnCode = 0
94
95tr = Test.AddTestRun()
96# Change server port number (which can vary) to a fixed string for compare to gold file.
97tr.Processes.Default.Command = "sed 's/{}/SERVER_PORT/' < log.txt > log2.txt".format(server.Variables.Port)
98tr.Processes.Default.ReturnCode = 0
99f = tr.Disk.File("log2.txt")
100f.Content = "log.gold"
101