_acs-tcl__util_http_json_encoding (private)

 _acs-tcl__util_http_json_encoding

Defined in packages/acs-tcl/tcl/test/http-client-procs.tcl

Partial Call Graph (max 5 caller/called nodes):
%3 aa_equals aa_equals (public) aa_log aa_log (public) aa_log_result aa_log_result (public) aa_run_with_teardown aa_run_with_teardown (public) aa_section aa_section (public) _acs-tcl__util_http_json_encoding _acs-tcl__util_http_json_encoding _acs-tcl__util_http_json_encoding->aa_equals _acs-tcl__util_http_json_encoding->aa_log _acs-tcl__util_http_json_encoding->aa_log_result _acs-tcl__util_http_json_encoding->aa_run_with_teardown _acs-tcl__util_http_json_encoding->aa_section

Testcases:
No testcase defined.
Source code:
        
        set _aa_export {}
        set body_count 1
        foreach testcase_body {{
        set endpoint_name /acs-tcl-test-http-client-procs-util-http-json-encoding
        set url [::acs::test::url]
        #
        # Fallback to util_current_location if ad_url returns an empty string,
        # such in cases when the SystemUrl is not set.
        #
        if {$url eq ""} {
            set url [util_current_location]
        }
        set url $url$endpoint_name

        set response {{key1: "äöü", key2: "äüö", key3: "Ilić"}}

        set methods {POST GET}
        set impls {curl native}

        aa_log "Will execute test on URL: '$url'"

        aa_run_with_teardown -test_code {
            set tcl9 [string match 9* $::tcl_version]
            foreach m $methods {
                aa_section "$m requests"
                foreach impl $impls {
                    aa_section "$impl implementation"
                    ns_register_proc $m $endpoint_name [subst {
                        ns_return 200 application/json {$response}
                    }]
                    aa_log "Request with correct application/json mime_type"
                    aa_log "... [list util::http::[string tolower $m] -preference $impl -url $url]"
                    set r [util::http::[string tolower $m] -preference $impl -url $url]
                    set headers [dict get $r headers]
                    set content_type [expr {[dict exists $headers content-type] ?
                                            [dict get $headers content-type] : [dict get $headers Content-Type]}]
                    aa_true "Content-type is application/json" [string match "*application/json*" $content_type]
                    aa_equals "Response from server is encoded as expected" [dict get $r page] $response

                    # Collect a sample of what is returned when we set
                    # encoding to the default one for application/json
                    # (which by RF4627 SHALL be some unicode version)
                    if {$m eq "GET"} {
                        set F_json [ad_opentmpfile tmpfile_app_json]
                        if {$impl eq "curl"} {
                            aa_log "... running [::util::which curl] $url -k -o -"
                            puts $F_json [exec -ignorestderr [::util::which curl] $url -s -k -o -]
                        } else {
                            ns_http run -method GET -spoolsize 0 -outputchan $F_json $url
                        }
                        close $F_json
                    }

                    ns_register_proc $m $endpoint_name [subst {
                        ns_return 200 "application/json;charset=UTF-8" {$response}
                    }]
                    aa_log "Request with correct application/json;charset=UTF-8 mime_type"
                    aa_log "... running [list util::http::[string tolower $m] -preference $impl -url $url]"
                    set r [util::http::[string tolower $m] -preference $impl -url $url]
                    set headers [dict get $r headers]
                    set content_type [expr {[dict exists $headers content-type] ?
                                            [dict get $headers content-type] : [dict get $headers Content-Type]}]
                    aa_true "Content-type is application/json" [string match "*application/json*" $content_type]
                    aa_true "Charset is UTF-8" [string match "*UTF-8*" $content_type]
                    aa_equals "Response from server is encoded as expected" [dict get $r page] $response

                    aa_log "Request with text/plain mime_type"
                    ns_register_proc $m $endpoint_name [subst {
                        ns_return 200 text/plain {$response}
                    }]
                    set r [util::http::[string tolower $m] -preference $impls -url $url]
                    set headers [dict get $r headers]
                    set content_type [expr {[dict exists $headers content-type] ?
                                            [dict get $headers content-type] : [dict get $headers Content-Type]}]
                    aa_true "Content-type '$content_type' is text/plain" [string match "*text/plain*" $content_type]
                    aa_equals "Response from server is encoded as expected" [dict get $r page] $response

                    if {!$tcl9} {
                        aa_log "Request with text/plain mime_type and iso8859-2 charset"
                        ns_register_proc $m $endpoint_name [subst {
                            ns_return 200 "text/plain; charset=iso8859-2" {$response}
                        }]

                        set r [util::http::[string tolower $m] -preference $impls -url $url]
                        set headers [dict get $r headers]
                        set content_type [expr {[dict exists $headers content-type] ?
                                                [dict get $headers content-type] : [dict get $headers Content-Type]}]
                        aa_true "Content-type is text/plain" [string match "*text/plain*" $content_type]
                        aa_true "Charset is iso8859-2" [string match "*iso8859-2*" $content_type]
                        aa_equals "Response from server is encoded as expected" [dict get $r page] $response

                        # Collect a sample of what is returned when we set
                        # encoding of the response to iso8859-2
                        if {$m eq "GET"} {
                            set F_iso8859_2 [ad_opentmpfile tmpfile_iso8859_2]

                            if {$impl eq "curl"} {
                                puts $F_iso8859_2 [exec -ignorestderr [::util::which curl] $url -s -k -o -]
                            } else {
                                ns_http run -method GET -spoolsize 0 -outputchan $F_iso8859_2 $url
                            }
                        }

                        # Here we expose that, when one uses the "naked"
                        # HTTP tool util::http is wrapping, response would
                        # not be automatically translated to the system
                        # encoding.
                        if {[info exists tmpfile_app_json] &&
                            [info exists tmpfile_iso8859_2] &&
                            [file exists $tmpfile_app_json] &&
                            [file exists $tmpfile_iso8859_2]} {
                            set rfd [open $tmpfile_app_json r]
                            set app_json_text [read $rfd]
                            close $rfd

                            set rfd [open $tmpfile_iso8859_2 r]
                            set iso8859_2_text [read $rfd]
                            close $rfd

                            aa_true "Setting the charset actually brings to different content in the response" {$app_json_text ne $iso8859_2_text}
                            file delete -- $tmpfile_app_json $tmpfile_iso8859_2
                        }
                    } else {
                        aa_log "skip test iso8859-2 for tcl9 (unclear semantics)"
                    }
                }
            }

        } -teardown_code {
            ns_unregister_op GET  $endpoint_name
            ns_unregister_op POST $endpoint_name
        }
    }} {
          aa_log "Running testcase body $body_count"
          set ::__aa_test_indent [info level]
          set catch_val [catch $testcase_body msg]
          if {$catch_val != 0 && $catch_val != 2} {
              aa_log_result "fail" "util_http_json_encoding (body $body_count): Error during execution: $msg, stack trace: \n$::errorInfo"
          }
          incr body_count
        }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: