Grinder, a Java Load Testing Framework. Using custom headers, cookies and payload in GET, PUT, POST, DELETE, OPTIONS HTTP methods

Adding headers, cookies, and payload for various HTTP methods in Grinder, a Java Load Testing Framework, is unfortunately, is not intuitively clear. So the following sample should make life much easier: we specify custom header, cookies, and payload for the mentioned HTTP methods.

Link to the Grinder: http://grinder.sourceforge.net/

# Sample of using custom headers and cookies for GET, PUT, POST, DELETE, OPTIONS HTTP
# methods for Grinder Load Framework

from net.grinder.script.Grinder import grinder
from net.grinder.script import Test, Statistics
from net.grinder.plugin.http import HTTPRequest, HTTPPluginControl
from HTTPClient import NVPair, Cookie, CookieModule, CookiePolicyHandler, HTTPResponse

from java.util import Date

log = grinder.logger.output

globalDebugOutput = True
filename = "/tmp/cookies.log"

# We declare a default URL for the HTTPRequest.
getRequest = HTTPRequest(url = "http://localhost:8080")
putRequest = HTTPRequest(url = "http://localhost:8080")
postRequest = HTTPRequest(url = "http://localhost:8080")
deleteRequest = HTTPRequest(url = "http://localhost:8080")
optionsRequest = HTTPRequest(url = "http://localhost:8080")

payload = [NVPair('param', 'that is a custom payload')]

## We declare various cookies sets for each type of the request
expiryDate = Date()
expiryDate.year += 10

cookieGetOne = Cookie("keyGetOne", "12345","localhost", "/", expiryDate, 0)
cookieGetTwo = Cookie("keyGetTwo", "10000","localhost", "/", expiryDate, 0)
cookiePutOne = Cookie("keyPutOne", "12345","localhost", "/", expiryDate, 0)
cookiePutTwo = Cookie("keyPutTwo", "10000","localhost", "/", expiryDate, 0)
cookiePostOne = Cookie("keyPostOne", "12345","localhost", "/", expiryDate, 0)
cookiePostTwo = Cookie("keyPostTwo", "10000","localhost", "/", expiryDate, 0)
cookieDeleteOne = Cookie("keyDeleteOne", "12345","localhost", "/", expiryDate, 0)
cookieDeleteTwo = Cookie("keyDeleteTwo", "10000","localhost", "/", expiryDate, 0)
cookieOptionsOne = Cookie("keyOptionsOne", "12345","localhost", "/", expiryDate, 0)
cookieOptionsTwo = Cookie("keyOptionsTwo", "10000","localhost", "/", expiryDate, 0)

# Set up a cookie handler to log all cookies that are sent and received.
class MyCookiePolicyHandler(CookiePolicyHandler):
def acceptCookie(self, cookie, request, response):
log("accept cookie: %s" % cookie)
return 1

def sendCookie(self, cookie, request):
log("send cookie: %s" % cookie)
return 1

CookieModule.setCookiePolicyHandler(MyCookiePolicyHandler())

def printCookiesIntoFile(cookies, logFileName):
if (globalDebugOutput):
FILE = open(logFileName,"a+")
for c in cookies: FILE.write("retrieved cookie: %s\n" % c)
FILE.close()

# GET
def page1Get():
# Now let's add a new cookie.
threadContextGet = HTTPPluginControl.getThreadHTTPClientContext()

CookieModule.addCookie(cookieGetOne, threadContextGet)
CookieModule.addCookie(cookieGetTwo, threadContextGet)

headersGet = \
( NVPair('Custom_Header_One', 'Any tests GET'),
NVPair('Accept-Language', 'en-us,en;q=0.5'),
NVPair('User-Agent', 'Grinder HTTP-GET Mozilla/5.0'), )
getRequest.setHeaders(headersGet)

httpResp = getRequest.GET('/get?param=22¶m2=papa')
if ( 500 == httpResp.getStatusCode() ):
statistics = grinder.statistics.getForCurrentTest()
statistics.success = 0 # Mark test as bad.

# Get all cookies for the current thread and write them to the log
cookies = CookieModule.listAllCookies(threadContextGet)
printCookiesIntoFile(cookies, filename)

# PUT
def page2Put():
threadContextPut = HTTPPluginControl.getThreadHTTPClientContext()

CookieModule.addCookie(cookiePutOne, threadContextPut)
CookieModule.addCookie(cookiePutTwo, threadContextPut)

headersPut = \
( NVPair('Custom_Header_One', 'Any tests PUT'),
NVPair('Accept-Language', 'en-us,en;q=0.5'),
NVPair('User-Agent', 'Grinder HTTP-PUT Mozilla/5.0'), )

