Quantcast
Channel: Runscope Blog - API Monitoring and Testing
Viewing all articles
Browse latest Browse all 231

Using Snippets and Script Libraries to Monitor AWS APIs

$
0
0

Note: this blog post is about the Snippets and Script Libraries features that are available on the Medium plan or higher.

One of the most common programming principles that we learn as developers is to generally avoid repeating yourself, or DRY. Copy-pasting the same lines of code is replaced with creating abstractions in the form of methods or functions that can be used and re-used in multiple places across a project, which in turn will make your code easier to understand and modify.

That same idea of avoiding repeating code can be applied to Runscope API monitors by using our Snippets and Script Libraries features.

A few months ago we added the aws4.js library to our script-libraries repository and published a blog post on how you can use it to monitor AWS APIs that use Signature Version 4. In it, we covered how to add the script library to your account and then use it in a pre-request script.

In this post, we're going to make this process easier by creating a script snippet, which can then be reused across multiple requests. This will also make maintenance easier if you need to update the snippet JavaScript code, and every request that uses it will also get updated.

Creating a Snippet

To create a snippet, the first thing we need to do is click on the account's profile picture on the top-right, and select Script Library:

The Runscope dashboard when a user is logged in, showing a zoomed in version of the account's menu when clicking on the top-right profile image, and highlighting the Script Library menu option.

Here you can see any existing libraries or snippets that have already been added. Snippets and Libraries are shared for all users in the same team. Once we create our snippet for this tutorial, anyone else that's part of your Runscope team will be able to see it and use it:

The Script Library page of a logged in user, displaying a list of Existing Libraries and Snippets in the user account.

Scroll down to the Create a Snippet section, and you'll find the two required fields for adding a snippet. For this tutorial, we're creating a script that will sign any AWS S3 requests. First, we need to give it a name, for example, "sign s3 requests". In the Script field, we'll use the following JavaScript:

// S3 - GET Object
var opts = {service: 's3', path: request.path};
var awsObj = aws4.sign(opts, {accessKeyId: variables.get("AWS_ACCESS_KEY_ID"), secretAccessKey: variables.get("AWS_SECRET_ACCESS_KEY")});

for (var header in awsObj.headers) {
    request.headers[header] = awsObj.headers[header];
}
The Create a Snippet section of the Script Library page, showing the Name field with the text field containing "sign s3 requests", the Script field containing the JavaScript snippet before the image, and the "Create Snippet" button below the Script field highlighted.

Click on Create Snippet, and we should see our new snippet show up in the Existing Libraries section:

Showing the Script Library page, and the Existing Libraries section that was shown in a previous image now includes the new Snippet "sign s3 requests" in its list.

Using a Snippet

Now that we've added the snippet to account, all that's left is to use it in one of our test steps. Let's open one of our buckets, and create a new API monitor.

The first thing we need to do is enable the aws4.js library in our environment settings (follow the instructions here to add the aws4.js script library to your Runscope account):

Showing a Runscope API monitor in Editor mode, with the Environment Settings expanded. The Script Libraries menu inside the environment settings is selected, and it shows the "aws4.js" script library toggled "On".

We'll make a simple GET request to an AWS S3 API endpoint:

http://s3.amazonaws.com/hitchikers-guide-to-the-galaxy/dont-panic.txt

A GET request step in the Editor mode of a Runscope test, highlighting the Endpoint section with the AWS S3 URL from the previous code block.

And lastly, we can go to the Pre-request Script tab, click on Add Snippet, and select our "sign S3 request" snippet from the drop-down menu:

The same request step in Editor mode from the previous image, but this time with the Pre-request Scripts tab selected. The "Select Snippet:" dropdown menu has the "sign s3 requests" snippet selected and highlighted.

Run the request, and you should get a successful 200 OK response! Now, if you want to add other test steps to hit AWS S3 endpoints, you can just use the snippet instead of copying the same lines of code over and over again in your pre-request script area.

Conclusion

This is just one example of using snippets to avoid repeating lines of code multiple times, and also combining them with script libraries.

Check out our script-libraries GitHub repository for examples of other libraries you can use, and feel free to open an issue or PR with suggestions of other common tasks we could add to it!

And if you need any help with the steps above, please reach out to our awesome support team.


Viewing all articles
Browse latest Browse all 231

Trending Articles