
set testfilename1 cookies.rvt

::tcltest::test cookies-1.1 {Cookies} \
    -setup {
        unset -nocomplain page
    } \
    -body {
        set page [::http::geturl "${urlbase}$testfilename1" -headers {Cookie "foo=bar"}]
        regexp -line {^cookies\(foo\) = bar$} [::http::data $page]
    } \
    -cleanup {
        if {[info exists page]} {
            ::http::cleanup $page
        }
        unset -nocomplain page
    } \
    -result 1

# This test sends raw non-ASCII characters in an HTTP header. Keep it disabled
# until the expected header encoding has been defined.
#::tcltest::test cookies-1.2 {Cookies + I18N} \
#    -setup {
#        unset -nocomplain page
#    } \
#    -body {
#        set page [::http::geturl "${urlbase}$testfilename1" \
#            -headers {Cookie "Más=Tú"}]
#        regexp -line {^cookies\(Más\) = Tú$} [::http::data $page]
#    } \
#    -cleanup {
#        if {[info exists page]} {
#            ::http::cleanup $page
#        }
#        unset -nocomplain page
#    } \
#    -result 1

::tcltest::test cookies-1.3 {Cookies + UTF-8 + encoding} \
    -setup {
        unset -nocomplain page data
    } \
    -body {
        set page [::http::geturl "${urlbase}$testfilename1" \
            -headers [list Cookie [::http::formatQuery Más Tú]]]
        set data [::rivet::unescape_string [::http::data $page]]
        regexp -line {^cookies\(Más\) = Tú$} $data
    } \
    -cleanup {
        if {[info exists page]} {
            ::http::cleanup $page
        }
        unset -nocomplain page data
    } \
    -result 1

::tcltest::test cookies-1.4 {Multiple Cookies} \
    -setup {
        unset -nocomplain page data matches
    } \
    -body {
        set page [::http::geturl "${urlbase}$testfilename1" \
            -headers {Cookie "bop; foo=bar;doo=wah; shoo=be ;doooo=bee;dot=dow  "}]
        set data [::http::data $page]
        set matches 0
        incr matches [regexp -line {^cookies\(foo\) = bar$} $data]
        incr matches [regexp -line {^cookies\(doo\) = wah} $data]
        incr matches [regexp -line {^cookies\(shoo\) = be} $data]
        incr matches [regexp -line {^cookies\(doooo\) = bee} $data]
        incr matches [regexp -line {^cookies\(dot\) = dow$} $data]
        incr matches [regexp -line {^cookies\(bop\) = } $data]
    } \
    -cleanup {
        if {[info exists page]} {
            ::http::cleanup $page
        }
        unset -nocomplain page data matches
    } \
    -result 6

::tcltest::test servercookies-1.1 {Cookies from Server} \
    -setup {
        unset -nocomplain page state responseHeaders
    } \
    -body {
        set page [::http::geturl "${urlbase}$testfilename1"]
        upvar 0 $page state

        set found 0
        foreach {name value} $state(meta) {
            if {[string equal -nocase $name "set-cookie"] &&
                [regexp {mod=rivet; expires=01-07-2026} $value]} {
                  set found 1
                  break
            }
        }
        set found
    } \
    -cleanup {
        if {[info exists page]} {
            ::http::cleanup $page
        }
        unset -nocomplain page state responseHeaders
    } \
    -result 1
