Skip to content

Commit

Permalink
-v option fix and wsa:Action handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mico Papp committed Aug 7, 2023
1 parent 4dd347b commit 5c441c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
35 changes: 25 additions & 10 deletions src/soap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
### soap-cli v1.1
soap_cli_version="v1.2"
### soap-cli v1.3
soap_cli_version="v1.3"
### https://github.com/pmamico/soap-cli

### To keep the script standalone, source is divided into parts
Expand Down Expand Up @@ -41,17 +41,18 @@ print_help()
printf '%s\n' "soap-cli $soap_cli_version"
printf "Send SOAP messages from command line. \n"
# shellcheck disable=SC2183
printf 'Usage: %b\n\033[1msoap <endpoint> <request>\033[0m [-i|--interactive] [-u|--update <arg>] [-v|--value <arg>] [-d|--dry] [-h|--help] [-p|--pretty] [--version] [curl options] \n'
printf 'Usage: %b\n\033[1msoap <endpoint> <request>\033[0m [-i|--interactive] [-U|--update <arg>] [-V|--value <arg>] [-d|--dry] [-h|--help] [-p|--pretty] [--version] [curl options] \n'
printf '\t%s\n' "<endpoint>: SOAP endpoint url"
printf '\t%s\n' "<request>: SOAP request file"
printf '\t%s\n' "-i, --interactive: use your XML as template, update values interactively before send"
printf '\t%s\n' "-u, --update: update the the value by given XPath; valid only with --value option"
printf '\t%s\n' "-v, --value: update the the value by given XPath; valid only with --update option"
printf '\t%s\n' "-U, --update: update the the value by given XPath; valid only with --value option"
printf '\t%s\n' "-V, --value: update the the value by given XPath; valid only with --update option"
printf '\t%s\n' "-d, --dry: dry run, prints the curl command but do not execute"
printf '\t%s\n' "-p, --pretty: syntax highlighting"
printf '\t%s\n' "-h, --help: Prints help"
printf '\t%s\n' "--version: Prints version number"
printf '\t%s\n' "All additional arguments and options passed to curl. (see 'curl --help all')"
printf '\t%s\n' "(except -U as proxy user, use --proxy-user instead)"
check_requirements
}

Expand Down Expand Up @@ -92,26 +93,26 @@ parse_commandline()
do
_key="$1"
case "$_key" in
-u|--update)
-U|--update)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_update="$2"
shift
;;
--update=*)
_arg_update="${_key##--update=}"
;;
-u*)
-U*)
_arg_update="${_key##-u}"
;;
-v|--value)
-V|--value)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_value="$2"
shift
;;
--value=*)
_arg_value="${_key##--value=}"
;;
-v*)
-V*)
_arg_value="${_key##-v}"
;;
-i|--interactive)
Expand Down Expand Up @@ -250,6 +251,18 @@ interactive_update(){
printf "\n\nRESPONSE\n"
}

soap_content_type()
{
content_type="application/soap+xml;charset=UTF-8"

action=$(xmlstarlet sel -N wsa="http://www.w3.org/2005/08/addressing" -t -v "//wsa:Action" "$_arg_request")
if [[ "$action" != "" ]]
then
content_type+=";action=\"$action\""
fi
echo "$content_type"
}

#################################
# Part: the actual HTTP request #
#################################
Expand All @@ -261,7 +274,9 @@ soap_call()
file='soap_cli_template.xml'
fi

curl_command="curl -s $_curl_options --request POST --header 'Content-Type: text/xml;charset=UTF-8' -d @$file $_arg_endpoint"
content_type=$(soap_content_type)

curl_command="curl -s $_curl_options --request POST --header 'Content-Type: $content_type' -d @$file $_arg_endpoint"

if [ "$_arg_dry" == "true" ]
then
Expand Down
17 changes: 17 additions & 0 deletions test/request_with_action.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>user</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<wsa:Action>https://github.com/pmamico/soap-cli/issues/13</wsa:Action>
</soap:Header>
<soap:Body>
<NumberToDollars xmlns="http://www.dataaccess.com/webservicesserver/">
<dNum>500</dNum>
</NumberToDollars>
</soap:Body>
</soap:Envelope>
21 changes: 18 additions & 3 deletions test/soap_cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ update_the_request_and_get_response_value() {
}

u_the_request_and_get_response_value() {
"$DIR"/../src/soap "$TEST_ENDPOINT" "$DIR/request.xml" -u "//*[name()='dNum']" -v "123" | xml sel -t -v "//*[name()='m:NumberToDollarsResult']"
"$DIR"/../src/soap "$TEST_ENDPOINT" "$DIR/request.xml" -U "//*[name()='dNum']" -V "123" | xml sel -t -v "//*[name()='m:NumberToDollarsResult']"
}

interactive_mode_get_first_input() {
Expand Down Expand Up @@ -71,6 +71,10 @@ dry_run() {
"$DIR"/../src/soap "$TEST_ENDPOINT" "$DIR/request.xml" --dry
}

request_with_action() {
"$DIR"/../src/soap "$TEST_ENDPOINT" "$DIR/request_with_action.xml" --dry
}

d_run() {
"$DIR"/../src/soap "$TEST_ENDPOINT" "$DIR/request.xml" -d
}
Expand All @@ -85,7 +89,7 @@ pretty_print() {

@test "version check" {
run get_version
assert_output "soap-cli v1.2"
assert_output "soap-cli v1.3"
}

@test 'src/soap "https://www.dataaccess.com/webservicesserver/NumberConversion.wso" "test/request.xml"' {
Expand Down Expand Up @@ -122,7 +126,7 @@ pretty_print() {
assert_output "one hundred dollars"
}

@test 'src/soap "https://www.dataaccess.com/webservicesserver/NumberConversion.wso" "test/request.xml" -u "//*[name\(\)="dNum"]" -v "123"' {
@test 'src/soap "https://www.dataaccess.com/webservicesserver/NumberConversion.wso" "test/request.xml" -U "//*[name\(\)="dNum"]" -V "123"' {
run u_the_request_and_get_response_value
assert_output "one hundred and twenty three dollars"
}
Expand Down Expand Up @@ -160,3 +164,14 @@ pretty_print() {
run dp
assert_output --partial "syntax=xml"
}

@test 'SOAP headers: Content-Type' {
run dry_run
assert_output --partial "application/soap+xml;charset=UTF-8"
}


@test 'SOAP headers: wsa:Action -> Content-Type: action' {
run request_with_action
assert_output --partial 'action="https://github.com/pmamico/soap-cli/issues/13"'
}

0 comments on commit 5c441c1

Please sign in to comment.