1'''
2Test the VIA header. This runs several requests through ATS and extracts the upstream VIA headers.
3Those are then checked against a gold file to verify the protocol stack based output is correct.
4'''
5#  Licensed to the Apache Software Foundation (ASF) under one
6#  or more contributor license agreements.  See the NOTICE file
7#  distributed with this work for additional information
8#  regarding copyright ownership.  The ASF licenses this file
9#  to you under the Apache License, Version 2.0 (the
10#  "License"); you may not use this file except in compliance
11#  with the License.  You may obtain a copy of the License at
12#
13#      http://www.apache.org/licenses/LICENSE-2.0
14#
15#  Unless required by applicable law or agreed to in writing, software
16#  distributed under the License is distributed on an "AS IS" BASIS,
17#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18#  See the License for the specific language governing permissions and
19#  limitations under the License.
20
21import os
22
23Test.Summary = '''
24Check VIA header for protocol stack data.
25'''
26
27Test.SkipUnless(
28    Condition.HasCurlFeature('http2'),
29    Condition.HasCurlFeature('IPv6')
30)
31Test.ContinueOnFail = True
32
33# Define default ATS
34ts = Test.MakeATSProcess("ts", enable_tls=True)
35server = Test.MakeOriginServer("server", options={'--load': os.path.join(Test.TestDirectory, 'via-observer.py')})
36
37testName = "VIA"
38
39# We only need one transaction as only the VIA header will be checked.
40request_header = {"headers": "GET / HTTP/1.1\r\nHost: www.example.com\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": ""}
42server.addResponse("sessionlog.json", request_header, response_header)
43
44# These should be promoted rather than other tests like this reaching around.
45ts.addDefaultSSLFiles()
46
47ts.Disk.records_config.update({
48    'proxy.config.http.insert_request_via_str': 4,
49    'proxy.config.http.insert_response_via_str': 4,
50    'proxy.config.ssl.server.cert.path': '{0}'.format(ts.Variables.SSLDir),
51    'proxy.config.ssl.server.private_key.path': '{0}'.format(ts.Variables.SSLDir),
52})
53
54ts.Disk.remap_config.AddLine(
55    'map http://www.example.com http://127.0.0.1:{0}'.format(server.Variables.Port)
56)
57ts.Disk.remap_config.AddLine(
58    'map https://www.example.com http://127.0.0.1:{0}'.format(server.Variables.Port, ts.Variables.ssl_port)
59)
60
61ts.Disk.ssl_multicert_config.AddLine(
62    'dest_ip=* ssl_cert_name=server.pem ssl_key_name=server.key'
63)
64
65# Set up to check the output after the tests have run.
66via_log_id = Test.Disk.File("via.log")
67via_log_id.Content = "via.gold"
68
69# Basic HTTP 1.1
70tr = Test.AddTestRun()
71# Wait for the micro server
72tr.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port))
73# Delay on readiness of our ssl ports
74tr.Processes.Default.StartBefore(Test.Processes.ts)
75
76tr.Processes.Default.Command = 'curl --verbose --ipv4 --http1.1 --proxy localhost:{} http://www.example.com'.format(
77    ts.Variables.port)
78tr.Processes.Default.ReturnCode = 0
79
80tr.StillRunningAfter = server
81tr.StillRunningAfter = ts
82
83# HTTP 1.0
84tr = Test.AddTestRun()
85tr.Processes.Default.Command = 'curl --verbose --ipv4 --http1.0 --proxy localhost:{} http://www.example.com'.format(
86    ts.Variables.port)
87tr.Processes.Default.ReturnCode = 0
88
89tr.StillRunningAfter = server
90tr.StillRunningAfter = ts
91
92# HTTP 2
93tr = Test.AddTestRun()
94tr.Processes.Default.Command = 'curl --verbose --ipv4 --insecure --header "Host: www.example.com" https://localhost:{}'.format(
95    ts.Variables.ssl_port)
96tr.Processes.Default.ReturnCode = 0
97
98tr.StillRunningAfter = server
99tr.StillRunningAfter = ts
100
101# TLS
102tr = Test.AddTestRun()
103tr.Processes.Default.Command = 'curl --verbose --ipv4 --http1.1 --insecure --header "Host: www.example.com" https://localhost:{}'.format(
104    ts.Variables.ssl_port)
105tr.Processes.Default.ReturnCode = 0
106
107tr.StillRunningAfter = server
108tr.StillRunningAfter = ts
109
110# IPv6
111tr = Test.AddTestRun()
112tr.Processes.Default.Command = 'curl --verbose --ipv6 --http1.1 --proxy localhost:{} http://www.example.com'.format(
113    ts.Variables.portv6)
114tr.Processes.Default.ReturnCode = 0
115tr.StillRunningAfter = server
116tr.StillRunningAfter = ts
117
118tr = Test.AddTestRun()
119tr.Processes.Default.Command = 'curl --verbose --ipv6 --http1.1 --insecure --header "Host: www.example.com" https://localhost:{}'.format(
120    ts.Variables.ssl_portv6)
121tr.Processes.Default.ReturnCode = 0
122
123tr.StillRunningAfter = server
124tr.StillRunningAfter = ts
125