How to add a linebreak in test::base/test::nginx/perl test case? - perl

I was writing test cases while implementing a feature in Apache APISIX where I came across a stubborn error where there is an extra linebreak in the "got" part in comparison to the "expected" part.
Here is the code for the test:
=== TEST 8: get value from vault: token env var wrong/missing
--- config
location /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://VALT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- request
GET /t
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
=== TEST 9: get value from vault: token env var contains wrong token
--- config
location /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://WRONG_VAULT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- request
GET /t
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
I tried adding "\n" at the end of the expected part like so:
--- response_body
failed to decode result, res: {"errors":["permission denied"]}\n
But that didn't work. So I tried to surround the "expected" part in quotes "..." so that the linebreak gets included that didn't work either.
Another approach would be to remove the linebreak from the "got" part by writing some code but I think that wouldn't be an ideal thing to do (modifying the response).
Thanks in advance!!
Here's the link to the workflow action run.

It was simple! I didn't know we could use regex to match "got" with "expected". This is how I did it.
I replaced this:
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
With this:
--- response_body_like
failed to decode result, res: {\"errors\":\[\"permission denied\"\]}\n
The idea was to use the response_body_like construct to be able to use regex for expression matching. Thanks to this blog.

Related

Calling Command Line From Blue Prism

I am trying to use Nitro PDF Reader from the command line.
This was working correctly, but I am now getting an error Internal : Could not execute code stage: Ambiguous match found.
Passing null values also produces this error.
Code:
timedOut = False
Dim startTime as Date = Date.Now
Dim info as New ProcessStartInfo(appn)
If args <> "" Then info.Arguments = args
If dir <> "" Then info.WorkingDirectory = dir
Using proc As Process = Process.Start(info)
timedOut = Not proc.WaitForExit( _
CInt(timeout.TotalMilliseconds))
End Using
Issue looked to be caused by another action with similar code. There was an action called Run Process until Ended. I then duplicated to have a second which was called Run Process.
This was causing the error.

AKAudioFile exportAsynchronously path errors

I'm trying to use AKAudioFile.exportAsynchronously to convert wav to m4a (based on the sample code here: https://audiokit.io/playgrounds/Playback/Exporting%20Files/). I've chosen .documents as my BaseDirectory, but I just keep getting directory <my_dir> isn't valid errors — e.g.:
AKAudioFile+ProcessingAsynchronously.swift:exportAsynchronously(name:baseDir:exportFormat:fromSample:toSample:callback:):379:ERROR AKAudioFile export: directory "/var/mobile/Containers/Data/Application/20C913AD-B2F4-4F26-AAD2-0DFA0C65A886/Documents/All Of Me.mp4" isn't valid
That URL looks completely reasonable, to me, so what's up?
Okay, following #jake's tip, the solution was to handle the spaces explicitly before passing into AKAudioFile's exportAsynchronously(name:baseDir:exportFormat:callback:). I just did:
var name = String(cafURL.lastPathComponent.split(separator: ".")[0])
name = name.replacingOccurrences(of: " ", with: "%20")
let exportFile = try AKAudioFile(readFileName: "\(name).wav", baseDir: .documents)
exportFile.exportAsynchronously(name: name, baseDir: .documents, exportFormat: .m4a, callback: self.callback)

Matlab: check if string has a correct file path syntax

I want to check if a string represents a full path of a file, like this:
p = 'C:\my\custom\path.txt'
The file does not exist, so commands like isdir and exist return false to me, but still the format of the string represents a valid path for my operating system, while the following one does not because it has an invalid character for the file name:
p = 'C:\my\custom\:path.txt'
So I'd like to know how to check if a string represents a valid file path without needing that the file actually exists.
You might want to use the regexp function with a regular expression to match Windows paths.
if isempty(regexp(p, '^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$', 'once'))
// This is not a path
end
You can also let Matlab try for you:
if ~isdir(p)
[status, msg] = mkdir(p);
if status
isdir(p)
rmdir(p)
else
error(msg)
end
end
First, you check if the folder exists, if not you try to create it. If you succeed then you delete it, and if not you throw an error.
This is not recommended for checking many strings but has the advantage of being cross-platform.
function bool = isLegalPath(str)
bool = true;
try
java.io.File(str).toPath;
catch
bool = false;
end
end

error: unexpected if for unknown reasons

I don't have a clue why this happens, but here is code of the file:
express = require "express"
fs = require "fs"
router = express.Router()
module.exports = (config) ->
fileRe = /^([a-zA-Z0-9-_]+)(\.(png|jpg|jpeg))?$/i
router.get '/:file', (req, res) ->
file = req.params.file
if !file or !fileRe.test file
return res.error(404).send "404 Not Found"
fileInfo = fileRe.exec file
if !fileInfo[3]
# .png is default if extension is ommited
# abc => abc.png, but abc.jpg will stay
file += ".png"
if fs.access config.destination + file, fs.F_OK, (err) ->
if err
res.status(500).send "An error occured"
else
if !fileInfo[3] or fileInfo[3] == "png"
ct = "image/png"
else if fileInfo[3] == "jpg"
ct = "image/jpg"
opts =
lastModified: false
headers:
"Content-Disposition": "inline; filename=\"#{file}\""
"Content-Type": ""
return router
I get the following error
/home/kindlyfire/Webroot/uplimg-server/src/web/view.coffee:24:9: error: unexpected if
if fs.access config.destination + file, fs.F_OK, (err) ->
^^
I looked at the spaces, no problem there. Has anybody an idea about what it might be ?
What you wrote is not valid coffeescript. Specifically, it is the commas on the line the error is pointing you to. I'd offer info on how to fix it, but I can't even tell what you were trying to accomplish here. You have to provide a way for the compiler (not to mention readers) to be able to tell, unambiguously, what divisions you want in your code:
# fine
if foo then (a, b) -> c
# also fine
if foo
(a, b) ->
c
# ??
if foo (a, b) -> c
# ????
if foo a, b -> c
Repro of the bug. Note that this is a good example of how to make a minimum reproduction of the problem. I highly, highly recommend you read a coffeescript style guide and discipline yourself to follow it. Which one is not terribly important, its the consistency that matters. Do not just randomly copy-paste stuff from the internets into your code, re-write it to follow the same style as the rest of your code. Doing so will often have the added benefit of realizing how the snippet you copied is working.

Selenium IDE - always fail on any 500 error

Is there an easy way to tell Selenium IDE that any action that results in a http 500 response means the test failed?
I have tests that are 75 page requests long. Sometimes, I get a crash and burn somewhere in the middle, but the tests come back green.
Taking a look at selenium-api.js, I saw that there is a parameter ignoreResponseCode in the signature of the doOpen method in selenium-api.js :
Selenium.prototype.doOpen = function(url, ignoreResponseCode) {
This parameter is used by the browserbot object :
if (!((self.xhrResponseCode >= 200 && self.xhrResponseCode <= 399) || self.xhrResponseCode == 0)) {
// TODO: for IE status like: 12002, 12007, ... provide corresponding statusText messages also.
LOG.error("XHR failed with message " + self.xhrStatusText);
e = "XHR ERROR: URL = " + self.xhrOpenLocation + " Response_Code = " + self.xhrResponseCode + " Error_Message = " + self.xhrStatusText;
self.abortXhr = false;
self.isXhrSent = false;
self.isXhrDone = false;
self.xhrResponseCode = null;
self.xhrStatusText = null;
throw new SeleniumError(e);
}
I've tried calling the open function from selenium IDE with value = false and this results in an error (test failed).
My PHP test page was :
<?php
header('HTTP/1.1 500 Simulated 500 error');
?>
And this results in :
For me, this solves the problem of checking HTTP response status.
Make a JavaScript file called "user-extensions.js" and add it to the Selenium-IDE under Options > Options. If you are running Selenium RC, pass it into the parameter when starting up your server in the jar command. There should be a user extensions javascript file attribute.
Then close and restart Selenium-IDE. The User-Extensions file is cached when the IDE starts up.
Add this code to your Selenium user-extensions.js file to make a custom command called "AssertLocationPart". As you know "assertLocation" and "storeLocation" are standard commands. I tried to reduce the extra line of code to storeLocation just by getting the href in the custom function. I wasn't able to get the doAssertValue command to work. I'll have to post my own question for that. That's why it's commented out. For now, just use "this.doStore" instead. And add an extra line to your script after your custom AssertLocationPart command. Since we're not actually doing an assertion in the custom function/command, we should call it "storeLocationPart" (function would be named "doStoreLocationPart"), not "assertLocationPart" (function would be named "doAssertLocationPart"), and just pass in the first parameter. But if you can get the doAssert* to work, please let me know. I'll mess with it another day since I need to do this same thing for work.
Selenium.prototype.doAssertLocationPart = function(partName,assertTo) {
var uri = selenium.browserbot.getCurrentWindow().document.location.href;
//alert("URI = " + uri);
var partValue = parseUri(uri,partName);
//alert("Part '" + partName + "' = " + partValue);
//this.doAssertValue(partValue,assertTo);
this.doStore(partValue,"var_"+partName);
};
// Slightly modified function based on author's original:
// http://badassery.blogspot.com/2007/02/parseuri-split-urls-in-javascript.html
//
// parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com)
// Splits any well-formed URI into the following parts (all are optional):
//
// - source (since the exec() method returns backreference 0 [i.e., the entire match] as key 0, we might as well use it)
// - protocol (scheme)
// - authority (includes both the domain and port)
// - domain (part of the authority; can be an IP address)
// - port (part of the authority)
// - path (includes both the directory path and filename)
// - directoryPath (part of the path; supports directories with periods, and without a trailing backslash)
// - fileName (part of the path)
// - query (does not include the leading question mark)
// - anchor (fragment)
//
function parseUri(sourceUri,partName){
var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
var uri = {};
for(var i = 0; i < 10; i++){
uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
if (uriPartNames[i] == partName) {
return uri[uriPartNames[i]]; // line added by MacGyver
}
}
// Always end directoryPath with a trailing backslash if a path was present in the source URI
// Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
if(uri.directoryPath.length > 0){
uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
if (partName == "directoryPath") {
return uri.directoryPath; // line added by MacGyver
}
}
return uri;
}
Then add this to your web.config file and make sure customErrors is turned off. Since you have a 500 error, it will redirect the user to the default page. Feel free to add a custom page for a 500 HTTP status code if you want to be specific in your Selenium scripts.
<customErrors mode="On" defaultRedirect="/ErrorHandler.aspx">
<error statusCode="401" redirect="/AccessDenied.aspx" />
<error statusCode="403" redirect="/AccessDenied.aspx" />
<error statusCode="404" redirect="/PageNotFound.aspx" />
</customErrors>
This is what your commands will look like in the IDE:
Make sure you're on this page (or something similar) before running the script:
https://localhost/ErrorHandler.aspx?aspxerrorpath=/path/pathyouweretryingtoviewinwebapp.aspx
Log shows that it passed!
[info] Executing: |storeLocation | var_URI | |
[info] Executing: |echo | ${var_URI} | |
[info] echo: https://localhost/ErrorHandler.aspx?aspxerrorpath=//path/pathyouweretryingtoviewinwebapp.aspx
[info] Executing: |assertLocationPart | fileName | ErrorHandler.aspx |
[info] Executing: |assertExpression | ${var_fileName} | ErrorHandler.aspx |
Using the error handler from my previous answer:
Command: assertLocation
Target: regexp:^(https://localhost/ErrorHandler.aspx).*$
Or (per your comment) it's inverse if you don't have error handling turned on, use AssertNotLocation. This may require more work on the person writing the scripts. You'd have to keep track of all pages.
More on pattern matching:
http://seleniumhq.org/docs/02_selenium_ide.html#matching-text-patterns
http://www.codediesel.com/testing/selenium-ide-pattern-matching/