#  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:

  # All responses should be served out of the cache for this test, so this
  # response from the origin should never been seen.
  - origin_response: &origin_response
      server-response:
        status: 500
        reason: "Internal Server Error"
        headers:
          fields:
          - [ Content-Length, 16 ]
          - [ X-Response, should_not_see ]


sessions:
- transactions:

  # Use a PUSH to populate the cache with a max-age response of 2 seconds.
  - client-request:
      method: "PUSH"
      version: "1.1"
      url: /a/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, push ]
        - [ X-Request, push ]
        - [ Content-Length, 113 ]
      content:
        encoding: plain
        data: "HTTP/1.1 200 OK\nServer: ATS/10.0.0\nAccept-Ranges: bytes\nContent-Length: 6\nCache-Control: public,max-age=2\n\nCACHED"

    <<: *origin_response

    # Verify that ATS confirmed that the PUSH was successful, which it does
    # with a 201 response.
    proxy-response:
      status: 201

  # Issue a GET request after the 2 second max-age. The test configures the
  # parent ATS cache to be non-existent, so it appears as down to this ATS
  # host. After this transaction, failcount == 1.
  - client-request:
      delay: 4s

      method: "GET"
      version: "1.1"
      url: /a/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, first_stale ]
        - [ X-Request, first_stale ]

    <<: *origin_response

    proxy-response:
      status: 200
      headers:
        fields:
          - [ Cache-Control, {value: max-age=2, as: contains } ]

  # Request the stale resource a second time. After this transaction, failcount == 2.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /a/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, second_stale ]
        - [ X-Request, second_stale ]

    <<: *origin_response

    proxy-response:
      status: 200

  # Request the stale resource a third time. Here the failcount will exceed the
  # fail_threshold of 2. ATS will still serve the stale entry, but it now
  # considers the parent unavailable.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /a/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, third_stale ]
        - [ X-Request, third_stale ]

    <<: *origin_response

    proxy-response:
      status: 200

  # Request the stale resource after enough delay to guarantee that the cached
  # object's age exceeds max_stale_age (10 seconds). Note that we already
  # delayed 4 seconds in a previous transaction. ATS should not serve the stale
  # entry anymore because it is too old.
  - client-request:
      delay: 8s

      method: "GET"
      version: "1.1"
      url: /a/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, past_max_age ]
        - [ X-Request, past_max_age ]

    <<: *origin_response

    # At this point, ATS should respond with a 502 since max_stale_age is exceeded.
    proxy-response:
      status: 502
