Skip to content

Commit

Permalink
apis: intercept api call and alter the response with test data (dummy…
Browse files Browse the repository at this point in the history
… json data extracted from file)
  • Loading branch information
ashwiniraokarai committed May 8, 2024
1 parent d3a609b commit 5e406b7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
38 changes: 31 additions & 7 deletions tests/intercepting-api-calls.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { test } from "@playwright/test"
import defaultObjectExported from "./test-data/articles";
import importedArticlesObject from "./test-data/articles";
import importedJSONFileParserObject from "./test-data/JSONFileParser";

test("can intercept and modify response with your own test data (dummy/ mock data)",
async({page}) => {
let endPointToIntercept = "https://conduit.productionready.io/api/articles?limit=10&offset=0";
await page.route(endPointToIntercept,
async (route) => {
console.log(`Intercepting route: ${endPointToIntercept} and sending dummy response body`);
console.log(defaultObjectExported.articlesData());
console.log(importedArticlesObject.articlesData());
await route.fulfill({
//convert the JS Object that contains the test data to JSON
body: JSON.stringify(defaultObjectExported.articlesData()),
body: JSON.stringify(importedArticlesObject.articlesData()),
});
}
);
Expand All @@ -20,10 +21,33 @@ test("can intercept and modify response with your own test data (dummy/ mock dat
}
);

test("can intercept and modify response data, with data extracted from a json file",
async({page}) => {}
test("can intercept and modify response data (via path), with data extracted from a json file",
async({page}) => {
const endPointToIntercept = "https://conduit.productionready.io/api/articles?limit=10&offset=0";
await page.route(endPointToIntercept,
async(route) => {
await route.fulfill({
path:"./tests/test-data/articles.json"
});
}
)
await page.goto("https://demo.realworld.io/#/");
}
);

test("can abort specific requests",
async({page}) => {}
test("can modify response data (via body), with data extracted from json file",
async({page}) => {
const endPointToIntercept = "https://conduit.productionready.io/api/articles?limit=10&offset=0";

await page.route(endPointToIntercept, async(route, request)=> {
const testDataJSONFilePath = "./tests/test-data/articles.json";
console.log(`Intercepting route ${request.url()} and responding with dummy data from a file`);
console.log(importedJSONFileParserObject.extractArticlesTestDataFrom(testDataJSONFilePath))
route.fulfill({
body: JSON.stringify(importedJSONFileParserObject.extractArticlesTestDataFrom(testDataJSONFilePath)),
})
})

await page.goto("https://demo.realworld.io/#/");
}
);
8 changes: 8 additions & 0 deletions tests/test-data/JSONFileParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from "fs";

function extractArticlesTestDataFrom(testDataJSONFilePath): Object {
const testDataJSONString = fs.readFileSync(testDataJSONFilePath, "utf8");
return JSON.parse(testDataJSONString);
}

export default {extractArticlesTestDataFrom};
31 changes: 31 additions & 0 deletions tests/test-data/articles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"articles": [
{
"slug":"slug-of-the-intercepted-articles-endpoint",
"title": "title of the intercepted articles endpoint",
"description": "description of the intercepted articles endpoint",
"body": "body of the intercepted articles endpoint",
"author": {
"username": "author: intercepted by ash karai"
}
},
{
"slug":"slug-of-the-intercepted-articles-endpoint",
"title": "title of the intercepted articles endpoint",
"description": "description of the intercepted articles endpoint",
"body": "body of the intercepted articles endpoint",
"author": {
"username": "author: intercepted by ash karai"
}
},
{
"slug":"slug-of-the-intercepted-articles-endpoint",
"title": "title of the intercepted articles endpoint",
"description": "description of the intercepted articles endpoint",
"body": "body of the intercepted articles endpoint",
"author": {
"username": "author: intercepted by ash karai"
}
}
]
}

0 comments on commit 5e406b7

Please sign in to comment.