def httpPost(url, params=None): try: request = HTTPRequest() if params is not None: return request.POST(url, params) else: return request.POST(url) except SSLProtocolException, e: log("SSL error for %s: %s" % (url, e)) return None
def __call__(self): if grinder.runNumber > 0 or grinder.threadNumber > 0: raise RuntimeError("Use limited to one thread, one run; " "see Amazon Web Services terms and conditions") request = HTTPRequest(url="http://xml.amazon.com/onca/xml") bookDetailsTest.record(request) parameters = ( NVPair("v", "1.0"), NVPair("f", "xml"), NVPair("t", "webservices-20"), NVPair("dev-t", "<insert license key here>"), NVPair("type", "heavy"), NVPair("AsinSearch", "1904284000"), ) bytes = request.POST(parameters).inputStream # Parse results document = parser.buildDocumentUrl(InputSource(bytes)) result = {} for details in document.getElementsByTagName("Details"): for detailName in ("ProductName", "SalesRank", "ListPrice"): result[detailName] = details.getElementsByTagName( detailName)[0].firstChild.nodeValue grinder.logger.info(str(result))
def send(self, data): request = HTTPRequest() result = request.POST(self._url, data) if not result.statusCode == 204: raise Exception("Unexpected HTTP response; " + result.getText()) return result
def __init__(self): # Login URL request = HTTPRequest(url="https://login.site.com") ##### reset to the all cookies ##### threadContext = HTTPPluginControl.getThreadHTTPClientContext() self.cookies = CookieModule.listAllCookies(threadContext) for c in self.cookies: CookieModule.removeCookie(c, threadContext) # do login request.POST("/login/do", ( NVPair("id", "my_id"),NVPair("pw", "my_passwd"))); ##### save to the login info in cookies ##### self.cookies = CookieModule.listAllCookies(threadContext)
class GrinderConnector(object): """A connector that makes HTTP requests via The Grinder's worker thread. This is used by the `user.User` class to authenticate. """ def __init__(self): self.request = HTTPRequest() def post(self, url, body, headers, *args, **kwargs): url = str(url) body = str(json.dumps(body)) resp = self.request.POST(url, body, headers) return ResponseWrapper(resp)
def maybeAuthenticate(lastResult): if lastResult.statusCode == 401 \ or lastResult.text.find("j_security_check") != -1: grinder.logger.info("Challenged, authenticating") authenticationFormData = ( NVPair("j_username", "weblogic"), NVPair("j_password", "weblogic"), ) request = HTTPRequest(url="%s/j_security_check" % lastResult.originalURI) authenticationTest.record(request) return request.POST(authenticationFormData)
def http_open(self, req): greq = HTTPRequest() url = req.get_full_url() payload = req.get_data() headers = [] for k, v in req.headers.items(): headers.append(NVPair(k, v)) if req.get_method() == 'GET': gresp = greq.GET(url, payload, headers) elif req.get_method() == 'POST': gresp = greq.POST(url, payload, headers) elif req.get_method() == 'PUT': gresp = greq.PUT(url, payload, headers) elif req.get_method() == 'DELETE': gresp = greq.DELETE(url, payload, headers) else: raise ValueError("HTTP method " + req.get_method() + " isn't supported") return self.GrinderHTTPResponse(gresp)
class G2HTTPTest: """Parses parameters for an individual test and records the test invocation using a G3 Test.""" def __init__(self, testNumber, properties): self.sleepTime = properties["sleepTime"] headers = [] seenContentType = 0 for e in properties.getPropertySubset("parameter.header.").entrySet(): headers.append(NVPair(e.key, e.value)) if not seenContentType and e.key.lower() == "content-type": seenContentType = 1 postDataFilename = properties["parameter.post"] if postDataFilename: file = open(postDataFilename) self.postData = file.read() file.close() if not seenContentType: headers.append( NVPair("Content-type", "application/x-www-form-urlencoded")) else: self.postData = None self.okString = properties["parameter.ok"] self.url = properties["parameter.url"] realm = properties["basicAuthenticationRealm"] user = properties["basicAuthenticationUser"] password = properties["basicAuthenticationPassword"] if realm and user and password: self.basicAuthentication = (realm, user, password) elif not realm and not user and not password: self.basicAuthentication = None else: raise "If you specify one of { basicAuthenticationUser, basicAuthenticationRealm, basicAuthenticationPassword } you must specify all three." self.request = HTTPRequest(headers=headers) self.test = Test(testNumber, properties["description"]) self.test.record(self.request) def doTest(self, iteration): if self.basicAuthentication: connection = HTTPPluginControl.getThreadConnection(self.url) connection.addBasicAuthorization(self.basicAuthentication[0], self.basicAuthentication[1], self.basicAuthentication[2]) grinder.statistics.delayReports = 1 if self.postData: page = self.request.POST(self.url, self.postData).text else: page = self.request.GET(self.url).text if not page: error = self.okString else: error = self.okString and page.find(self.okString) == -1 if error or logHTML: if self.test.description: description = "_%s" % self.test.description else: description = "" filename = grinder.filenameFactory.createFilename( "page", "_%d_%.3d%s" % (iteration, self.test.number, description)) file = open(filename, "w") print >> file, page file.close() if error: grinder.logger.error( "The 'ok' string ('%s') was not found in the page " "received. The output has been written to '%s'." % (self.okString, filename)) if error: grinder.statistics.forLastTest.success = 0 if self.sleepTime: grinder.sleep(long(self.sleepTime))
def solrCommit(self): request = HTTPRequest() url = urljoin(self.solrURL, 'update') request.POST(url, '<commit />')