DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Trending

  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Navigating Change Management: A Guide for Engineers
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • AI Speaks for the World... But Whose Humanity Does It Learn From?

How To Validate Error Responses in Mocking Services APIs

The implementation of mocking services plays a vital role in design-specific development. Here, we look at them through the lens of behavioral headers.

By 
vikas puri user avatar
vikas puri
·
Mar. 23, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
9.6K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

The implementation of mocking services plays a vital role in design-specific development. If we are developing any new project or implementing any new functionality by consuming some API’s which are still in a development phase, no consumers (front-end developer or any other) of the API will wait until the API is completely developed; they need some mocking service with some sample responses and error responses so they can start their development (assuming front-end dev in this case).

With mocking services, we can easily check what successful response we are getting, but it is quite difficult to validate the error response messages. Like how a mocking service can return an error message corresponding to 500 lines of status code, as there is no server involved.

Fortunately, we have “Behavioral Headers” for this, which we will discuss in this article.

What Are Behavioral Headers?

Behavioral headers enable you to change the behavior of the mocking service. There are some keys and values which you have to pass in the headers to test the expected behavior of mocking service. In this post, we are going to brief some of the Behavioral Headers provided by MuleSoft. If in case, you want to see it in detail, visit the MuleSoft website here.

Without wasting time, let's dive into the basic concepts of Behavioral Headers.

In order to test Behavioral Headers, we need the RAML specification and its Mocking Service URL. So I have created the sample RAML as shown in Figure 1, after publishing makes it public and get the mocking URL which in my case is:

https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/a9481520-ffff-4426-b4f7-466b04a1743c/testbehavioralheadersapi/1.0.1/m/users

Figure 1 Test of behavioral header

Figure 1 Test of behavioral header

Now in order to hit this, you can use any client; here I have used Postman.

Let us now go through some of the Behavioral headers:

  • MS2-Status-Code: MS2-Status-Code Behavioral Header will enforce mocking service to return an error response corresponding to a specific HTTP Status code. Suppose you want the mocking service to return an error response corresponding to 500 status code, there is no way to return that, so in order to achieve this, we just need to pass the following attributes in headers:

         Key: MS2-Status-Code

         Value: 500

          This you can see in Figure 2 as below:

                                                  ms2-status code example

Figure 2 An example of the ms2-status-code used to identify an error response

Note: If you pass any other value that is not implemented in the mocking service, it will throw an error message. Like in my case, I have not implemented the 404 error code, so when I pass 404 as a value, then it will throw an error message as shown in Figure 3.


404 code error message

Figure 3 404 as a value is passed since a 404 error code has not been implemented

  • S2-Delay:  Suppose we are consuming  API using front-end Angular, and we know that some API endpoints can have a delay while returning a response, and want to handle the front-end side by introducing Asynchronous concepts. How will mocking service return that delay? Well, here comes the role of MS2-Delay behavior headers.

It's very simple—you just need to provide one header with an MS2-Delay with time in milliseconds as shown in Figure 4.

 MS2-Delay with time in milliseconds

Figure 4 MS2-Delay with time in milliseconds

Note: You can only provide a maximum time delay of 10000 ms.

  • MS2-Randomize: While writing a mocking service, if we only provide data types and don’t want to provide response examples because of any reason (like shortage of time, etc.), but the consumer wants that response, then just by setting true corresponding to MS2-Randomize will generate random responses.

    For example, suppose that an API specification contained the definitions of this type:  
Properties files
 




xxxxxxxxxx
1


 
1
 types:
2
    User:
3
        properties:
4
            id: integer
5
            name: string



Now passing MS2-Randomize with true value in headers will generate a random response of something like the code below:        

JSON
 




xxxxxxxxxx
1


 
1
 {
2
    "id": 2317, 
3
    "name": "BIDSQ"
4
 }



  • MS2-Strict-Model-Validation

If you have some error in your RAML, but you want to test some other endpoints which are not having errors, you cannot do that until your API doesn’t have any errors, because when you invoke the mocking service it will validate your RAML first. If you want to skip the validation for mocking service, you need to set false corresponding to the MS2-Strict-Model-Validation in the Headers part.

Conclusion

We came to know about some Behavioral Headers which are useful while testing the mocking services like testing the specific error response code, introducing some delays, generating random responses, and skipping the validation of RAML.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!