Mock REST API Server
A mock API server imitates a real API server by providing realistic responses to requests. Responses simulate the data the real API would return by matching the schema (data types, arrays, objects etc.)
A mock API server is very handy during development when we don't have any live data yet. You can use mock APIs and mock data to work concurrently on the front-end (could be a React Client App) and the back-end API (could be a Node/Express Rest API). A mock API allows the front-end developers to consume a test API with the same interface as the eventual live API.
You can also use Mock APIs in many other situations:
- You want to fake results from a public API, especially in cases when you incur a charge from the provider.
- You want to fake results from a public API when you are not connected to the internet.
- You want to do faster unit testing against a local API instead of making lots of calls against a public API.
I will look at a way of creating Mock APIs rapidly in a future article.
Mock Data
Before you can work on constructing the Mock API, you need Mock data. Lots of it. Massive amounts of data. So, how do you generate this data that matches your schema? You don't want to manually create data by typing every single field in every single row. That is too tedious and too expensive.
One way to do this is by using web scraping (if the data is there on the internet). You can use web scraping using Node.js or Python to construct your data. I will be exploring this in a few articles shortly.
Another way to do this is to generate the (fake) data by using web sites like JSON-Generator or by using Node.js and NPM packages like Faker and Lodash.
JSON-generator
If you were to do a google search for "json generator" you will find that there are several websites which will help you generate JSON data for the schema that you specify. Here we will look at how we can use the JSON-Generator web site to construct the data.
https://www.json-generator.com/
IMDB Example -- Data we are mocking here
Let us say that you are working on a client app that displays information about Movies -- similar to what is shown on the IMDB website. If you look at the IMDB page for the movie "Joker" or the movie "The Shawshank Redemption", you will see that every movie has the following data:
- Title - Let us assume for mocking purposes that we have a random title with 4 words (use a lorem ipsum text generator function)
- Year of release - 4 digit number from say 1920 to 2019
- MPAA Rating -- one of G, PG, PG-13, R, NC-17
- Running time -- say from 1 hr to 4 hrs
- Genre -- one or more genres (say max of 3) from a list of about 20 -- Action, Adventure, Animation, Biography, Comedy, Crime, Documentary, Drama, Family, Fantasy, Film Noir, History, Horror, Music, Musical, Mystery, Romance, Sci-Fi, Short Film, Sport, Superhero, Thriller, War, Western
- Date of Release
- IMDB User rating - 0 through 10. Realistically, say 4 through 10
- Rating count -- number of people who have rated the movie
- Director(s) - one or more; one to three for mocking purposes
- Writer(s) - one or more; one to three for mocking purposes
- Stars - one or more; Realistically say 5 through 10
- Metascore - from 20 to 100
- Number of user reviews - from 1 to 10,000
- Number of critic reviews - from 1 to 350
- Plot summary
https://www.imdb.com/title/tt7286456/?ref=fnaltt1 https://www.imdb.com/title/tt0111161/?ref=nvsr1?ref=nvsr1
JSON Template for generating IMDB Mock Data
[
{
'repeat(3)': {
ID: '{{index()}}',
Title: '{{lorem(4, "words")}}',
Release_Year: '{{integer(1920, 2019)}}',
MPAA_Rating: '{{random("G", "PG", "PG-13", "R", "NC-17")}}',
Running_Time(tags) {
var num = integer(60,240);
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
return rhours + "h " + rminutes + "min";
},
Genre: [
{
'repeat(1,3)': '{{random("Action", "Adventure", "Animation", "Biography", "Comedy", "Crime", "Documentary", "Drama", "Family", "Fantasy", "Film Noir", "History", "Horror", "Music", "Musical", "Mystery", "Romance", "Sci-Fi", "Short Film", "Sport", "Superhero")}}'
}
],
Release_Date: '{{moment(this.date(new Date(1920, 0, 1), new Date())).format("DD MMMM YYYY")}}',
IMDB_Rating: '{{floating(4.0, 10.0, 1)}}',
Ratings_Count: '{{integer(500, 5000000)}}',
Directors: [
{
'repeat(1,3)': '{{firstName()}} {{surname()}}'
}
],
Writers: [
{
'repeat(1,3)': '{{firstName()}} {{surname()}}'
}
],
Actors: [
{
'repeat(5,10)': '{{firstName()}} {{surname()}}'
}
],
Metascore: '{{integer(20, 100)}}',
Number_UserReviews: '{{integer(1, 10000)}}',
Number_CriticReviews: '{{integer(1,350)}}',
Plot_Summary: '{{lorem(1, "paragraphs")}}'
}
}
]
-
Generate 3 rows of random IMDB Movie data -- As an array of 10 JSON objects
'repeat(10)'
-
ID: Is defined as an index of the current cloned / generated row starting from 0.
ID: '{{index()}}',
-
Title: set up as random Lorem Ipsum text of 4 words each using the lorem function. You could change the first parameter from 4 to anything else that you want.
Title: '{{lorem(4, "words")}}'
-
Release_Year - 4 digit number generated using the integer function. It generates a random integer in the range from 1920 to 2019
Release_Year: '{{integer(1920, 2019)}}',
-
MPAA Rating -- one of G, PG, PG-13, R, NC-17; Use the random function to select from one of the 5 ratings
MPAA_Rating: '{{random("G", "PG", "PG-13", "R", "NC-17")}}'
-
Running time -- say from 1 hr to 4 hrs; Use a javascript function to convert an integer between 60 and 240 to an hour and minute format; So 121 minutes becomes 2h 1min
Running_Time(tags) { var num = integer(60,240); var hours = (num / 60); var rhours = Math.floor(hours); var minutes = (hours - rhours) * 60; var rminutes = Math.round(minutes); return rhours + "h " + rminutes + "min"; },
-
Genre -- one or more genres (say max of 3) from a list of about 20 -- Action, Adventure, Animation, Biography, Comedy, Crime, Documentary, Drama, Family, Fantasy, Film Noir, History, Horror, Music, Musical, Mystery, Romance, Sci-Fi, Short Film, Sport, Superhero, Thriller, War, Western. Use the random function to select up to 3 genres from the list of 20
Genre: [ { 'repeat(1,3)': '{{random("Action", "Adventure", "Animation", "Biography", "Comedy", "Crime", "Documentary", "Drama", "Family", "Fantasy", "Film Noir", "History", "Horror", "Music", "Musical", "Mystery", "Romance", "Sci-Fi", "Short Film", "Sport", "Superhero")}}' } ],
-
Date of Release - Select a random date from 1920 to 2019, formatted using the Moment.js
Release_Date: '{{moment(this.date(new Date(1920, 0, 1), new Date())).format("DD MMMM YYYY")}}',
-
IMDB User rating - 0 through 10. Realistically, say 4 through 10. Use a Random float number in specified range with 1 decimal.
IMDB_Rating: '{{floating(4.0, 10.0, 1)}}'
-
Rating count -- number of people who have rated the movie; Random integer from 500 to 5 million
Ratings_Count: '{{integer(500, 5000000)}}',
-
Director(s) - one or more; one to three for mocking purposes
Directors: [ { 'repeat(1,3)': '{{firstName()}} {{surname()}}' } ],
-
Writer(s) - one or more; one to three for mocking purposes
Writers: [ { 'repeat(1,3)': '{{firstName()}} {{surname()}}' } ],
-
Stars - one or more; Realistically say 5 through 10
Actors: [ { 'repeat(5,10)': '{{firstName()}} {{surname()}}' } ],
-
Metascore - from 20 to 100
Metascore: '{{integer(20, 100)}}',
-
Number of user reviews - from 1 to 10,000
Number_UserReviews: '{{integer(1, 10000)}}',
-
Number of critic reviews - from 1 to 350
Number_CriticReviews: '{{integer(1,350)}}',
-
Plot summary - one Lorem Ipsum paragraph
Plot_Summary: '{{lorem(1, "paragraphs")}}'
Generate the data
That is it. When you have set up the above template, you can hit the Generate / Update button to generate your mock data. You can then change the number of rows that you want by changing the parameter to the repeat function at the top. You can also change any of the other random parameters -- the number of actors, directors, writers, the length of the title or plot summary etc. etc.
The generated data could then be stored in a MongoDB collection or a JSON file to serve as the data for your Mock REST API Server.
Here is what the generated data looks like:
[
{
"ID": 0,
"Title": "commodo ea eiusmod nulla",
"Release_Year": 2012,
"MPAA_Rating": "R",
"Running_Time": "1h 21min",
"Genre": [
"Musical"
],
"Release_Date": "11 September 2017",
"IMDB_Rating": "6.7",
"Ratings_Count": 3521127,
"Directors": [
"Burgess Bentley",
"Finch Blevins",
"Hendrix Rollins"
],
"Writers": [
"Kasey Solomon"
],
"Actors": [
"Amalia Pena",
"Sheree Fleming",
"Lilly Yates",
"Peterson Pennington",
"Nichols Cohen",
"Warner Rosa",
"Naomi Goodwin",
"Cline Moran"
],
"Metascore": 79,
"Number_UserReviews": 8118,
"Number_CriticReviews": 235,
"Plot_Summary": "Culpa ex nostrud ullamco sint magna voluptate minim nostrud deserunt consectetur enim. Dolore aliquip pariatur aute commodo deserunt. Dolore do ut culpa magna nostrud esse Lorem ullamco non ea in. Officia excepteur nulla nostrud Lorem eu incididunt sunt veniam. Dolor nostrud pariatur magna ullamco sit duis eu ad sunt ipsum eu enim."
},
{
"ID": 1,
"Title": "nulla quis deserunt eiusmod",
"Release_Year": 1958,
"MPAA_Rating": "PG-13",
"Running_Time": "2h 26min",
"Genre": [
"Comedy",
"Animation"
],
"Release_Date": "27 October 1935",
"IMDB_Rating": "6.4",
"Ratings_Count": 3182254,
"Directors": [
"Duffy Mckay",
"Jannie Griffin",
"Esther Pacheco"
],
"Writers": [
"Mai Slater",
"Valerie Peck",
"Flossie Kirkland"
],
"Actors": [
"Blevins Mills",
"Hale Moses",
"Concetta Guzman",
"Ashley Langley",
"Patty Everett",
"Kaufman Hernandez",
"Kate Conley",
"Lee Gillespie"
],
"Metascore": 31,
"Number_UserReviews": 2865,
"Number_CriticReviews": 29,
"Plot_Summary": "Ex elit consectetur veniam fugiat deserunt in deserunt velit non Lorem Lorem id. Non in fugiat consectetur pariatur aute eiusmod pariatur. Commodo quis id minim nisi reprehenderit. Adipisicing esse occaecat nostrud ad irure fugiat. Excepteur deserunt aliquip officia Lorem irure culpa enim amet esse ea voluptate ad."
},
{
"ID": 2,
"Title": "magna in labore id",
"Release_Year": 1953,
"MPAA_Rating": "NC-17",
"Running_Time": "1h 27min",
"Genre": [
"Sci-Fi",
"Horror"
],
"Release_Date": "26 January 1957",
"IMDB_Rating": "5.7",
"Ratings_Count": 2090527,
"Directors": [
"Carlene Harding",
"Danielle Sargent",
"Kelley Fowler"
],
"Writers": [
"Chang Joyce",
"Obrien Hampton",
"Byers Mccullough"
],
"Actors": [
"Herminia Mcintyre",
"Isabel Morton",
"Ester Bryan",
"Erica Gates",
"Colon Coffey",
"Etta Moody",
"Ellis Duncan",
"Bennett Rogers",
"Sherry Guerrero",
"Hansen Justice"
],
"Metascore": 75,
"Number_UserReviews": 5380,
"Number_CriticReviews": 233,
"Plot_Summary": "Aute in eu non veniam consectetur ut tempor aliquip nulla mollit voluptate. Ipsum culpa amet occaecat duis cillum labore sint eiusmod eu. Ea culpa excepteur aute tempor id commodo sunt mollit veniam excepteur in proident exercitation esse. Veniam cupidatat aliqua occaecat adipisicing nulla pariatur proident non sit enim eu ipsum. Dolore culpa eiusmod et id aute non nisi dolor do do elit pariatur ad est."
}
]