local helpers = require "spec.helpers"

for _, strategy in helpers.each_strategy() do
    describe("Plugin: console-header-management [#" .. strategy .. "]", function()
        local proxy_client

        lazy_setup(function()
            local bp = helpers.get_db_utils(strategy, {"routes", "services", "plugins"})

            local service = bp.services:insert({
                protocol = "http",
                host = helpers.mock_upstream_host,
                port = helpers.mock_upstream_port
            })

            local route = bp.routes:insert({
                protocols = {"http", "https"},
                hosts = {"console-header.test"},
                service = service
            })

            bp.plugins:insert({route = {id = route.id}, name = "console-header-management"})

            assert(helpers.start_kong({
                database = strategy,
                nginx_conf = "spec/fixtures/custom_nginx.template",
                plugins = "console-header-management"
            }))
      end)

        lazy_teardown(function()
            helpers.stop_kong()
        end)

        before_each(function()
            proxy_client = helpers.proxy_client()
        end)

        after_each(function()
            if proxy_client then
                proxy_client:close()
            end
        end)

        describe("request headers", function()
            it("should remove all X-Auth-* headers", function()
                local res = assert(proxy_client:send({
                    method = "GET",
                    path = "/status/200",
                    headers = {
                        ["Host"] = "console-header.test",
                        ["X-Auth-Type"] = "open_api",
                        ["X-Auth-Content"] = "valid_content",
                        ["X-Auth-Method"] = "POST",
                        ["X-Auth-Username"] = "test_user"
                    }
                }))
                assert.res_status(200, res)

                -- Verify headers were removed by checking upstream request
                local upstream_req_headers = helpers.get_upstream_request_headers()
                assert.equal("console", upstream_req_headers["x-auth-type"])
                assert.is_nil(upstream_req_headers["x-auth-content"])
                assert.is_nil(upstream_req_headers["x-auth-method"])
                assert.is_nil(upstream_req_headers["x-auth-username"])
            end)

            it("should handle requests with partial X-Auth headers", function()
                local res = assert(proxy_client:send({
                    method = "GET",
                    path = "/status/200",
                    headers = {
                        ["Host"] = "console-header.test",
                        ["X-Auth-Type"] = "jwt",
                        ["X-Auth-Content"] = "another_content"
                    }
                }))
                assert.res_status(200, res)

                local upstream_req_headers = helpers.get_upstream_request_headers()
                assert.equal("console", upstream_req_headers["x-auth-type"])
                assert.is_nil(upstream_req_headers["x-auth-content"])
            end)

            it("should not affect non-X-Auth headers", function()
                local res = assert(proxy_client:send({
                    method = "GET",
                    path = "/status/200",
                    headers = {
                        ["Host"] = "console-header.test",
                        ["Content-Type"] = "application/json",
                        ["Authorization"] = "Bearer token123",
                        ["X-Request-ID"] = "12345"
                    }
                }))
                assert.res_status(200, res)

                local upstream_req_headers = helpers.get_upstream_request_headers()
                assert.equal("application/json", upstream_req_headers["content-type"])
                assert.equal("Bearer token123", upstream_req_headers["authorization"])
                assert.equal("12345", upstream_req_headers["x-request-id"])
            end)
        end)
    end)
  end