Cross Column

Showing posts with label JMeter. Show all posts
Showing posts with label JMeter. Show all posts

Wednesday, October 17, 2018

JMetesr—How to Load Test CSRF-Protected Web Sites

As shown in below Figure (click to enlarge), an X-CSRF-Token header is used in an HTTP request.  In this article, we will discuss how to load test CSRF-Protected web sites using JMeter.  To begin with, what is CSRF?

Figure 1.  X-CSRF-Token header shown in View Results Tree

Cross-Site Request Forgery (CSRF)


Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user's browser. Cross-Site Request Forgery (CSRF) attacks occur through a malicious website that sends the requests to the targeted application website if the user is already authenticated through a different website.

These attacks happen if the user logs in to the actual website and leaves the session open, and accesses the malicious website links and forms that try to form the dynamic URLs to the targeted application where the user has already logged in.

The best way to prevent CSRF attacks is to attach CSRF tokens to each request from the application users and bind them to the user session. This way, the applications can restrict the access to the user's secure information by confirming the request is coming from known user sessions.

View Results Tree


After script recording, the best way to debug correlation issues is to use View Results Tree component (a Listener).

The View Results Tree shows a tree of all sample responses, allowing you to view the response for any sample. In addition to showing the response, you can see the time it took to get this response, and some response codes.

Note that View Results Tree MUST NOT BE USED during load test as it consumes a lot of resources (memory and CPU). Use it only for either functional testing or during Test Plan debugging and Validation.

From Figure 1, we have found an X-CSRF-Token header used in Step 32.  This means that we need to search for CSRF token backwards in the Response Data of earlier Steps.

Correlation


From the end-user’s point of view, CSRF protection is transparent. On the protocol level, CSRF protection is an additional mandatory dynamic parameter, such as the:[2]
  • Cookie
  • Header
  • Request Parameter
When a real-life user surfs a CSRF-protected website with a web browser, the browser’s CSRF security token can be set (for example: this can be set with a JavaScript function). Now, here’s where JMeter’s “not being a browser” issue really becomes a limitation. As it’s not a browser, it can’t execute a client-side JavaScript and therefore can’t generate and record a proper CSRF token.

To resolve the challenges raised by CSRF sites, you’ll need to use a JMeter Correlation.  In this article, we will demonstrate the use of a JMeter PostProcessor Regular Expression Extractor to extract the CSRF token.

Figure 2.  Specification of  a PostProcessor Regular Expression Extractor 

Regular Expression Extractor


At Step 15 (see Figure 2), you need to add a new PostProcessor Regular Expression Extractor to extract the CSRF token.

Sample Response Data:

[
  {
   <snipped>
    "bimodelerURL": "/bimodeler",
    "csrftoken": "h3CYF2EDNYlfBPKM01grVQQMfUKE0lAvhwfRzHtxU1Mdigx6",
    "vaAdminPermission": true,
   <snipped>
   }
]

Regular Expression:

"csrftoken":"([^\"]+)


Then, you can use a text editor to open the jmx file and do a global replacement of
h3CYF2EDNYlfBPKM01grVQQMfUKE0lAvhwfRzHtxU1Mdigx6
with
${csrftoken}
For example, at Step 32, you should see something updated like below:

Figure 3.  Variable Substitution in HTTP Header


References

  1. Oracle JET for Developers
  2. How to Load Test CSRF-Protected Web Sites


Monday, August 20, 2018

JMeter—Using "Save Responses to a file" for Error Logging

Using JMeter to do load test, you often see a small percentage of responses failed.  How do you investigate those errors?

In [1], it suggests two approaches:

In this article, we will cover using Save Responses to a file listener with the option of saving responses only in error.  Note that don't forget disabling it in your final run (i.e., after the debugging process).

Save Responses to a file


The primary use for this is in creating functional tests, but it can also be useful where the response is too large to be displayed in the View Results Tree Listener.  Listed below are parameters that you can configure for this test element.




Example


