Script to test if two specific key values are present in each item within Postman

Here is a sample of the tested response:

[
    {
        "label": "Summer '14",
        "url": "/services/data/v31.0",
        "version": "31.0"
    },
    {
        "label": "Winter '15",
        "url": "/services/data/v32.0",
        "version": "32.0"
    },
    {
        "label": "Spring '15",
        "url": "/services/data/v33.0",
        "version": "33.0"
    }
]

I am testing to ensure that each URL in the response matches the expected format: "/services/data/v" followed by the version number specified in the corresponding version key.

The script I have written checks this condition for each item in the response:

pm.test("URL formatted correctly and version matches version key", function () {
    const response = pm.response.json();
    
    response.forEach(item => {
        // Extract version from the URL
        const urlPattern = /^\/services\/data\/v(\d{2}\.\d)$/;
        const match = item.url.match(urlPattern);

        // Verify the URL follows the expected format
        pm.expect(match).to.not.be.null; // Ensure the URL matches the pattern

        // Extract the version from the URL if the pattern matched
        const urlVersion = match ? match[1] : null;

        // Verify the extracted version matches the version key
        pm.expect(urlVersion).to.eql(item.version); 
    });
});

I would appreciate feedback from experienced individuals in Postman testing or JavaScript to confirm if this script functions as intended based on the described requirements.

Answer №1

The regex you've designed successfully matches /services/data/v and properly verifies the version.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

My AJAX requests do not include any custom headers being sent

I'm facing an issue with making an AJAX request from my client to my NodeJS/ExpressJS backend. After firing the request, my backend successfully receives it but fails to recognize the custom headers provided. For example: $.ajax({ type: " ...

How can you delay the return of a function until after another asynchronous call has finished executing?

Currently, I am working on implementing route authentication in my project. To achieve this, I have decided to store the authenticated status in a context so that other React components can check whether a user is logged in or not. Below is the relevant co ...

The function `open` is not a valid prop for the `Dialog` component

Upon clicking an icon, a dialog box containing a form should appear for either adding a tab or deleting a specific tab. I have utilized reactjs, redux, and material-ui for the components. While I am able to display the dialog box when the icon is clicked, ...

Can you send an array of objects as data in an AJAX post request?

My primary objective is to gather the dropdown values from a webpage and then send them to a PHP file for processing. Currently, I am using jQuery to achieve this by creating an overall schedule array and adding each element to it for updating. Here' ...

Step-by-step guide on how to make a POST request with session data to a specific endpoint in a Node.js server

Currently, I am utilizing express and have a task to execute a POST request to an internal endpoint in the server. Below is the code snippet I am using: request({ url : 'http://localhost:3000/api/oauth2/authorize', qs:{ transaction_id:re ...

How can you write a parameterless function in Ramda using a point-free style?

Take a look at this functioning code snippet: var randNum = x => () => Math.floor(x*Math.random()); var random10 = randNum(10) times(random10, 10) // => [6, 3, 7, 0, 9, 1, 7, 2, 6, 0] The function randNum creates a random number generator that wi ...

Guide on importing a JS file into the main JS file using Three.js

I'm fairly new to Threejs and I am trying to organize my code by putting some of my threejs code in a separate JS file and then using it in my main.js file. Here is a simplified version of what I am trying to do: main.js import * as THREE from &ap ...

Setting a CSS Variable with the Help of jQuery

I need help with changing the background color of a specific .div element when it is clicked on. I want the color to be a variable that can be changed by the user, and this change should occur when the document is ready. Currently, I am using a CSS variab ...

preventing further executions by halting a function after the initial click

I've come across this function: function display() { $.ajax({ url: "new.php", type: "POST", data: { textval: $("#hil").val(), }, success: function(data) { ...

Creating a JSoup document with embedded JavaScript: A step-by-step guide

After generating a JSoup Document with two <script type="application/javascript">/* .. */</script> elements, I encountered an issue. The Problem: Whenever I use .html() or .toString() in JSoup, my JavaScript code gets escaped. if (foo &&a ...

Storing and Manipulating a JavaScript Object with Vuex: A New Perspective

Consider a hypothetical JavaScript object class like this: class Car { var engineTurnedOn = false; ... public turnEngineOn() { engineTurnedOn = true } } If I want to turn the engine on, should I create an action called 'turnEngineOn&ap ...

Ensure the page is always updated by automatically refreshing it with JavaScript each time it

I'm currently working on a web application that makes use of JQuery. Within my javascript code, there's a function triggered by an onclick event in the HTML which executes this line: $('#secondPage').load('pages/calendar.html&apos ...

Why is the variable showing as null when I use AJAX to send data with POST to PHP?

Here is the jQuery code snippet I am working with: $(document).ready(function() { $('input[type="submit"]').click(function() { event.preventDefault(); var email = $('.email').val(); $.ajax({ ty ...

Executing promises in a for-loop within a setTimeout function

Looking to integrate a web API with JavaScript/AngularJS 1.x on a timed interval due to rate limiting. The code provided attempts to achieve this by utilizing a list of objects called RecordList and a function called setDelay that introduces delays with th ...

Discovering the screen dimensions or viewport using Javascript in Bootstrap 4

Is there a method to identify the view port/screen size being utilized by Bootstrap 4 using Javascript? I've been struggling to find a dependable way to determine the current view port after a user resizes the browser. I attempted to use the Bootstra ...

Having issues with 'direction' in React withStyles causing errors

I am experiencing an issue with my React website where I am using the withStyles feature to apply styles to a material-ui Grid element. Specifically, when attempting to use direction: "column" in the style, I encounter the error message provided below. Th ...

jQuery: What is the reason this small function is not functioning as expected?

How can I add a number to the right of LI elements in sequence? Is there any difference between i = i+1; and i++;? If so, what is it? Check out this link for more information: http://jsfiddle.net/nicktheandroid/U8byW/9/ ...

Angular 2 components are experiencing issues with incorporating external JavaScript files

Currently, I am in the process of converting the altair admin template into an angular2 spa. To accomplish this, I am utilizing mgechev's angular-seed package which can be found here You can view my file structure by clicking here Below is a snippet ...

Issue with loading partial view in CodeIgniter using AJAX

My ajax function is working when trying to load a specific view (createClass.php), but it isn't functioning for other views like classAction.php. script: //--------------this works--------------- $(function(){ $(".classloader").click(function( ...

Silencing feature on the video control bar

I recently created a video embedded in a slider, with the intention of removing the start and stop buttons for a seamless experience. However, I now seek to include a mute button so that users have the option to silence videos with sound. Can anyone provid ...