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

meta:
  version: "1.0"


sessions:
- transactions:

  # Populate the cache with a response to a GET request.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 1 ]

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

    proxy-response:
      status: 200

  # Verify that we reply to the request out of the cache.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 2 ]

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

    # The request should be served out of cache, so this 403 should not be
    # received.
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]

    # ATS should serve the cached 200 response.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: first_get_response, as: equal} ]

  # Verify that we reply to the request out of the cache with HEAD requests.
  - client-request:
      method: "HEAD"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 3 ]

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

    # The request should be served out of cache, so this 403 should not be
    # received.
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]

    # ATS should serve the cached 200 response.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: first_get_response, as: equal} ]

  # Since the HEAD response was served from cache and didn't invalidate object,
  # the cached response for the GET method should still be valid.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 4 ]

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

    # The server should not receive this request because ATS should reply out
    # of the cache.
    server-response:
      status: 500
      reason: Internal Server Error
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ X-Response, internal_server_error ]

    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: first_get_response, as: equal} ]

  # Test 2: Verify HEAD doesn't cache and only served from cache when it's from GET
  - client-request:
      method: "HEAD"
      version: "1.1"
      url: /some/head/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 5 ]

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

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

    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: first_head_response, as: equal} ]

  # Repeat the request again, it should not be served from cache.
  - client-request:
      method: "HEAD"
      version: "1.1"
      url: /some/head/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 6 ]

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

    # This should not be served out of cache
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ Cache-Control, max-age=300 ]
        - [ X-Response, second_head_response ]

    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: second_head_response, as: equal} ]

  # GET request should also be from server and this time it will be cached
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/head/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 7 ]

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

    # The server should not receive this request because ATS should reply out
    # of the cache.
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ Cache-Control, max-age=300 ]
        - [ X-Response, second_get_response ]

    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: second_get_response, as: equal} ]

  # Verify that we reply to the request out of the cache.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/head/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 8 ]

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

    # The request should be served out of cache, so this 403 should not be
    # received.
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]

    # ATS should serve the cached 200 response.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: second_get_response, as: equal} ]

  # Verify that we reply to the request out of the cache with HEAD requests.
  - client-request:
      method: "HEAD"
      version: "1.1"
      url: /some/head/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 9 ]

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

    # The request should be served out of cache, so this 403 should not be
    # received.
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]

    # ATS should serve the cached 200 response.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: second_get_response, as: equal} ]