This test element can be placed anywhere in the test plan. For each sample in its scope, it will create a file of the response Data.  In this example, Save Responses to a file listener is placed in MyProfile test element (i.e., a Transaction Controller), there are two samples (i.e., 111 and 112) in its scope.



The file name is created from the specified prefix, plus a number (unless this is disabled). The file extension is created from the document type, if known. If not known, the file extension is set to 'unknown'. If numbering is disabled, and adding a suffix is disabled, then the file prefix is taken as the entire file name. This allows a fixed file name to be generated if required. The generated file name is stored in the sample response, and can be saved in the test log output file if required.

The current sample is saved first, followed by any sub-samples (child samples). If a variable name is provided, then the names of the files are saved in the order that the sub-samples appear. 

In this example, we have specified "MyServices_1832_MyProfile" as the Filename prefix.  By default, it's placed in the JMeter's bin directory.  For example,

-rw-r--r-- 1 root root        0 Aug 19 16:46 MyServices_1832_myProfile25.unknown
-rw-r--r-- 1 root root     2769 Aug 19 16:46 MyServices_1832_myProfile26.unknown

The contents of MyServices_1832_myProfile26.unknown show the following exception:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:158)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:82)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:271)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
<snipped>
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.createTunnelToTarget(DefaultRequestDirector.java:876)
at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:794)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:614)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:654)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:413)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1189)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1178)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:491)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:425)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:254)
at java.lang.Thread.run(Thread.java:745)

Saturday, January 6, 2018

JMeter―Select on Multiple Criteria with JSONPath

If your web services are RESTful, you will get JSON responses.  In Apache JMeter, there is a JSON Extractor (i.e., a Post Processor) available for parsing JSON responses.

In this article, we will introduce JSONPath, which is used in JSON Extractor, and present an example of selecting an object/element based on multiple criteria with it.[1]

JSON Extractor / JSONPath


One of the advantages of XML is the availability of numerous tools to analyse, transform and selectively extract data out of XML documents. XPath is one of these powerful tools.  For JSON, we have a similar tool called JSONPath.

JSONPath is the XPath for JSON.  Since a JSON structure is normally anonymous, JSONPath assigns symbol $ as the root object.

Below is a side-by-side comparison of the JSONPath syntax elements with its XPath counterparts.[3]


XPath
JSONPath
Description
/
$
the root object/element
.
@
the current object/element
/
. or []
child operator
..
n/a
parent operator
//
..
recursive descent. JSONPath borrows this syntax from E4X.
*
*
wildcard. All objects/elements regardless their names.
@
n/a
attribute access. JSON structures don't have attributes.
[]
[]
subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
|
[,]
Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
n/a
[start:end:step]
array slice operator borrowed from ES4.
[]
?()
applies a filter (script) expression.
n/a
()
script expression, using the underlying script engine.
()
n/a
grouping in Xpath

JSONPath expressions can use the dot–notation
$.ccapiInfo.canonicalLink

or the bracket–notation
$['ccapiInfo']['canonicalLink']

for input paths. For the internal or output paths, they will always be converted to the more general bracket–notation.  

Selecting on Multiple Criteria with JSONPath 


Given an array of books, the following JSONPath applies two filters (e.g., "price" and "category") on the array to retrieve the name of author matching the criteria:


JSON Input

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

JSONPath Syntax


$..book[?(@.price==8.99 && @.category=='fiction')] .author


Below shows the evaluation result using JSONPath Online Evaluator with the above input and JSONPath Expression:


Evaluation Results

[
  "Herman Melville"
]


More Examples


Given the same input, you can also retrieve different objects (or elements) using the following JSONPath expressions:[4]

JSONPath expressions
Result
$.store.book[*].author
the authors of all books in the store
$..author
all authors
$.store.*
all things in store (four books, a blue ball)
$.store..price
the price of everything in the store
$..book[-1:]
the last book in order
$..book[0,1]
the first two books
$..*
all members of the JSON structure

