# https.test --
#
# Tests HTTPS transport scenarios shared with the HTTP coverage.
#
# 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

set ::tclcurl::test::https_common_opts [list -cainfo [::tclcurl::test::https_cert_file]]

test https-2.1 {HTTPS GET shares the common GET transport scenario} -constraints https_server -body {
    ::tclcurl::test::transport_basic_get ::tclcurl::test::server::https_base_url $::tclcurl::test::https_common_opts
} -result {0 200 1}

test https-2.2 {HTTPS HEAD shares the common HEAD transport scenario} -constraints https_server -body {
    ::tclcurl::test::transport_head ::tclcurl::test::server::https_base_url $::tclcurl::test::https_common_opts
} -result {0 200 1}

test https-2.3 {HTTPS -writeproc shares the common callback delivery scenario} -constraints https_server -body {
    ::tclcurl::test::transport_writeproc ::tclcurl::test::server::https_base_url $::tclcurl::test::https_common_opts
} -cleanup {
    unset -nocomplain ::tclcurl::test::writeBuffer
} -result {0 200 1}

test https-2.4 {HTTPS POST shares the common request body scenario} -constraints https_server -body {
    set payload "firstName=Massimo&lastName=Manghi&city=Parma+Italy"
    ::tclcurl::test::transport_postfields ::tclcurl::test::server::https_base_url $::tclcurl::test::https_common_opts $payload
} -result {0 200 POST application/x-www-form-urlencoded 1 1}

test https-1.1 {HTTPS GET succeeds when certificate verification is disabled} -constraints https_server -body {
    set body {}
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::https_base_url] \
                          -sslverifypeer 0 \
                          -sslverifyhost 0 \
                          -bodyvar body \
                          -noprogress 1
        set rc [$handle perform]
        list $rc \
             [$handle getinfo responsecode] \
             [expr {[::tclcurl::test::normalizedBody $body] eq "tclcurl test server"}]
    }
} -result {0 200 1}

test https-1.2 {HTTPS GET succeeds with -cainfo for the local self-signed certificate} -constraints https_server -body {
    set body {}
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::https_base_url] \
                          -cainfo [::tclcurl::test::https_cert_file] \
                          -bodyvar body \
                          -noprogress 1
        set rc [$handle perform]
        list $rc \
             [$handle getinfo responsecode] \
             [expr {[::tclcurl::test::normalizedBody $body] eq "tclcurl test server"}]
    }
} -result {0 200 1}

test https-1.3 {HTTPS certificate validation fails without a trusted CA} -constraints https_server -body {
    set errbuf {}
    ::tclcurl::test::with_easy_handle handle {
        $handle configure -url [::tclcurl::test::server::https_base_url] \
                          -errorbuffer errbuf \
                          -noprogress 1
        list [catch {$handle perform}] \
             [expr {[string length $errbuf] > 0}]
    }
} -result {1 1}

cleanupTests
