#! /usr/bin/env tclsh # Integration tests for jsonwatch. # ============================================================================== # Copyright (c) 2020, 2023-2024 D. Bohdan and contributors listed in AUTHORS # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # ============================================================================== package require Tcl 8.6 package require Expect 5 package require tcltest 2 source [file dirname [info script]]/vendor/wapp/wapp.tcl # Expect settings. set max_match 100000 set timeout 5 # Our own settings. set env_var JSONWATCH_COMMAND set host 127.0.0.1 set port 8015 if {![info exists ::env($env_var)]} { puts stderr [list environment variable $env_var must be set] exit 2 } set binary $::env($env_var) if {![file exists $binary]} { puts stderr [list file $binary from environment variable $env_var doesn't exist] exit 2 } # CLI smoke tests. tcltest::test cli-1.1 {} -body { exec $binary -h } -match regexp -result ***:(?i)usage tcltest::test cli-1.2 {} -body { exec $binary --nonsense } -returnCodes error -match glob -result * tcltest::test cli-1.3 {} -body { exec $binary url http://example.com command false } -returnCodes error -match glob -result * # Command tests. tcltest::test command-1.1 {} -body { spawn $binary command cat tests/weather1.json expect { -regexp {"description":\s*"light shower sleet"} { return matched } timeout { return {timed out} } } } -cleanup close -result matched tcltest::test command-1.2 {} -setup {set timeout 2} -body { spawn $binary -n 1 --no-initial-values command cat tests/weather1.json expect { -regexp {[A-Za-z0-9]} { return text } timeout { return {timed out} } } } -cleanup {close; set timeout 5} -result {timed out} tcltest::test command-1.3 {} -body { set ch [file tempfile path] puts $ch {[1,2,3,4,5]} flush $ch spawn $binary -n 1 command sh -c "cat '$path'" expect { -glob *1*2*3*4*5* {} timeout { return {first timeout} } } seek $ch 0 puts $ch {[1,2,3,4,5,6,7]} flush $ch expect \ -glob *[clock format [clock seconds] -format %Y-%m]*6*7* {} \ timeout { return {second timeout} } \ close $ch lindex completed } -cleanup {close; file delete $path} -result completed ### URL tests. proc wapp-page-timestamp {} { wapp-mimetype application/json wapp-subst {{"timestamp": %string([clock seconds])}} } set count 0 proc wapp-page-alternate {} { wapp-mimetype application/json if {$::count % 2} { wapp {["foo", "baz"]} } else { wapp {["bar"]} } incr ::count } proc wapp-page-nested-obj {} { wapp-mimetype application/json if {$::count % 2} { wapp {{"k": "v"}} } else { wapp {{"k": {"nested": "v2"}}} } incr ::count } proc wapp-page-headers {} { wapp-mimetype application/json wapp [format \ {{"X-Bar": "%s", "X-Foo": "%s"}} \ [wapp-param .hdr:X-BAR] \ [wapp-param .hdr:X-FOO] \ ] } wapp-start [list -fromip 127.0.0.1 -nowait -server $port -trace] tcltest::test url-1.1 {} -body { spawn $binary --interval 1 u http://$host:$port/timestamp set re {{\s*"timestamp":\s*\d+\s*}} expect \ -regexp $re { lindex matched } \ timeout { lindex {first timeout} } \ set re2 [format {%1$s[\dT:+-]+ .timestamp: \d+ -> \d+} \ [clock format [clock seconds] -format %Y-%m] \ ] expect \ -regexp $re2 { lindex matched } \ timeout { lindex {second timeout} } \ } -cleanup close -result matched tcltest::test url-1.2 {} -body { spawn $binary -n 1 --no-date url http://$host:$port/timestamp expect { -regexp {\n.timestamp: \d+ -> \d+} { lindex matched } timeout { lindex {timed out} } } } -cleanup close -result matched tcltest::test url-1.3 {} -body { spawn $binary -I -n 1 url http://$host:$port/alternate expect { -regexp { ?\.0: "?bar"? -> "?foo"?\s+ ?\+ .1: "?baz"?} { lindex matched } timeout { lindex {timed out} } } } -cleanup close -result matched tcltest::test url-1.4 {} -body { spawn $binary -n 1 --no-initial-values url http://$host:$port/nested-obj expect { -regexp { ?- \.k\.nested: "v2"\s+ \+ \.k: "v"} { lindex matched } timeout { lindex {timed out} } } } -cleanup close -result matched tcltest::test url-1.5 {} -body { spawn $binary \ --interval 1 \ url \ -H {X-Foo: This is foo. } \ --header X-Bar:this-is-bar \ http://$host:$port/headers \ ; expect { -regexp {this-is-bar.*This is foo\."} { lindex matched } timeout { lindex {timed out} } } } -cleanup close -result matched # Exit with a nonzero status if there are failed tests. set failed [expr {$tcltest::numTests(Failed) > 0}] tcltest::cleanupTests if {$failed} { exit 1 }