Functional Testing 07: Using Response Data – Part 1

Seventh in a series focusing on using JMeter for functional testing of an API.

Quite often you will need to use data from one API response to make another subsequent API call. In this tutorial we we will see how to use the token returned by signing in to make an authorized API call to another API end point which requires you to be signed in (we will get a list of folders).

We are really going to start testing now!

Before we begin make sure you have the test plan from the previous tutorial or download this one to use.

Functional Testing 07: Using Response Data – Part 1

Every call to the test API, except for info and signin, require you send along the authorization token you were given when you logged in pre-pended by “Bearer “. We are faking a simple Bearer token authorization in this case, which passes the token in the “Authorization” header of all API calls.

In this tutorial we will enhance our Signin library to extract the token data from signin into a variable we can use in other calls. We will then create two new library modules which will use the authorization token, Folders and Files.

Part 1: Enhancing our Signin Library Module
  1. Open the JMeter test plan and expand Sampler LIbrary > Signin > If Success.
  2. Right-click on POST Signin and selct Add > Post Processors > JSON Extractor.

    A Post Processor are things which you want to run after a Sampler which use the response from the Sampler.
    A JSON Extractor Post Processor will extract the value at the end of a given JSON path  from the response into a JMeter variable. So, much like we used a JSON Assertion to validate the token path existed, we will now use a JSON Extractor to save its value.
  3. In the JSON Extractor
    Names of created variables to “token
    JSON path expression to “$.token

    This will put the token in the variable named token.
  4. Let’s make sure our changes are working. In the Signin tests, right-click the Debug Sampler we added last tutorial and select Enable.
  5. Run your tests. Examining the Response data in the Debug Sampler you should see the token variable updating after siginin.
Part 2: Using the Token to Make Another Call

Next we will add a new library module to get our user’s folders. This new module will use the token in the sampler header to send an authorized request.

  1. Click on the API Resources User Defined Variables in the test plan. Here we will enter the new URL info for the Folders API endpoint.
  2. Add a new row and
    Set Name to “URL_Folders
    Set Value to “${API_ROOT}folders
  3. Right-click Sampler Library and select Add > Logic Controller > Simple Controller.
  4. Rename the Simple Controller to ” GET Folders”.
  5. Right click Folders and select Add > Samplers > HTTP Request.
    Seem familiar? These are the same steps we followed to create our Signin Library module.
  6. Rename the HTTP Sampler to “GET Folders“.
  7. In the HTTP Sampler
    Set Protocol to “${PROTO}
    Set Server Name or IP to “${SERVER}
    Set Port Number to “${PORT}
    Set Method to “GET
    Set Path to “${URL_Folders}
  8. Now right click on the Folders Simple Controller and select Add > Config Element > HTTP Header Manager. Drag the Header Manager above the GET Folders Sampler.
  9. Add a new row to the HTTP Header Manager.
    Set Name to “Content-Type
    Set Value to “application/json
  10. Add a new row to the HTTP Header Manager.
    Set Name to “authorization
    Set Value to “Bearer ${token}

    This causes the GET Folders Sampler to send an authorization header which will contain our token as part of our GET Folders API call.
  11. Next right-click on the Test Plan and select Add > Threads (Users) > Thread Group. Rename the Thread Group to Folder Tests.
  12. Select My First Test and Signin Tests, right-click, and select Disable. We’ll get these tests out of the way for now while we work on our Folder tests.
  13. Right-click Folder tests and select Add > Logic Controller > Transaction Controller. Click the Generate Parent Sample checkbox in the Transaction Controller. Your test plan should look like this:

    In order to get the folders for our user, that user first needs to be logged in. So the test which we will create will first use our Signin Library module to sign our user in (and grab the token) and then use our new Get Folders module to get the user’s folders.
  14. Right click the Get Folders Transaction Controller and select Add > Logic Controller > jp@gc – Parameterized Controller (remember this from Functional Testing 03?). Rename it to Signin.
  15. Our Signin module needs three variables set to run. Add three rows.
    Row 1 set to Name: “username” Value: “PeterParker
    Row 2 set to Name: “password” Value: “Spider-man
    Row 3 set Name: to “outcome” Value: “Success

    If we forgot these variables we could always look at the variables reference we added to the Signin Library module.
  16. Right-click on Signin and select Add > Logic Controller > Module Controller.
  17. In the Module Controller, expand Sampler Library and select SignIn.
  18. I like to stop and make sure that everything works when it makes sense to, so let’s take a chance here to run the Folder Tests and make sure everything is working as we expect. If all is well you should see a passing with only a POST Signin so far.
  19. Great! Now right-click on the Get Folders Transaction Controller and select Add > Logic Controller > Module Controller.
  20. In the Module Controller, expand Sampler Library and select Get Folders.
  21. Now let’s go run the test again.
    You should now see the GET Folders call. If you click on the Request tab you’ll see that the request included our authorization header with our Bearer token in it.

    Clicking on the Response data tab you can see we get back a list of folders and their count.

    Hooray! We just used the response from one API call to make another API call!
  22. To better visualize the Get Folders repsonce you could copy the JSON response and paste it into a site like Online JSON Viewer.
    We can see the user has three folders, and how many files are in each folder, as well as the unique id for each folder.

Congrats, you’ve just finished Part 1 of Using Response Data. In the next tutorial we will use the Get Folders response to get the contents of a randomly chosen folder.


Tutorial Test Plan

Leave a Reply

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