References

  1. JsonPath AND Operator on Array
  2. JSONPath Online Evaluator - jsonpath.com
  3. JSONPath - XPath for JSON - stefan.goessner
  4. JSONPath reference
  5. JMeter: How to Verify JSON Response? (Xml and More)

Friday, January 5, 2018

JMeter―How to Use Firefox to Record Test Scenarios

Video 1.  JMeter Beginner Tutorial 23 - How to use Test Script Recorder (YouTube link)


This article will cover the details of recording HTTP/HTTPS traffic with Firefox (FF) in Apache JMeter. To achieve that, you can use a workbench with HTTP(S) Test Script Recorder.  If you are new to JMeter, watch the above video first.




"Recording" Template


The easiest way to create a complete Test Plan with the HTTP(S) Test Script Recorder is using Recording Template:[1]
  1. Go to JMETER_HOME/bin and start JMeter
  2. Select Templates... on the menu bar
  3. Select Recording template on the list
  4. A complete Test Plan is generated (see the diagram below)
Using "Recording" template, save you a lot of efforts.  For comparison, you can read [2] for manual steps.

In the HTTP Request Defaults element, you can optionally set "Server name":
Server name
enter example.com
Path
leave blank



JMeter Temporary Root CA


Return to HTTP(S) Test Script Recorder, and click the Start button at the top.
Note that you need to "Configure your browser to use the JMeter Proxy" (see the section below) first.  For the discussion only, we will show you the "JMeter Temporary Root CA" first.

This will start the JMeter proxy server which is used to intercept the browser requests. A file called ApacheJMeterTemporaryRootCA.crt will be generated in jmeter/bin folder and you will find a prompt showing below messages:



Installing the certificate in Firefox for HTTPS Recording


 If you're recording SSL/TLS traffic (i.e., your site URL has https prefix), you need to install the certificate (i.e., ApacheJMeterTemporaryRootCA.crt) in your browser. So, JMeter could record encrypted traffic.  If you don't know how to do it, read Installing the JMeter CA certificate for HTTPS recording first.

For the Firefox (or FF) browser, you choose the following options (note that the configuration looks different in different FF versions):
  • Tools / Options
  • Advanced / Certificates
  • View Certificates
  • Authorities
  • Import …
  • Browse to the JMeter launch directory, and click on the file ApacheJMeterTemporaryRootCA.crt, pressOpen
  • Click View<
    and check that the certificate details agree with the ones displayed by the JMeter Test Script Recorder
  • If OK, select "Trust this CA to identify web sites", and press OK
  • Close dialog by pressing OK as necessary 
After import, you can find a new entry on the Authorities list as shown below:



Configure Your Browser to Use the JMeter Proxy


Like many other web testing tools, JMeter records tests by inserting itself as an HTTP proxy between the browser and the web site.  We use FF as our browser when using the JMeter HTTP(S) Test Script Recorder because, unlike Chrome and some other browsers, it does allows you to override system-wide configuration for its proxy settings (see also [8]).

