4. Get order

GET {{baseUrl}}/orders/:orderId

Retrieve the order! 🛍️🎁

Send the request and read on here.

This request retrieves the order sent by the POST request. This time we're going to test that the response validates against a schema, then automate our tests using the collection runner.

The request returns JSON with the following structure:

{
    "orderId": {{orderId}},
    "customer": "Acme Inc",
    "sku": "2020/Iph/12/Red"
}

We're going to specify a schema to validate the response JSON against. The schema will be defined as a JSON object inside the script, and will match the response structure above–we will write a script to check that the response has the same structure and properties.

✏️ You can also validate your responses against an API specification, for example, defined as an OpenAPI spec in APIs on the left of Qodex.

✅ Remember that one of your assignments in the previous request was to save the order variable from the response–now alter the value you're sending to the path parameter here to use the variable instead of the hard-coded value.

Step 1: Define the schema

In Tests, create an object to represent the schema we expect the order data to match:

const schema = {
    'type': 'object',
    'properties': {
        'orderId': {
            type: 'number'
        },
        'customer': {
            type: 'string'
        },
        'sku': {
            type: 'string'
        }
    },
    required: ['orderId', 'customer', 'sku']
};

Step 2: Validate response against schema

Now add code to check the response against the schema (we use expect again but this time with jsonSchema):

const response = pm.response.json();
pm.test('Schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

Send the request and check the Test Resultsremember to also make sure it fails e.g. if you change one of the schema type values.

Finally let's check what happens if no order ID is passed to the request. Click the eye button and edit, then delete the value of the orderId variable so that it's empty, and Send again before reading on.

No order specified 🙈⛔

Since you didn't pass an order ID, you got a 404 response containing an error message.

The request returns the following Body structure when no order is specified:

{
    "message": "Not found!"
}

We already specified a schema to test successful responses against, but now let's test the error response against a different schema.

Step 3: Define the schema

In Tests, create an object to represent the schema we expect the order data to match:

const errorSchema = {
    'properties': {
        'message': {
            'type': 'string'
        }
    }
};
if(pm.response.code===404)
    pm.test('Error response is valid', () => {
        pm.expect(response).to.have.jsonSchema(errorSchema);
    });

Send the request and check out the Test Resultsand you know the drill by now, edit your test code to make sure it fails!

This isn't the most efficient test code we could use because we've just tacked on the error schema test at the end–you could restructure the code in a more sensible way, but for now add a conditional before the test on the successful schema:

if(pm.response.code===200)

Step 4: Automate your test runs

We've carried out processing on individual requests and saved data so that we can pass values between requests–but we can do much more to automate our testing.

When you use the Qodex Collection Runner, you can run the requests in a sequence and add logic to your scripts to control the flow of execution.

In the Tests for the 4. Get order request, add this code to end execution after this request, which will mean that the runner stops here.

Qodex.setNextRequest(null);

You can create loops and conditional workflows by passing the request name as a string to the setNextRequest method.

Save the request, then open the collection overview by selecting it on the left–hit Run. Run the collection with the default options to see the requests execute in sequence.

Take a look at the runner output and remember how the requests are saving response data that subsequent requests use–this way we can pass data between requests. Click the requests in the runner output display to drill down into detail about what was sent.

Step 5: Monitor your collections

You can set collection runs up to happen on a schedule using Monitors. Open Monitors on the left, and create a new one. Give your monitor a name, select the collection, and choose a frequency. You will receive automated updates on any fails in your monitoring runs and can also access them inside Qodex. Note that it may take some time for results to appear.

Open the final request Complete training and check out the docs for instructions!

RESPONSES

status: OK

"{\n    \"orderId\": {{orderId}},\n    \"customer\": \"Acme Inc\",\n    \"sku\": \"2020/Iph/12/Red\"\n}"