#  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 with negative caching enabled with
# otherwise default conciguration.
#

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 cached.
  #
  - 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

  # Repeat the request and verify that it is served from the cache.
  - 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

    # 404 responses should be cached when negative caching is enabled, so this
    # should not get to the server.  But if it does, return a 200 so the test
    # knows that something went wrong.
    <<: *200_response

    # Verify the cached 404 response is served.
    proxy-response:
      status: 404

  #
  # Test 2: Verify that a 400 response is not cached.
  #
  - 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 that the request is forwarded to the server,
  # not replied with any incorrectly cached response.
  - 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

    # 400 responses should not be cached. Verify this goes to the server
    # by returning and expecting a 200 response.
    <<: *200_response

    # Verify that the origin's 200 response is served back.
    proxy-response:
      status: 200

  #
  # Test 3: Verify that a 200 response is cached.
  #
  - 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, but if it does return a 400 so we catch
    # it.
    server-response:
      status: 400
      reason: "Bad Request"
      headers:
        fields:
        - [ Content-Length, 8 ]

    # Verify that the cached 200 response is served.
    proxy-response:
      status: 200

  #
  # Test 4: Verify that a 405 response is not cached.
  #
  - all: { headers: { fields: [[ uuid, 7 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/405
      headers:
        fields:
        - [ Host, example.com ]

    server-response:
      status: 405
      reason: "Method Not Allowed"
      headers:
        fields:
        - [ Content-Length, 8 ]

    proxy-response:
      status: 405

  # Repeat the request and verify that the request is forwarded to the server,
  # not replied with any incorrectly cached response.
  - all: { headers: { fields: [[ uuid, 8 ]]}}
    client-request:
      method: "GET"
      version: "1.1"
      scheme: "http"
      url: /path/405
      headers:
        fields:
        - [ Host, example.com ]

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

    # 405 responses should not be cached. Verify this goes to the server
    # by returning and expecting a 200 response.
    <<: *200_response

    # Verify that the origin's 200 response is served back.
    proxy-response:
      status: 200
