def doService(httpMethod, url, credential, requestBody=None): Security.addProvider(MySSLProvider()) Security.setProperty("ssl.TrustManagerFactory.algorithm", "TrustAllCertificates") HttpsURLConnection.setDefaultHostnameVerifier(MyHostnameVerifier()) urlObj = URL(url) con = urlObj.openConnection() con.setRequestProperty("Accept", "application/xml") con.setRequestProperty("Content-Type", "application/xml") con.setRequestProperty("Authorization", credential) con.setDoInput(True); if httpMethod == 'POST': con.setDoOutput(True) con.setRequestMethod(httpMethod) output = DataOutputStream(con.getOutputStream()); if requestBody: output.writeBytes(requestBody); output.close(); responseCode = con.getResponseCode() logger.info('response code: ' + str(responseCode)) responseMessage = con.getResponseMessage() logger.info('response message: ' + str(responseMessage)) contentLength = con.getHeaderField('Content-Length') logger.info('content length: ' + str(contentLength)) stream = None if responseCode == 200 or responseCode == 201 or responseCode == 202: stream = con.getInputStream() elif contentLength: stream = con.getErrorStream() if stream: dataString = getStreamData(stream) logger.info(httpMethod + ' url: ' + url) if not url.endswith('.xsd') and len(dataString) < 4096: xmlStr = Util.prettfyXmlByString(dataString) logger.info(httpMethod + ' result: \n\n' + xmlStr) else: logger.info('response body too big, no print out') if responseCode == 200 or responseCode == 201 or responseCode == 202: return dataString else: ''' to mark the case failed if response code is not 200-202 ''' return None else: logger.error('') logger.error('---------------------------------------------------------------------------------------------------') logger.error('-------->>> Input or Error stream is None, it may be a defect if it is positive test case') logger.error('---------------------------------------------------------------------------------------------------') logger.error('') return None
def verifyCreate(self): if self.mySubClassName != 'Pod' and self.isOffLine and self.createTaskXml: self.logger.info(Util.prettfyXmlByString(self.createTaskXml)) NsmUtil.printHeadLine2('START: create response verification: parameters from request body') status = True for key, value in self.requestParams.items(): keyItems = key.split('.') pattern = None if key.endswith('.ipv4.start'): pattern = '<ipv4Start>' + value + '</ipv4Start>' elif key.endswith('.ipv4.end'): pattern = '<ipv4End>' + value + '</ipv4End>' elif key.endswith('.ipv4') or key.endswith('.mask') or key.endswith('.protocol') or key.endswith('.start') or key.endswith('.end') or key.endswith('.ASNumber') or key.endswith('.uid') or key == 'name' or key == 'description': pattern = '<' + keyItems[-1] + '>' + value + '</' + keyItems[-1] + '>' elif key.endswith('.netmask'): pattern = '<integer>' + value + '</integer>' elif key.startswith('serverfarm.probe.') or key == 'lb.algorithm': pattern = '<string>' + value + '</string>' elif key.endswith('redirect.port'): pattern = '<redirectPort>' + value + '</redirectPort>' if pattern: if self.createTaskXml.find(pattern) < 0: status = False NsmUtil.printStatusHeadLine2('Failed: pattern not found: ' + pattern) else: NsmUtil.printStatusHeadLine2('Passed: pattern found: ' + pattern) NsmUtil.printHeadLine2('END: create response verification: parameters from request body') return status
def verifyUpdate(self, updateVerificationPatternList, storage): if self.mySubClassName != 'Pod' and self.isOffLine and self.updateTaskXml: self.logger.info(Util.prettfyXmlByString(self.updateTaskXml)) NsmUtil.printHeadLine2('START: update response verification: parameters from request body') status = True for pattern in updateVerificationPatternList: if self.updateTaskXml.find(pattern) < 0: status = False NsmUtil.printStatusHeadLine2('Failed: pattern not found: ' + pattern) else: NsmUtil.printStatusHeadLine2('Passed: pattern found: ' + pattern) NsmUtil.printHeadLine2('END: update response verification: parameters from request body') return status