#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

#
# This replay file assumes that caching is enabled and
# proxy.config.http.cache.ignore_client_cc_max_age is set to 0 so that we can
# test max-age in the client requests.
#

meta:
  version: "1.0"

  blocks:
  - 200_ok_response: &200_ok_response
      server-response:
        status: 200
        reason: OK
        headers:
          fields:
          - [ Content-Length, 16 ]
          - [ Cache-Control, max-age=300 ]

  - 404_ok_response: &404_ok_response
      server-response:
        status: 404
        reason: "Not Found"
        headers:
          fields:
          - [ Content-Length, 16 ]
          - [ Cache-Control, max-age=300 ]

sessions:
- transactions:

  # A simply path
  - client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /index.html
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 1 ]

    <<: *200_ok_response

    proxy-response:
      status: 200

  # An initial // without an authority is not valid per RFC 3986 section-3.3,
  # but we have historically accepted it and will continue to do so.
  - client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: //index.html
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 2 ]

    <<: *200_ok_response

    proxy-response:
      status: 200

  # A '^' is an invalid path.
  - client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: ^
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 3 ]

    <<: *200_ok_response

    proxy-response:
      status: 400

- transactions:
  #
  # Make sure that we consistently treat encoding of paths.
  #
  - client-request:
      method: "GET"
      version: "1.1"
      url: /path%2ffoo
      headers:
        fields:
        - [ Host, test.com ]
        - [ Connection, keep-alive ]
        - [ uuid, encoded_path ]

    proxy-request:
      # Verify that the origin gets the encoded path.
      url: /path%2ffoo

    <<: *404_ok_response

    proxy-response:
      status: 404

  # Request with an unencoded path: go to origin.
  - client-request:
      # Give plenty of time to cache the response.
      delay: 200ms

      method: "GET"
      version: "1.1"
      # Note that the path is not encoded.
      url: /path/foo
      headers:
        fields:
        - [ Host, test.com ]
        - [ Connection, keep-alive ]
        - [ uuid, unencoded_path ]

    proxy-request:
      url: /path/foo

    <<: *200_ok_response

    proxy-response:
      # ATS should treat the unencoded path as different and go to the origin,
      # not serve the encoded path out of cache.
      status: 200

  # Request again with an encoded path, should be served from cache.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /path%2ffoo
      headers:
        fields:
        - [ Host, test.com ]
        - [ Connection, keep-alive ]
        - [ uuid, encoded_path_again ]

    # The origin should not receive this request, but if it does, reply with a
    # non-404 response so we can detect it.
    <<: *200_ok_response

    proxy-response:
      # ATS should serve the encoded path response from cache.
      status: 404

  # Request again with an encoded path, should be served from cache.
  - client-request:
      # Give plenty of time to cache the unencoded response.
      delay: 200ms

      method: "GET"
      version: "1.1"
      # Note that the path is not encoded.
      url: /path/foo
      headers:
        fields:
        - [ Host, test.com ]
        - [ Connection, keep-alive ]
        - [ uuid, unencoded_path_again ]

    # The origin should not receive this request, but if it does, reply with a
    # non-200 response so we can detect it.
    <<: *404_ok_response

    proxy-response:
      # Verify that ATS returns out of cache for the uneencoded path object.
      status: 200
