Last active
September 6, 2022 01:35
-
-
Save yavorg/49551a0b34522791ef4b2192dda51fb4 to your computer and use it in GitHub Desktop.
Undici mocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"affenpinscher": [], | |
"african": [], | |
"airedale": [], | |
"akita": [], | |
"appenzeller": [], | |
"australian": [ | |
"shepherd" | |
], | |
"basenji": [], | |
"beagle": [], | |
"bluetick": [], | |
"borzoi": [], | |
"bouvier": [], | |
"boxer": [], | |
"brabancon": [], | |
"briard": [], | |
"buhund": [ | |
"norwegian" | |
], | |
"bulldog": [ | |
"boston", | |
"english", | |
"french" | |
], | |
"bullterrier": [ | |
"staffordshire" | |
], | |
"cattledog": [ | |
"australian" | |
], | |
"chihuahua": [], | |
"chow": [], | |
"clumber": [], | |
"cockapoo": [], | |
"collie": [ | |
"border" | |
], | |
"coonhound": [], | |
"corgi": [ | |
"cardigan" | |
], | |
"cotondetulear": [], | |
"dachshund": [], | |
"dalmatian": [], | |
"dane": [ | |
"great" | |
], | |
"deerhound": [ | |
"scottish" | |
], | |
"dhole": [], | |
"dingo": [], | |
"doberman": [], | |
"elkhound": [ | |
"norwegian" | |
], | |
"entlebucher": [], | |
"eskimo": [], | |
"finnish": [ | |
"lapphund" | |
], | |
"frise": [ | |
"bichon" | |
], | |
"germanshepherd": [], | |
"greyhound": [ | |
"italian" | |
], | |
"groenendael": [], | |
"havanese": [], | |
"hound": [ | |
"afghan", | |
"basset", | |
"blood", | |
"english", | |
"ibizan", | |
"plott", | |
"walker" | |
], | |
"husky": [], | |
"keeshond": [], | |
"kelpie": [], | |
"komondor": [], | |
"kuvasz": [], | |
"labradoodle": [], | |
"labrador": [], | |
"leonberg": [], | |
"lhasa": [], | |
"malamute": [], | |
"malinois": [], | |
"maltese": [], | |
"mastiff": [ | |
"bull", | |
"english", | |
"tibetan" | |
], | |
"mexicanhairless": [], | |
"mix": [], | |
"mountain": [ | |
"bernese", | |
"swiss" | |
], | |
"newfoundland": [], | |
"otterhound": [], | |
"ovcharka": [ | |
"caucasian" | |
], | |
"papillon": [], | |
"pekinese": [], | |
"pembroke": [], | |
"pinscher": [ | |
"miniature" | |
], | |
"pitbull": [], | |
"pointer": [ | |
"german", | |
"germanlonghair" | |
], | |
"pomeranian": [], | |
"poodle": [ | |
"miniature", | |
"standard", | |
"toy" | |
], | |
"pug": [], | |
"puggle": [], | |
"pyrenees": [], | |
"redbone": [], | |
"retriever": [ | |
"chesapeake", | |
"curly", | |
"flatcoated", | |
"golden" | |
], | |
"ridgeback": [ | |
"rhodesian" | |
], | |
"rottweiler": [], | |
"saluki": [], | |
"samoyed": [], | |
"schipperke": [], | |
"schnauzer": [ | |
"giant", | |
"miniature" | |
], | |
"setter": [ | |
"english", | |
"gordon", | |
"irish" | |
], | |
"sheepdog": [ | |
"english", | |
"shetland" | |
], | |
"shiba": [], | |
"shihtzu": [], | |
"spaniel": [ | |
"blenheim", | |
"brittany", | |
"cocker", | |
"irish", | |
"japanese", | |
"sussex", | |
"welsh" | |
], | |
"springer": [ | |
"english" | |
], | |
"stbernard": [], | |
"terrier": [ | |
"american", | |
"australian", | |
"bedlington", | |
"border", | |
"cairn", | |
"dandie", | |
"fox", | |
"irish", | |
"kerryblue", | |
"lakeland", | |
"norfolk", | |
"norwich", | |
"patterdale", | |
"russell", | |
"scottish", | |
"sealyham", | |
"silky", | |
"tibetan", | |
"toy", | |
"welsh", | |
"westhighland", | |
"wheaten", | |
"yorkshire" | |
], | |
"tervuren": [], | |
"vizsla": [], | |
"waterdog": [ | |
"spanish" | |
], | |
"weimaraner": [], | |
"whippet": [], | |
"wolfhound": [ | |
"irish" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MockAgent } = require('undici'); | |
const Breeds = require('./breeds.json'); | |
const agent = new MockAgent() | |
agent.disableNetConnect() | |
const client = agent.get('https://dog.ceo'); | |
client.intercept({ | |
path: '/api/breeds/list/all', | |
method: 'GET' | |
}) | |
.reply(200, { | |
"message": Breeds, | |
"status" : "success" | |
}); | |
function isValidBreedImagePath(path){ | |
const match = /\/api\/breed\/([\da-z-]*)\/images/.exec(path); | |
// If the overall path matched and the specific breed they specified | |
return match && (Breeds[match[1].toLowerCase()] != null); | |
} | |
client.intercept({ | |
path: isValidBreedImagePath, | |
method: 'GET' | |
}) | |
.reply(200, { | |
"message": [ | |
"https://images.dog.ceo/breeds/hound-walker/n02089867_149.jpg", | |
"https://images.dog.ceo/breeds/hound-walker/n02089867_1504.jpg", | |
"https://images.dog.ceo/breeds/hound-walker/n02089867_1504.jpg" | |
], | |
"status": "success" | |
}); | |
client.intercept({ | |
path: (path) => !isValidBreedImagePath(path), | |
method: 'GET' | |
}) | |
.reply(404, { | |
"message":"Breed not found (master breed does not exist)", | |
"status":"error", | |
"code":404 | |
}); | |
module.exports = agent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { request } = require('undici') | |
module.exports.getBreeds = async () => { | |
const { body } = await request('https://dog.ceo/api/breeds/list/all'); | |
const data = (await body.json()).message; | |
return data; | |
} | |
module.exports.getBreedImages = async ( breed ) => { | |
const { | |
statusCode, body | |
} = await request(`https://dog.ceo/api/breed/${breed}/images`); | |
let data = (await body.json()).message; | |
if (statusCode == 404){ | |
let e = new Error(data) | |
e.code = "BreedNotFound" | |
throw e; | |
} | |
return data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { getBreeds, getBreedImages } = require('./dog-client'); | |
const assert = require('assert'); | |
const DogClientMockAgent = require ('./dog-client-mock.js') | |
const { setGlobalDispatcher } = require('undici'); | |
setGlobalDispatcher(DogClientMockAgent); | |
describe('DogClient', function() { | |
describe('#getBreeds()', function() { | |
it('should return an object containing list of breeds', async function() { | |
const breeds = await getBreeds(); | |
assert.ok(breeds.hound); | |
assert.ok(breeds.chihuahua); | |
}); | |
}); | |
describe('#getBreedImages()', function() { | |
it('should return an array of images for known breeds', async function(){ | |
const images = await getBreedImages('hound'); | |
assert.ok(images[0].startsWith('https://')); | |
assert.ok(images[0].endsWith('.jpg')); | |
}); | |
it('should throw an error for unknown breeds', async function () { | |
try{ | |
const images = await getBreedImages('half-chimpanzee-half-elephant'); | |
} catch(e) { | |
assert.equal(e.code, "BreedNotFound") | |
} | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment