#  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 negative caching configuration in which 400
# responses, and only 400 responses, are cached. This is done via
# `proxy.config.http.negative_caching_list`.
#

meta:
  version: "1.0"

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

sessions:
- transactions:

  #
  # Test 1: Verify that a 404 response is not cached since the custom
  # negative_caching_list excludes it.
  #
  - all: { headers: { fields: [[ uuid, 1 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/404
      headers:
        fields:
        - [ Host, example.com ]

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

    proxy-response:
      status: 404

  # Request the same item again. It should not be cached and the request should
  # be forwarded to the server.
  - all: { headers: { fields: [[ uuid, 2 ]]}}
    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

    # Since 404 responses are customized to not be cached, this will go
    # through.
    <<: *200_response

    # Expect the server's 200 response.
    proxy-response:
      status: 200

  #
  # Test 2: Verify that a 400 response is cached since the custom
  # negative_caching_list includes it.
  #
  - all: { headers: { fields: [[ uuid, 3 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/400
      headers:
        fields:
        - [ Host, example.com ]

    server-response:
      status: 400
      reason: "Bad Request"
      headers:
        fields:
        - [ Content-Length, 8 ]

    proxy-response:
      status: 400

  # Repeat the request and verify the response comes from the cache.
  - all: { headers: { fields: [[ uuid, 4 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/400
      headers:
        fields:
        - [ Host, example.com ]

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

    # By customization, the 400 will be cached and this will not go through.
    <<: *200_response

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

  #
  # Test 3: Verify that a 200 response is cached since it is a non-negative
  # response.
  #
  - all: { headers: { fields: [[ uuid, 5 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/200
      headers:
        fields:
        - [ Host, example.com ]

    <<: *200_response

    proxy-response:
      status: 200

  - all: { headers: { fields: [[ uuid, 6 ]]}}
    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

    # This should not go to the server since the 200 response is cached.
    server-response:
      status: 400
      reason: "Bad Request"
      headers:
        fields:
        - [ Content-Length, 8 ]

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