Reputation: 1951
I have a service that is returning an image in base64 format. I want to write some unit tests for this and obviously want to go a little further that statusCode === 200
.
I have looked at this post that seems to get close to what I want with regex How to check whether the string is base64 encoded or not but it only matches if I remove the following:
"data:image/jpeg;base64,/9j"
This is what my response will look like:
{
"statusCode": 200,
"headers": {
"Content-Type": "image/jpeg"
},
"body": "data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQY.........
}
If I am going in the wrong way with testing this, please let me know a better solution.
Upvotes: 0
Views: 1000
Reputation: 55972
Another option could be to use the opposite approach to what you outlined in your answer:
Which could be to have a test mapping of images to Base64 encodings that you generate yourself, and then you could request the image from your server and assert against the base64 encoding that you generated yourself.
https://en.wikipedia.org/wiki/Oracle_%28software_testing%29
This should simplify your test logic because you know that if the base64 returned matches your oracle base64 it complies with the image-size
Upvotes: 1
Reputation: 1951
Actually, rather than playing with regex, I am going to do the following:
1: Strip the base64 off the header.
2: write the encoded string to a test file
3: Use https://github.com/image-size/image-size to verify the dimensions of the image are as expected.
This seems like a more solid testing strategy.
Upvotes: 0