POSTMAN | GET Variable for if - statement - postman

i want to do a if statement in Postman. I have the Environmet Variable: server_state1 & server_state2
These Variables both gets numbers from 0-3.
Now i want to write a Pre-request-Script in an PUT Statement.
The IF Statement sounds like
var server_state1 = postman.GetEnvironmentVariable("state_server1");
var server_state2 = postman.GetEnvironmentVariable("state_server2");
if (server_state1 === 0 && server_state2 === 0){
postman.SetEnvironmentVariable("server_state12", "Everything is fine");
}
But this does not work.

THE ANSWER IS:
var server_state1 = postman.GetEnvironmentVariable("state_server1");
var server_state2 = postman.GetEnvironmentVariable("state_server2");
if (server_state1 === "0" && server_state2 === "0"){
postman.SetEnvironmentVariable("server_state12", "Everything is fine");
}

The functions you are looking for are defined in lowerCamelCase.
Try to use:
postman.getEnvironmentVariable("state_server1")
postman.setEnvironmentVariable("server_state12", "Everything is fine");
Instead of:
postman.GetEnvironmentVariable("state_server1");
postman.SetEnvironmentVariable("server_state12", "Everything is fine");

Related

Postman test never returns "no pass"

I'm trying make a test to my API server and I don't get test result no pass.
My code:
var data = JSON.parse(responseBody);
var days = data["days"];
var total_distance = 0;
days.forEach(function(value,index,array){
total_distance = total_distance + value["distance"];
});
pm.test("Distance data"),function(){
pm.expect(data["total_distance"].to.equal(total_distance));
}
This script never returns no pass. What is my error?
The syntax for your test is incorrect. You have a closing parentheses after pm.test("Distance data" which should actually be the last character on the last line.
Try:
pm.test("Distance data", function () {
pm.expect(data["total_distance"].to.equal(total_distance));
});

IRC String comparison is always true

I'm trying to simply compare a line in a text file to today's date.
The line I want help with always seems to evaluate true for my code.
Any examples?
My code:
set %lines $lines(test.txt)
set %date $adate
while (%i <= %lines)
set %read $read(test.txt, n, %i)
if( %date isin %read ){ ; <-- Line in question
do things
}
}
You have a few errors.
Your while loop is missing an open bracket. (And closing at the end)
while (%i <= %lines) {
You must have a space between () { } and the rest of the lines
if<space>(
)<space> {
if ( %date isin %read ) {
I took the liberty of suggesting another version.
Code:
var %filename = test.txt
var %lines = $lines(%filename)
var %currentDate = $adate
var %i = 1
while (%i <= %lines) {
var %line = $read(%filename, n, %i)
if (%currentDate isin %line) {
# do things
# Should uncomment the break in case you want to stop after a match
#break
}
inc %i
}
I am sorry if I didn't understand, but what is the reason for a complex script just to check if there is a date format in a line of a text file?
There is no reason to set a variable %read to store the line of the loop in question, when you can do an IF condition after the loop:
var %x = 1
while (%x <= $lines(test.txt)) {
if ($adate isin $read(test.txt,n,%x)) {
;do things
}
inc %x
}

Array comparison?

I am making a function that takes in an example and an ip address. For ex.
compare('192.168.*','192.168.0.42');
The asterix indicates that the following parts of ip can be anything. The function returns true or false based on if the example and ip is a match. I tried this kind of solution.
var compare = function(example, ip){
var ex = example.split(".");
var ip = ip.split(".");
var t = 0;
for(var i=0; i<4; i++){
if(ex[i] == ip[i] || ex[i] == "*" || typeof ex[i] === 'undefined' && ex[i-1] == "*"){
t++
if(t==4){
return true
}
}else{
return false;
}
}
}
What are the main advantages of using regular expression over this solution? What would be the best regular expression to do this?
How about checking if they are not equal then just return false?
var compare = function(example, ip){
// You should have some basic IP validations here for both example and ip.
var ex = example.split(".");
var ip = ip.split(".");
for(var i=0; i<ex.length; i++){
if(ex[i]=='*')
break;
if(ex[i]!=ip[i])
return false;
}
return true;
}
alert(compare('333.321.*','333.321.345.765'));
alert(compare('333.322.*','333.321.345.765'));
alert(compare('333.321.345.*','333.321.345.765'));
This goes way better with regular expressions. Try this:
function compare(example, ip) {
var regexp = new RegExp('^' + example.replace(/\./g, '\\.').replace(/\*/g, '.*'));
return regexp.test(ip);
}
compare('192.168.*', '192.168.0.42'); // => true
compare('192.167.*', '192.168.0.42'); // => false
What this does is, it translates your pattern to an regular expression. Regular expressions are extremely powerful in matching string. It also covers cases like this:
compare('192.168.*.42', '192.168.1.42'); // => true
compare('192.167.*.42', '192.168.1.43'); // => false

ColdFusion Code as failing on if condition

Working with cfscript code in ColdFusion, The following seems correct to me, if client_discount is either 0 or NULL, just do not generate the UniqueKey, use existing else use new one. But it does work somehow, I am not sure what I am missing here, trying different cflib UDF's also:
Here is my code:
f = structnew();
f.discountoffered = '#arguments.structform.client_discount#';
writedump(arguments);
result = structFindKeyWithValue(f,f.discountoffered,"0","ALL");
writedump(result);
if((arguments.structform.client_discount EQ 0)
OR (arguments.structform.client_discount NEQ "")) {
f.orderunique = generateRandomKey();
}
else {
f.orderunique = '#arguments.structform.orderunique#';
}
NULL is kind of wonky in ColdFusion.
I would handle this by paraming the value so it gets a value I decide if it does not exist.
Add this code under f = structNew() - or at the beginning of the function, does not really matter.
param name="arguments.structForm" default="#structNew()#;
param name="arguments.structForm.client_discount" default="0";
This way if client_discount is not present, it is set to 0 - the first line is to make sure that structform exists in arguments and if not, sets it to an empty struct.
Then your if statement need only check if it is 0.
if( arguments.structForm.client_discount == 0 ){
f.orderunique = generateRandomKey();
}
else{
f.orderunique = arguments.structform.orderunique;
}
Of course...you would need to verify that arguments.structForm.orderunique exists before using it.
I think that's what you are trying to do
<cfscript>
f = structnew();
if(not isnull(arguments.structform.client_discount)){
f.discountoffered = '#arguments.structform.client_discount#';
result = structFindKeyWithValue(f,f.discountoffered,"0","ALL");
if((arguments.structform.client_discount EQ 0))
f.orderunique = generateRandomKey();
else
f.orderunique = '#arguments.structform.orderunique#';
}
else {
f.orderunique = '#arguments.structform.orderunique#';
}
</cfscript>

AS3 RegEx returns null

Can anyone explain why the code below traces null when on the timeline?
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
trace(str.match(cleanRegExp.toString()));
I've read the documentation, so I'm pretty sure that I'm declaring the RegEx correctly and that String.match() should only return null when no pattern is passed in, otherwise it should be an array with 0+ elements. I suspected a badly written expression, but surely that should still return an empty array?
EDIT: Both these trace "no matches" instead of either 5 or 0, depending on the expression being correct:
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Array = str.match(cleanRegExp);
trace((res == null) ? "no matches" : res.length);
And:
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Object = cleanRegExp.exec(str);
trace((res == null) ? "no matches" : res[0]);
UPDATE
If you're going to work in flash with regex, this tool is a must-have:
http://gskinner.com/RegExr/
http://gskinner.com/RegExr/desktop/
ORIGINAL ANSWER
Don't use toString(), you're then doing a literal search, which will include the addition of all of your regex formatting, including flags. Do:
str.match(cleanRegExp);
In fact the proper method is to reference the returned object like so:
var results:Array = str.match(cleanRegExp);
if(results != null){
//We have a match!
}