#  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} ]

  # Now, repeat the request but this time using the POST method.  The server
  # will reply with a non-error code which will invalidate the cached response.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 3 ]

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

    # Since the method differs, ATS should not reply out of Cache.  Also, the
    # non-error code (200 OK) response should now invalidate the cache to the
    # response to the previous GET request.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: response_to_post, as: equal} ]

  # Now, repeat the GET request. Should not be served out of cache.
  - 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 previous cached response was invalidated by the POST response, so
    # this response should go to the server.
    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} ]

  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/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

    # This should be served out of cache, so we do not expect for the server
    # to reply with this.
    server-response:
      status: 500
      reason: Internal Server Error
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ X-Response, internal_server_error ]

    # Should be served out of the cache.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: second_get_response, as: equal} ]


  # Now, repeat the request with a second POST method. This time the server
  # will reply with an error response, which should not invalidate the cache.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 6 ]

    server-response:
      status: 507
      reason: Insufficient Storage
      headers:
        fields:
        - [ Content-Length, 16 ]
        - [ X-Response, insufficient_storage_response ]

    # Since the method differs, ATS should not reply out of Cache.  Also, the
    # error code response should not invalidate the cache to the response to
    # the previous GET request.
    proxy-response:
      status: 507
      headers:
        fields:
        - [ X-Response, { value: insufficient_storage_response, as: equal} ]

  # Since the POST response was an error code, 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, 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: 500
      reason: Internal Server Error
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ X-Response, internal_server_error ]

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