#  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 a configuration without negative caching enabled.
#

meta:
  version: "1.0"

  blocks:
  - request_for_path_200: &request_for_path_200
      client-request:
        method: "GET"
        version: "1.1"
        scheme: "http"
        url: /path/200
        headers:
          fields:
          - [ Host, example.com ]

        # Add a delay so ATS has time to finish any caching IO for the previous
        # transaction.
        delay: 100ms

  - request_for_path_404: &request_for_path_404
      client-request:
        method: "GET"
        version: "1.1"
        scheme: "http"
        url: /path/404
        headers:
          fields:
          - [ Host, example.com ]

        # Add a delay so ATS has time to finish any caching IO for the previous
        # transaction.
        delay: 100ms

  - request_for_no_cache_control_response: &request_for_no_cache_control_response
      client-request:
        method: "GET"
        version: "1.1"
        scheme: "http"
        url: /path/no_cache_control
        headers:
          fields:
          - [ Host, example.com ]

        # Add a delay so ATS has time to finish any caching IO for the previous
        # transaction.
        delay: 100ms


  - request_for_404_with_cc: &request_for_404_with_cc
      client-request:
        method: "GET"
        version: "1.1"
        scheme: "http"
        url: /path/404_with_cc
        headers:
          fields:
          - [ Host, example.com ]

        # Add a delay so ATS has time to finish any caching IO for the previous
        # transaction.
        delay: 100ms

sessions:
- transactions:

  #
  # Test 1: Verify that a 200 response is cached.
  #
  - all: { headers: { fields: [[ uuid, 1 ]]}}
    <<: *request_for_path_200

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

    proxy-response:
      status: 200

  - all: { headers: { fields: [[ uuid, 2 ]]}}
    <<: *request_for_path_200

    # This should not go through to the server. Return a non-200 response to
    # verify it is served from cache.
    server-response:
      status: 400
      reason: "Bad Request"
      headers:
        fields:
        - [ Content-Length, 0 ]

    # Expect the cached 200 response.
    proxy-response:
      status: 200

  #
  # Test 2: Verify that a 404 response is not cached.
  #
  - all: { headers: { fields: [[ uuid, 3 ]]}}
    <<: *request_for_path_404

    server-response:
      status: 404
      reason: "Not Found"
      headers:
        fields:
        - [ Content-Length, 8 ]

    proxy-response:
      status: 404

  - all: { headers: { fields: [[ uuid, 4 ]]}}
    <<: *request_for_path_404

    # 404 responses should not be cached. Verify this goes to the server
    # by returning and expecting a 200 response.
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 16 ]
        - [ Cache-Control, max-age=300 ]

    # Expect the non-cached, origin server 200 response.
    proxy-response:
      status: 200

  #
  # Test 3: Verify that without Cache-Control, a 200 response is not cached.
  #
  - all: { headers: { fields: [[ uuid, 5 ]]}}
    <<: *request_for_no_cache_control_response

    # Reply without a cache-control header. Should not be cached.
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 16 ]

    proxy-response:
      status: 200

  - all: { headers: { fields: [[ uuid, 6 ]]}}
    <<: *request_for_no_cache_control_response

    # Expect this to go to the origin server since the previous 200 should not
    # have been cached.
    server-response:
      status: 404
      reason: "Not Found"
      headers:
        fields:
        - [ Content-Length, 8 ]

    # Since there was no Cache-Control, expect the non-cached, origin server
    # 404 response.
    proxy-response:
      status: 404

  #
  # Test 4: Verify that a negative response is cached if it has a Cache-Control
  # header. Such a header indicates that the server thinks this is OK to cache.
  #
  - all: { headers: { fields: [[ uuid, 7 ]]}}
    <<: *request_for_404_with_cc

    # Reply with a negative response containing a Cache-Control header.
    server-response:
      status: 404
      reason: "Not Found"
      headers:
        fields:
        - [ Content-Length, 16 ]
        - [ Cache-Control, max-age=300 ]

    proxy-response:
      status: 404

  - all: { headers: { fields: [[ uuid, 8 ]]}}
    <<: *request_for_404_with_cc

    # This should be served out of cache, therefore the origin server should
    # not see this.
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 16 ]

    # Expect the cached 404 response.
    proxy-response:
      status: 404