putRequest.setHeaders(headersPut)
putRequest.setFormData(payload)
httpResp = putRequest.PUT('/put')
if ( 500 == httpResp.getStatusCode() ):
statistics = grinder.statistics.getForCurrentTest()
statistics.success = 0 # Mark test as bad.

cookies = CookieModule.listAllCookies(threadContextPut)
printCookiesIntoFile(cookies, filename)

# POST
def page3Post():
threadContextPost = HTTPPluginControl.getThreadHTTPClientContext()

CookieModule.addCookie(cookiePostOne, threadContextPost)
CookieModule.addCookie(cookiePostTwo, threadContextPost)

headersPost = \
( NVPair('Custom_Header_One', 'Any tests POST'),
NVPair('Accept-Language', 'en-us,en;q=0.5'),
NVPair('User-Agent', 'Grinder HTTP-POST Mozilla/5.0'), )
postRequest.setHeaders(headersPost)
postRequest.setFormData(payload);
httpResp = postRequest.POST('/post')
if ( 500 == httpResp.getStatusCode() ):
statistics = grinder.statistics.getForCurrentTest()
statistics.success = 0 # Mark test as bad.

cookies = CookieModule.listAllCookies(threadContextPost)
printCookiesIntoFile(cookies, filename)

# DELETE
def page4Delete():
threadContextDelete = HTTPPluginControl.getThreadHTTPClientContext()

CookieModule.addCookie(cookieDeleteOne, threadContextDelete)
CookieModule.addCookie(cookieDeleteTwo, threadContextDelete)

headersDelete = \
( NVPair('Custom_Header_One', 'Any tests DELETE'),
NVPair('Accept-Language', 'en-us,en;q=0.5'),
NVPair('User-Agent', 'Grinder HTTP-DELETE Mozilla/5.0'), )

deleteRequest.setHeaders(headersDelete)
deleteRequest.setFormData(payload);
httpResp = deleteRequest.DELETE('/delete')
if ( 500 == httpResp.getStatusCode() ):
statistics = grinder.statistics.getForCurrentTest()
statistics.success = 0 # Mark test as bad.

cookies = CookieModule.listAllCookies(threadContextDelete)
printCookiesIntoFile(cookies, filename)

# OPTIONS
def page5Options():
threadContextOptions = HTTPPluginControl.getThreadHTTPClientContext()

CookieModule.addCookie(cookieOptionsOne, threadContextOptions)
CookieModule.addCookie(cookieOptionsTwo, threadContextOptions)

headersOptions = \
( NVPair('Custom_Header_One', 'Any tests OPTIONS'),
NVPair('Accept-Language', 'en-us,en;q=0.5'),
NVPair('User-Agent', 'Grinder HTTP-OPTIONS Mozilla/5.0'), )
optionsRequest.setHeaders(headersOptions)
optionsRequest.setFormData(payload);
httpResp = optionsRequest.OPTIONS('/options')
if ( 500 == httpResp.getStatusCode() ):
statistics = grinder.statistics.getForCurrentTest()
statistics.success = 0 # Mark test as bad.

cookies = CookieModule.listAllCookies(threadContextOptions)
printCookiesIntoFile(cookies, filename)

page1GetTest = Test(1, "HTTP-GET").wrap(page1Get)
page2PutTest = Test(2, "HTTP-PUT").wrap(page2Put)
page3PostTest = Test(3, "HTTP-POST").wrap(page3Post)
page4DeleteTest = Test(4, "HTTP-DELETE").wrap(page4Delete)
page5OptionsTest = Test(5, "HTTP-OPTIONS").wrap(page5Options)

class TestRunner:
def __call__(self):
page1GetTest()
page2PutTest()
page3PostTest()
page4DeleteTest()
page5OptionsTest()

7 Comments

Filed under technology

7 Responses to Grinder, a Java Load Testing Framework. Using custom headers, cookies and payload in GET, PUT, POST, DELETE, OPTIONS HTTP methods

  1. sqwrodriguez

    I love the valuable info you supply in your posts. I like your writing style.

  2. I just signed up to your blogs rss feed. Will you post more on this subject?

  3. linikzh@gmail.com

    I think other web site proprietors should take this site as an model, very clean and great user friendly style and design, as well as the content. You’re an expert at this stuff!

  4. John Silver

    I’ll discover why the correspondence course on “Mail Fraud” that I bought never showed up.

  5. r.yasmam

    I think I’ve read about this somewhere . Did you source this from somewhere else?

  6. alexzar

    Not really, that is mostly the collection of the results of my own experiments.

  7. Hurrah, that’s what I was searching for, what a material!
    existing here at this weblog, thanks admin of this site.

Leave a Reply

Your email address will not be published. Required fields are marked *