Configure Firefox to use localhost (127.0.0.1) on port 8080 (Note that you also need to set the Port to  be 8080 in HTTP(S) Test Script Recorder) as its proxy for all traffic by following these steps:[7]
  1. Open Firefox
  2. Go to the Preferences menu
    • From the tool bar, click Edit → Preferences (or Tools → Preferences or type about:preferences#advanced as URL). This should bring up the options.
  3. Click on the Advanced tab
  4. Then Network tab
  5. In the "Connection" section, click on "Settings..."
  6. Select the "Manual proxy configuration" radio button
  7. Set HTTP Proxy to "localhost" and Port to "8080"
  8. Check "Use this proxy server for all protocols"
  9. Click OK and exit the Preferences menu

Note: When Firefox is configured to use JMeter's Script Recorder as a proxy, it will only work properly if the Script Recorder is running.


Recording Tips


When you record, you may want to consider the following extra configurations:
  • Follow Redirects vs Redirect Automatically[3,4]
  • Retrieve All Embedded Resources from HTML Files
    • Set in HTTP Request sampler
    • Tell JMeter to parse the HTML file and send HTTP/HTTPS requests for all images, Java applets, JavaScript files, CSSs, etc. referenced in the file.
  • Firefox Data Collection and Use
  • Use the Transaction Controller[5]
    • To generate a “virtual” sample representing a real page to measure aggregate times of all nested samples (i.e., embedded resources―images, styles, scripts, recordings or anything that seems like a separate request).
    • Transaction controllers also provide scopes for JMeter test elements[10,11]
  • URL Patterns to Exclude
    • Set in HTTP(S) Test Script Recorder→ Requests Filtering
    • If the following patterns were not excluded yet, set them manually:
      • (?i).*\.(bmp|css|js|gif|ico|jpe?g|png|swf|woff|woff2)
      • (?i).*\.(bmp|css|js|gif|ico|jpe?g|png|swf|woff|woff2)[\?;].*
  • Post-recording updates of Test Scenarios
  • Prepare your click stream before recording
  • Use timers to simulate a virtual user’s “think time”

References

  1. Apache JMeter HTTP(S) Test Script Recorder (Apache Software Foundation)
  2. Apache JMeter HTTP(S) Test Script Recorder (pdf)
  3. When to use “Follow Redirects” & “Redirect Automatically” while recording with jmeter
  4. Difference Between Redirect Automatically and Follow Redirects in JMeter
  5. JMeter―Using the Transaction Controller (Xml and More)
  6. JMeter Proxy exclusion patterns still being recorded
  7. How To Use JMeter To Record Test Scenarios
  8. Use the Profile Manager to create and remove Firefox profiles (Mozilla)
  9. A Comprehensive Guide to Using JMeter Timers
  10. JMeter Scoping Rules - The Ultimate Guide
  11. Scope and Execution Order of Elements in Jmeter
  12. JMeter Beginner Tutorial 21 - How to use Test Script Recorder (video)

Thursday, December 21, 2017

JMeter—Use Extractors (Post-Processor Elements) for Correlation



What Is Correlation


Correlation is the process of capturing and storing the dynamic response (e.g., "Session ID" in the above diagram) from the server and passing it on to subsequent requests. A response is considered dynamic when it can return different data for each iterating request, occasionally affecting successive requests. Correlation is a critical process during performance load test scripting, because if it isn’t handled correctly, your script will become useless.

Correlation is 2-step process:
  1. Parse and extract the dynamic value from the response of a step using a Post Processor element such as:
  2. Refer the extracted value in the request of a subsequent step
    • http://.../.../...?sessionID=${sessionId}#....


How to Use Regular Expression Extractor


Watching above video, you can learn how to create Regular Expression Extractors in JMeter in the following steps:[2]
  1. Create a Test Plan where you want to do dynamic referencing in JMeter
  2. Add Regular Expression Extractor in the Step from where response value(s) needs to be extracted
    • You can use RegExr—an online tool—to learn, build, and test Regular Expressions
  3. Refer the extracted value (referred by Reference Name) in subsequent step(s)
  4. Run and validate it


How to Use CSS/JQuery Extractor





Watching above video, you can learn how to create CSS/JQuery Extractors in JMeter in similar steps:[8]
  1. Create a Test Plan where you want to do dynamic referencing in JMeter
  2. Add CSS/JQuery Extractor[9]in the Step from where response value(s) needs to be extracted
    • You can find a detailed explanation of CSS syntax here. jQuery's selector engine uses most of the same syntax as CSS with some exceptions. For selecting an arbitrary locator, you can use field Match No. with the ‘0’ value, which returns a random value from all found results.
    • It is also worth mentioning there is a list of very convenient browser plugins to test CSS locators right into your browser. For Firefox, you can use the ‘Firebug’ plugin, while for Chrome ‘XPath Helper’ is the most convenient tool.
  3. Refer the extracted value (referred by Reference Name) in subsequent step(s)
  4. Run and validate it


How to Use JSON Extractor


Read the companion articles on this subject:

References

  1. Advanced Load Testing Scenarios with JMeter: Part 1 - Correlations
  2. JMeter Beginner Tutorial 19 - Correlation (with Regular Expression Extractor)
  3. Using RegEx (Regular Expression Extractor) with JMeter
  4. RegExr—an online tool (good)
  5. JMeter Listeners - Part 1: Listeners with Basic Displays
  6. Understand and Analyze Summary Report in Jmeter
  7. How to Automate Auth Token using JMETER
  8. How to Use the CSS/JQuery Extractor in JMeter (BlazeMeter)
  9. How to Use the CSS/jQuery Extractor in JMeter  (DZone)
  10. JMeter: How to Turn Off Captive Portal from the Recording Using Firefox (Xml and More)
  11. JMeter―Select on Multiple Criteria with JSONPath  (Xml and More)
  12. JMeter: How to Verify JSON Response?  (Xml and More)

Sunday, September 17, 2017

JMeter: How to Verify JSON Response?


JSON (Javascript object notation) is a serialization format (in key-value pairs) of data structures .  For REST API, it is widely used for data transfer from server to client.  For example, a client sends an HTTP request with below header:

  • Accept: application/json

The server can respond with below sample JSON data:

  {
    "result": [],
    "ccapiInfo": {
      "createdOn": "2017-09-07T15:25:29.000Z",
      "cachedOn": "2017-09-07T15:21:49.513Z",
      "origin": "cache",
      "canonicalLink": "http://www.myServer.com:9885/computeConsoleApi/infra1626compute1/api/v1/instance/Compute-infra1626compute1/"
    }
  }

with a response header of:
Content-Type: application/json

In this article, we will discuss how to achieve two tasks in Apache JMeter:


JSON Extractor / JSONPath


One of the advantages of XML is the availability of numerous tools to analyse, transform and selectively extract data out of XML documents. XPath is one of these powerful tools.  For JSON, we have a similar tool called JSONPath.

JSONPath is the XPath for JSON.  Since a JSON structure is normally anonymous, JSONPath assigns symbol $ as the root object.

Below is a side-by-side comparison of the JSONPath syntax elements with its XPath counterparts.[9]

XPathJSONPathDescription
/$the root object/element
.@the current object/element
/. or []child operator
..n/aparent operator
//..recursive descent. JSONPath borrows this syntax from E4X.
**wildcard. All objects/elements regardless their names.
@n/aattribute access. JSON structures don't have attributes.
[][]subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
|[,]Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
n/a[start:end:step]array slice operator borrowed from ES4.
[]?()applies a filter (script) expression.
n/a()script expression, using the underlying script engine.
()n/agrouping in Xpath

JSONPath expressions can use the dot–notation

$.ccapiInfo.canonicalLink

or the bracket–notation

$['ccapiInfo']['canonicalLink']

for input paths. For the internal or output paths, they will always be converted to the more general bracket–notation.  Below diagram shows the evaluation result using a JSONPath Online Evaluator with the input and JSONPath Expression as given in this article.


JSR223 Assertion


Assertion in JMeter help verify that your server under test returns the expected results. JMeter includes quite a few assertion elements for validating the sampler’s response, yet sometimes your validation decision might follow complex logic, and can’t be configured using the out-of-the-box JMeter assertions - scripting is then required.

If you need to write scripting assertion code to extend baseline JMeter functionality, JSR223, in combination with Groovy language is a good choice performance-wise—especially when its compilation caching is enabled.



Groovy Script

String jsonString =  vars.get("myCanonicalLink");
String userNameString = vars.get("user_name");

log.info ("The canonicalLink is " + jsonString);

if ( jsonString != "http://myserver.com:9885/computeConsoleApi/" + 
      userNameString + "/api/v1/instance/Compute-" + userNameString + "/") 
{
AssertionResult.setFailureMessage("The canonicalLink is wrong");
    AssertionResult.setFailure(true); 
}


However, every test element including assertion added to the test plan will increase the total CPU and memory requirements.  So, plan your use of assertions sparingly.

© Travel for Life Guide. All Rights Reserved.

Analytical Insights on Health, Culture, and Security.