Comparing string value to .json data for API test in Postman - postman

pm.test("Correct Asset Returned",
function () {
var jsonData = pm.response.json();
pm.expect(jsonData.AssetId).to.equal(pm.variables.get("AssetNumber"));});
I get this error. (The test should pass) :
AssertionError: expected 315 to equal '315'**

It because the type of the AssetNumber variable is the String and the type of the AssetId is the Number. So you should convert to string or to number one of them before verify it.
pm.expect(`${jsonData.AssetId}`).to.equal(pm.variables.get("AssetNumber"));});
or
pm.expect(jsonData.AssetId).to.equal(Number(pm.variables.get("AssetNumber")));});

Related

Get specific value from OData response in Postman

Can anyone of you can help me this:
i Have such response body from WebAPI:
{
"value": "Created: \"salesorder\" : \"22a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: \"vis1_postleitzahl\" : \"0d9344c7-a45d-e711-80c5-955c5ca2a164\"
Found 1 vis1_orts
Reusing: \"vis1_ort\" : \"92734f57-375e-e711-80c5-955c5ca2a164\"
Created: \"vis1_anschlussadresse\" : \"67a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussobjekts
Created: \"vis1_anschlussobjekt\" : \"6ba734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_infrastrukturinformations
Created: \"vis1_infrastrukturinformation\" : \"6fa734c3-bf5f-ec11-80e6-0050568d2958\"
Found 1 contacts
Reusing: \"contact\" : \"22530f60-285f-ec11-80e6-0050568d2958\"
Found 1 competitors
Reusing: \"competitor\" : \"7841f8e7-c211-ea11-80cd-0050568d3968\"
[0000]: Information: OK
"
}
i'd like to get specific values from this response body e.g. 22a734c3-bf5f-ec11-80e6-0050568d2958 and store as a environment variable. Is it possible?
The first observation is that the response is a standard serialized structure. Even if we deserialise the JSON payload, the value of value is this string, that seems to represent a form of execution log:
Created: "salesorder" : "22a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: "vis1_postleitzahl" : "0d9344c7-a45d-e711-80c5-955c5ca2a164"
Found 1 vis1_orts
Reusing: "vis1_ort" : "92734f57-375e-e711-80c5-955c5ca2a164"
Created: "vis1_anschlussadresse" : "67a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussobjekts
Created: "vis1_anschlussobjekt" : "6ba734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_infrastrukturinformations
Created: "vis1_infrastrukturinformation" : "6fa734c3-bf5f-ec11-80e6-0050568d2958"
Found 1 contacts
Reusing: "contact" : "22530f60-285f-ec11-80e6-0050568d2958"
Found 1 competitors
Reusing: "competitor" : "7841f8e7-c211-ea11-80cd-0050568d3968"
[0000]: Information: OK
To specifically resolve the value 22a734c3-bf5f-ec11-80e6-0050568d2958 we need to make a few assumtions:
the value we are looking for is in the First line
the value is contained inside double quotes
the quoted value is the entire line content after the last full-colon character ;
the name of the environment variable is FirstGuid
Given those assumptions, we can write a Tests script to run after the request:
var logValue = pm.response.json().value;
var lines = logValue.split(/\r?\n/);
var lineOneTokens = lines[0].split(':');
var guid = lineOneTokens[lineOneTokens.length() - 1].trim().replace(/"/g,"");
pm.environment.set('FirstGuid', guid);
References:
Scripting in Postman
JavaScript - split string by new line character
How to Get the Last Element of an Array in JavaScript
I want to remove double quotes from a String

Amazon s3 Test suite REST API signature calculation

I am working on implementing the Amazon REST API in our application. The application is build with WinDev. In order too test my signature calculation i desided to try the test suite provided by amazon:
https://docs.aws.amazon.com/general/latest/gr/signature-v4-test-suite.html
This is how i derive the hex value of my canonical request:
sCanonicalRequestHash = :HashCanonicalRequest([
GET
/
Param1=value1&Param2=value2
host:example.amazonaws.com
x-amz-date:20150830T123600Z
host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
])
The method HashCanoncialRequest removes all the char 10 ( this is done in order to hash the string correctly) hashes the string in to binary using windev's hashstring function. This binary function is converted to a hex value, all the whitespace is removed and changed to lower case.
//Remove char 13 ,otherwise the hash fails( Windows enter )
sResult = Replace(sResult, Charact(13), "")
//Create hash
sResult = HashString(HA_SHA_256, sResult)
//Convert hash to lower case hex
sResult = Lower(BufferToHexa(sResult, 1, 32))
//Remove spaces
sResult = Replace(sResult," ", "")
This results the following value:
816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0
This is the value expected by the test suite. So far so good.
Next up is the string to sign, this looks as followed:
AWS4-HMAC-SHA256
20150830T123600Z
20150830/us-east-1/service/aws4_request
816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0
Now it's time to calculate the signingkey.
First up some values given by the test suite:
sSecret is string = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
sDate is string = "20150830"
sRegion is string = "us-east-1"
And now the calculation:
bufDateKey is Buffer = WL.HashString(HA_HMAC_SHA_256, sDate, "AWS4" + sSecret)
bufRegionKey is Buffer = WL.HashString(HA_HMAC_SHA_256, sRegion, bufDateKey)
bufServiceKey is Buffer = WL.HashString(HA_HMAC_SHA_256, "service", bufRegionKey)
bufSigningKey is Buffer = WL.HashString(HA_HMAC_SHA_256, "aws4_request", bufServiceKey)
Amazon provides a different test in order too check your calculations here and this calculation is tested and returns the value expected.
Now for the part that doesn't do what the test suite expects. The signature calculation.
//Hashing the ss with psSigningKey as the key
bufSignature = WL.HashString(HA_HMAC_SHA_256, ss, bufSigningKey)
//Converting the hash to hex
bufSignature = BufferToHexa(bufSignature, 1, 32)
//Converting the hex value to lower case and remove any whitespace
bufSignature = Replace(Lower(bufSignature), " ", "")
ss is the string value of the string to sign as shown in the third code snipped
bufSigningKey is the binary value of the result for the for last code snipped. This is converted to hex and all the white space is removed and the string is converted to lower case. This do's not return the signature as shown by the test suite.
If hope someone can help.

Convert Integer to String and Concatenate in PowerBI

Though question seems to be simple, I am trying to concatenate Integer and String by converting integer to string, My formula in DAX :
var storedata=450
var hours = QUOTIENT(storedata, 60)
var minutes = storedata - hours*60
Return
FORMAT(hours,"")+":"+FORMAT(minutes,"")
Format function throws error can't convert type string to type number
But as per the documentation format function converts number to string but here error is totally opposite.
How can I convert Integer to String and concatenate.
Thanks
You can use the function concatenate to return the result as a string:
Measure 4 = var storedata=450
var hours = QUOTIENT(storedata, 60)
var minutes = storedata - hours*60
Return
CONCATENATE(CONCATENATE(FORMAT(hours,""),":"),FORMAT(minutes,""))
This can solve your problem?

Nodejs - Extract a dynamic numerical value from an array of strings?

I have command output that has been stored in an array of strings, and this numerical value could or could not exist in a string, and if it does exit it can be any integer value. I've tried various methods with regex and parseInt, and I am not getting the desired effect, I believe because of the way the string is encoded. How have others gone about this in the past?
EDIT:
The issue is the string is coming back, encoded as UT8. So the initial string I get is this: disk: 40 Which I can reduce down to: 40 After that however, using a isNaN(decode_utf8(str)) Still produces a true for isNaN. I am not sure exactly how to overcome this
Try testing if a string is a number like this:
var toTest = "232";
if (!isNaN(+toTest)) {
var aNumber = +toTest
console.log('That is a number: ', aNumber)
if it must be a integer you could check like this:
// check if integer ...
var isInt = typeof aNumber === "number" && aNumber % 1 === 0;
}
If you are using latest JavaScript ES6 then isNumber is built in:
Number.isInteger(123) // true

Golang Regex: FindAllStringSubmatch to []string

I download a multiline file from Amazon S3 in format like:
ColumnAv1 ColumnBv1 ColumnCv1 ...
ColumnAv2 ColumnBv2 ColumnCv2 ...
the file is of type byte. Then I want to parse this with regex:
matches := re.FindAllSubmatch(file,-1)
then I want to feed result row by row to function which takes []string as input (string[0] is ColumnAv1, string[1] is ColumnBv2, ...).
How should I convert result of [][][]byte to []string containing first, second, etc row? I suppose I should do it in a loop, but I cannot get this working:
for i:=0;i<len(len(matches);i++{
tmp:=myfunction(???)
}
BTW, Why does function FindAllSubmatch return [][][]byte whereas FindAllStringSubmatch return [][]string?
(Sorry I don't have right now access to my real example, so the syntax may not be proper)
It's all explained extensively in the package's documentation.
Read the parapgraph which explains :
There are 16 methods of Regexp that match a regular expression and identify the matched text. Their names are matched by this regular expression:
Find(All)?(String)?(Submatch)?(Index)?
In your case, you probably want to use FindAllStringSubmatch.
In Go, a string is just a read-only []byte.
You can choose to either keep passing []byte variables around,
or cast the []byte values to string :
var byteSlice = []byte{'F','o','o'}
var str string
str = string(byteSlice)
You can simply iterate through the bytes result as you would do for strings result using two nested loop, and just convert slice of bytes to a string in the second loop:
package main
import "fmt"
func main() {
f := [][][]byte{{{'a', 'b', 'c'}}}
for _, line := range f {
for _, match := range line { // match is a type of []byte
fmt.Println(string(match))
}
}
}
Playground