Skip to content

Instantly share code, notes, and snippets.

@pcolazurdo
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcolazurdo/3e7c08816dc962cf1283 to your computer and use it in GitHub Desktop.
Save pcolazurdo/3e7c08816dc962cf1283 to your computer and use it in GitHub Desktop.
Ruby Client for automating SOAP testing
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sup="http://supplier.v10_0_2_2.service.ssm.emptoris.ibm.com">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-68" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>FILL-THIS-UP!!!</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">FILL-THIS-UP!!!</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"> <%= nonce %> </wsse:Nonce>
<wsu:Created><%= wsucreated %></wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<sup:getSuppliers>
<criteria>
<returnBlockSize>50</returnBlockSize>
<startingRow>0</startingRow>
</criteria>
</sup:getSuppliers>
</soapenv:Body>
</soapenv:Envelope>
# WARNING: This tool hasn't been tested with large XML files.
# It could fail due to the lack of management of multipart calls.
# TODO: Add Error Handling
require 'erb'
require 'httpclient'
require 'base64'
require 'getoptlong'
# This function uses ERB to apply the variables using the template file
def update_tokens(template_file)
template = ""
open(template_file) {|f|
template = f.to_a.join
}
updated = ERB.new(template, 0, "%<>").result
return updated
end
# Manages the Command Line Options
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ]
)
uri = nil
debug = false
opts.each do |opt, arg|
case opt
when '--help'
puts <<-EOF
hello [OPTION] ... URL
-h, --help:
show help
-v, --verbose:
Debug to STDOUT
URL: The URL for the SOAP Service Endpoint
EOF
when '--verbose'
debug = true
end
end
if ARGV.length != 1
puts "Missing URL argument (try --help)"
exit 0
end
#If you have a proxy in your network you can use the HTTP_PROXY environment variable to set it up
if ENV['HTTP_PROXY']
h = HTTPClient.new(ENV['HTTP_PROXY'])
else
h = HTTPClient.new()
end
h.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE #Disable the SSL Checks for using in development mode. Unrecommended in Production
if debug
h.debug_dev = STDOUT #Enable Debug of httpclient calls
end
uri = ARGV.shift #Get the URI from the command line paramenters
contents = ""
wsucreated = Time.now.utc.iso8601(3)
nonce = Base64.strict_encode64(wsucreated)
# p nonce
# p wsucreated
contents=update_tokens("sample.xml.erb") #Read sample.xml.erb file and call to process using ERB
# p contents
ext = {'Content-Type' => 'text/soap+xml; charset=utf-8'} #Set headers
body = contents
res = h.post(uri, body, ext) # Make the POST Call
p "HEADER"
p "------"
print res.header.dump
p "BODY"
p "----"
p res.body
#Debug Output
if debug
p res.status
p res.header
p res.contenttype
end
Usage:
To install:
gem install httpclient
To run:
create a sample.xml.erb file with the required SOAP body
if you need to fill some variables do so in the code. In this sample we're only creating two variables:
nonce (base64 of timestamp)
wsucreated (now at UTC)
ruby soaptest.rb <URL>
Enjoy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment