Skip to content

Instantly share code, notes, and snippets.

@gunesmes
Last active March 13, 2022 16:30
Show Gist options
  • Save gunesmes/983b2e36e214e4c3dd3b to your computer and use it in GitHub Desktop.
Save gunesmes/983b2e36e214e4c3dd3b to your computer and use it in GitHub Desktop.
Testing SOAP services, I used Python requests library to test the SOAP services. What I did is just creating a form variable within a triple-double quotes for Python docstring, like """this is triple-double quotes with V1: %s and V2: %s""" %(variable-1, variable-2) . At the end I am just calling requests.post method with sending url, headers, da…
~/P/m/h/services python run.py
test_get_session (__main__.SoapServices) ... ok
test_get_token (__main__.SoapServices) ... ok
test_CompleteReturnRequest (__main__.SoapServices) ... ok
test_ReturnTransactionRequest (__main__.SoapServices) ... ok
test_UserInfoRequest (__main__.SoapServices) ... ok
test_RefundRequest (__main__.SoapServices) ... ok
test_StartTransactionRequest (__main__.SoapServices) ... ok
test_ReturnTransactionRequest (__main__.SoapServices) ... ok
----------------------------------------------------------------------
Ran 8 tests in 7.362s
OK
import unittest
import test_services
import xml.etree.ElementTree as ET
# I am using python unittest for asserting cases.
# In this module, there should be test cases.
# If you want to run it, you should type: python <module-name.py>
class SoapServices(unittest.TestCase):
def test_CompleteReturnRequest(self):
# service should return 2xx for the first request
result = test_services.CompleteReturnRequest(Username, Password, merchantCode, storeCode, returnId)
self.assertIn(result.status_code, [200, 201, 202])
# service should return 5xx for the second request
result = test_services.CompleteReturnRequest(Username, Password, merchantCode, storeCode, returnId)
self.assertIn(result.status_code, [500, 501, 502])
# or you can check inside of the response message
keys = ["provisionId", "otpNeeded"]
for key in keys: self.assertIn(key, unicode(result.text))
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(SoapServices)
unittest.TextTestRunner(verbosity=2).run(suite)
#!/usr/bin/env python
# encoding: utf-8  
import requests
def CompleteReturnRequest(Username, Password, merchantCode, storeCode, returnId):
form = """
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:pos="http://testthis.com/xmlschema/pos">
<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"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>%s</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">%s</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<pos:CompleteReturnRequest>"
<!--Optional:-->
<pos:merchantCode>%s</pos:merchantCode>
<pos:storeCode>%s</pos:storeCode>
<pos:returnId>%d</pos:returnTrxId>
</pos:CompleteReturnRequest>
</soapenv:Body>
</soapenv:Envelope>""" %(Username, Password, merchantCode, storeCode, returnId)
encoded_request = form.encode('utf-8')
headers = {"Content-Type": "text/xml; charset=UTF-8","Content-Length": len(encoded_request)}
response = requests.post(url="https://testthis.pos.com:443/pos/soap/pos",headers = headers,data = encoded_request,verify=False)
return response
@adityap31
Copy link

I modified the code as per my url and argumets. Not getting any response printed. Only a line ..\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)

@oimeqmo
Copy link

oimeqmo commented Feb 18, 2021

good example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment