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

  #
  # Test 1: Verify caching of a response to a POST request.
  #

  # The simple case: perform two POST requests and make sure the second
  # is served with the cached response of the first.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /simple/post/test
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 11 ]

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

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

  - client-request:
      method: "POST"
      version: "1.1"
      url: /simple/post/test
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 12 ]

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

    # The request should not go to the server, so this 500 response should not
    # be served.
    server-response:
      status: 500
      reason: Internal Server Error
      headers:
        fields:
        - [ Content-Length, 16 ]

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

  #
  # Test 2: Verify correct interaction between cached responses to GET and POST
  # requests when the POST succeeds.
  #

  # 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, 21 ]

      # 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, 16 ]
        - [ Cache-Control, max-age=300 ]
        - [ X-Response, first_get_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.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 22 ]

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

    # 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 for the same resource but this time using the POST
  # method.  The server will reply with a non-error status and should be cached.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 23 ]

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

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

  # Now, repeat the POST request. ATS should reply out of cache.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ Content-Length, 48 ]
        - [ uuid, 24 ]

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

    # The earlier response to the POST request should be returned, not this 403.
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]

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

  # Make sure that we've invalidated our cached GET response, however.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /some/path
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 25 ]

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

    # The response should be served not from the cache but from this
    # server-response.
    server-response:
      status: 200
      reason: OK
      headers:
        fields:
        - [ Content-Length, 16 ]
        - [ Cache-Control, max-age=300 ]
        - [ X-Response, second_get_response ]

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

  #
  # Test 3: Verify error responses to POST requests do not cause cache issues.
  #

  # Populate the cache with a GET response for the resource.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /error/response
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 31 ]

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

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

  # Now request this resource via a POST and respond with an error code.
  - client-request:
      method: "POST"
      version: "1.1"
      url: /error/response
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 32 ]
        - [ Content-Length, 48 ]

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

    # Have the server reply with an error code
    server-response:
      status: 403
      reason: Forbidden
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ X-Response, forbidden_server_error ]

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

  # Now repeat the request via the GET. Make sure that the error response
  # didn't invalidate the cached resource.
  - client-request:
      method: "GET"
      version: "1.1"
      url: /error/response
      headers:
        fields:
        - [ Host, example.com ]
        - [ uuid, 33 ]

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

    # The response should be served out of cache, not via this server response.
    server-response:
      status: 507
      reason: Insufficient Storage
      headers:
        fields:
        - [ Content-Length, 8 ]
        - [ X-Response, insufficient_storage_response ]

    # Expect the cached response to be served.
    proxy-response:
      status: 200
      headers:
        fields:
        - [ X-Response, { value: another_get_response, as: equal} ]
