Compare two objects in cypress which are base64 - javascript

I'm saving image base64 into variable(base64Image) and would to compare with the image(local) in base64 format but I'm finding error.
var base64Image=res.body.image;
cy.readFile('cypress/e2e/Testdata/Uber Label.png','base64').then((original) => {
base64Image.should('eq',original);});
I'm expecting it to pass or help me how should I compare both values in cypress

Related

in node.js how to sanitize api requests that failed input validation for future security review

I need to sanitize api requests that failed input validation for future security review.
I have it in json format and I need to keep them in json format.
this is for example valid json but I can't log it to our system like that:
{
"<script>alert('test!!!!');</script>": "<script>alert('xss12!!!!');</script>"
}
I need a lib or some way in node.js to sanitize it and keep it json and then log it, for example convert it to utf:
{
"#utf3c#script#utf3c#alert('test#utf3c##utf3c##utf3c##utf3c#')#utf3c##utf3c##utf3c#script#utf3c#": "#utf3c#script#utf3c#alert('xss12#utf3c##utf3c##utf3c#')#utf3c##utf3c##utf3c#script#utf3c#"
}
I had the idea to convert it to string with JSON.stringify convert to codePointAt with this replacer with string replace and then convert back to json with JSON.parse but the string it generate can't be converted back to json easily, with complex nested json it breaks.
function replacer(match, index, wholeString) {
const result = "codePoint#" + wholeString.codePointAt(index);
return result;
}
const dataToEncodeAsString = JSON.stringify(dataToEncode);
const test = dataToEncodeAsString.replace(/[^a-zA-Z1-9:'" ]/g, replacer);
console.log(test);
I found this package flat
and used it to flat the structure of the deeply nested JSON and then clean all chars from its keys and values and then used flat again with unflatten function to bring the JSON to its original structure.
hope it will help others that need this kind of solution!

How to parsing the correct values from a json decoding [duplicate]

This question already has an answer here:
PHP or JavaScript issue when parsing JSON encoded PHP array into JavaScripts JSON.parse()
(1 answer)
Closed 1 year ago.
Hii ha ve written this question because i have a json converted to a string with a function of php called json_encode()
Something like this:
{ "data":"2","state":"false"}
when the original json that im trying to encode is like this:
{ "data":2,"state":false}
(Please note that the type of the variables are different, in the original i have an int number called data and boolean called state, but when i use json_encode() every variable goes to a string..)
The problem is when i try to json.PARSE() the value from the json encode of php in angular i donĀ“t get the correct value, every variable is a string...
For example, isntead of getting the boolean state of the variable, i get "false" or "true", and this is a problem...
There is a way to parse this avoiding this problem? basically my problem is when i parse the json in my angular project i dont get the correct type of variable..
Thanks!
You can use JSON.parse on an individual value to convert it into the desired type:
const oldValue = JSON.parse('{"data":"2","state":"false"}')
const result = {}
Object.keys(oldValue).forEach(key => {
result[key] = JSON.parse(oldValue[key]);
});
Here's the other way around:
const oldValue = JSON.parse('{"data": 2,"state": false}');
const result = {}
Object.keys(oldValue).forEach(key => {
result[key] = oldValue[key].toString();
});
Note: These examples assume flat objects. You'd need to recurse through the object's tree if some of the values are objects, for example {a: {b: "c"}}.

transform array of strings to array of objects

I have an array of strings (strings have an object shape). I would like to convert it to an array of objects.
I tried with JSON.parse but am not successful.
let a=["{Axis:1,Value:-74}", "{Axis:2,Value:7}", "{Axis:3,Value:-47}", "{Axis:4,Value:85}"]
Desired result
a=[{Axis:1,Value:-74}, {Axis:2,Value:7}, {Axis:3,Value:-47}, {Axis:4,Value:85}]
You could evaluate the objects, because they are not formatted as JSON standard.
let a = ["{Axis:1,Value:-74}", "{Axis:2,Value:7}", "{Axis:3,Value:-47}", "{Axis:4,Value:85}"],
result = a.map(s => eval(`(${s})`));
console.log(result);
There are a couple of issues here.
The first is that you won't be able to call JSON.parse on a because JSON.parse can only parse strings containing properly formatted JSON.
The second issue is that the strings in your array are not properly formatted JSON.
The solution for this would be to make the strings properly formatted JSON, as follows:
let a=['{"Axis":1,"Value":-74}", '{"Axis":2,Value:7}', '{"Axis":3,"Value":-47}', '{"Axis":4,"Value":85}'];
Then to create an array of those objects, you'll have to use Array.map, as follows:
a = a.map(e => JSON.parse(e));

How to convert array of images (from input field) into json string

I have an array of images created with
var a = Array.from(document.getElementById("id").files);
then I want to make a JSON string of that array with
var b = JSON.stringify(a);
but it produces nothing but an empty array. As I read this is a common problem with stringify but I couldn't find a solution.
Assuming your array of images are proper File objects, they are a type of binary data called blob.
JSON.Stringify won't work out of the box with blobs, but here is an answer with some workarounds: JSON.stringify or how to serialize binary data as base64 encoded JSON?

Passing an array of multiple base64 strings

There are base64 strings in an array that I need to send via jquery's $.post() to a php.
var base64s = [........];
$.post("getbase64.php",
{
'base64s': base64s
},
php:
$base64s = $_POST['base64s'];
//and used as `$base64s[$i]` inside a for loop.
It works only when there are max 2 base64 strings. Is there a maximum size for an array? or what might be the reason?

Categories

Resources