# request_headers.test --
#
# Tests explicit outgoing request header customization.
#
# Copyright (c) 2024-2026 Massimo Manghi
#
# SPDX-License-Identifier: TCL
#
# See the file "license.terms" at the top level of this distribution
# for information on usage and redistribution of this file, and for the
# complete disclaimer of warranties and limitation of liability.
#
# Portions of this file were drafted or revised with AI assistance
# under the direction and review of Massimo Manghi

package require tcltest
namespace import ::tcltest::*

source [file join [file dirname [file normalize [info script]]] support.tcl]
source [file join [file dirname [file normalize [info script]]] http_common.tcl]

::tclcurl::test::load_package

test request_headers-1.1 {-httpheader sends a custom request header} -constraints http_server -body {
    set body {}
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::base_url request-inspect] \
                          -httpheader [list "Expect:" "X-TclCurl-Test: hello"] \
                          -bodyvar body \
                          -noprogress 1
        set rc [$handle perform]
        set response [::tclcurl::test::parse_server_response $body]
        list $rc [$handle getinfo responsecode] \
                 [dict get $response x-tclcurl-test]
    }
} -result {0 200 hello}

test request_headers-1.2 {-useragent sends the configured User-Agent header} -constraints http_server -body {
    set body {}
    set agent "TclCurl Test Agent/1.0"
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::base_url request-inspect] \
                          -useragent $agent \
                          -bodyvar body \
                          -noprogress 1
        set rc [$handle perform]
        set response [::tclcurl::test::parse_server_response $body]
        list $rc \
             [$handle getinfo responsecode] \
             [dict get $response user-agent]
    }
} -result {0 200 {TclCurl Test Agent/1.0}}

test request_headers-1.3 {-referer sends the configured Referer header} -constraints http_server -body {
    set body {}
    set refererUrl "https://example.test/origin"
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::base_url request-inspect] \
                          -referer $refererUrl \
                          -bodyvar body \
                          -noprogress 1
        set rc [$handle perform]
        set response [::tclcurl::test::parse_server_response $body]
        list $rc \
             [$handle getinfo responsecode] \
             [dict get $response referer]
    }
} -result {0 200 https://example.test/origin}

cleanupTests
