-
-
Save bingoabs/302011613096b3307d6f8e0f62f8f895 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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import requests | |
from pyquery import PyQuery | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment