1'''
2Tests that HEAD requests return proper responses
3'''
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#      http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing, software
15#  distributed under the License is distributed on an "AS IS" BASIS,
16#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17#  See the License for the specific language governing permissions and
18#  limitations under the License.
19
20import os
21import sys
22
23Test.Summary = '''
24Tests that HEAD requests return proper responses
25'''
26
27ts = Test.MakeATSProcess("ts")
28
29HOST = 'www.example.test'
30
31server = Test.MakeOriginServer("server")
32
33ts.Disk.remap_config.AddLine(
34    'map http://{0} http://127.0.0.1:{1}'.format(HOST, server.Variables.Port)
35)
36
37server.addResponse("sessionfile.log", {
38    "headers": "HEAD /head200 HTTP/1.1\r\nHost: {0}\r\n\r\n".format(HOST),
39    "timestamp": "1469733493.993",
40    "body": ""
41}, {
42    "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n",
43    "timestamp": "1469733493.993",
44    "body": "This body should not be returned for a HEAD request."
45})
46
47server.addResponse("sessionfile.log", {
48    "headers": "GET /get200 HTTP/1.1\r\nHost: {0}\r\n\r\n".format(HOST),
49    "timestamp": "1469733493.993",
50    "body": ""
51}, {
52    "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n",
53    "timestamp": "1469733493.993",
54    "body": "This body should be returned for a GET request."
55})
56
57server.addResponse("sessionfile.log", {
58    "headers": "GET /get304 HTTP/1.1\r\nHost: {0}\r\n\r\n".format(HOST),
59    "timestamp": "1469733493.993",
60    "body": ""
61}, {
62    "headers": "HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n",
63    "timestamp": "1469733493.993",
64    "body": ""
65})
66
67
68Test.Setup.Copy(os.path.join(os.pardir, os.pardir, 'tools', 'tcp_client.py'))
69Test.Setup.Copy('data')
70
71trhead200 = Test.AddTestRun("Test domain {0}".format(HOST))
72trhead200.Processes.Default.StartBefore(Test.Processes.ts)
73trhead200.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port))
74trhead200.StillRunningAfter = ts
75trhead200.StillRunningAfter = server
76
77trhead200.Processes.Default.Command = f"{sys.executable} tcp_client.py 127.0.0.1 {ts.Variables.port} data/{HOST}_head_200.txt"
78trhead200.Processes.Default.TimeOut = 5  # seconds
79trhead200.Processes.Default.ReturnCode = 0
80trhead200.Processes.Default.Streams.stdout = "gold/http-head-200.gold"
81
82
83trget200 = Test.AddTestRun("Test domain {0}".format(HOST))
84trget200.StillRunningBefore = ts
85trget200.StillRunningBefore = server
86trget200.StillRunningAfter = ts
87trget200.StillRunningAfter = server
88
89trget200.Processes.Default.Command = f"{sys.executable} tcp_client.py 127.0.0.1 {ts.Variables.port} data/{HOST}_get_200.txt"
90trget200.Processes.Default.TimeOut = 5  # seconds
91trget200.Processes.Default.ReturnCode = 0
92trget200.Processes.Default.Streams.stdout = "gold/http-get-200.gold"
93
94
95trget304 = Test.AddTestRun("Test domain {0}".format(HOST))
96trget304.StillRunningBefore = ts
97trget304.StillRunningBefore = server
98trget304.StillRunningAfter = ts
99trget304.StillRunningAfter = server
100
101cmd_tpl = f"{sys.executable} tcp_client.py 127.0.0.1 {ts.Variables.port} data/{HOST}_get_304.txt"
102trget304.Processes.Default.Command = cmd_tpl
103trget304.Processes.Default.TimeOut = 5  # seconds
104trget304.Processes.Default.ReturnCode = 0
105trget304.Processes.Default.Streams.stdout = "gold/http-get-304.gold"
106