How does the "replace" handlebars helper work? - replace

Faceted search is giving me problems when the facet name includes special characters - specifically /, (, and ). I am trying to replace special characters using the handlebars helpers built into Stencil. I keep getting a 500 error any time I use the replace handlebar helper.
npm's documentation example:
{{replace "Liquid Snake" "Liquid" "Solid"}}
Even using that exact bit of code (simple Strings, not variables), I get the 500 error.
Here's the log:
Debug: internal, implementation, error
TypeError: Uncaught error: options.inverse is not a function
at Object.<anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/helpers/replace.js:19:28)
at Object.template.1 (eval at <anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/index.js:71:44), <anonymous>:11:72)
at Object.prog [as fn] (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/handlebars/dist/cjs/handlebars/runtime.js:193:15)
at Object.<anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/helpers/if.js:85:28)
at Object.template.main (eval at <anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/index.js:71:44), <anonymous>:70:35)
at Object.ret [as components/faceted-search/facets/multi] (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/handlebars/dist/cjs/handlebars/runtime.js:159:30)
at Object.<anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/helpers/dynamicComponent.js:32:50)
at Object.template.7 (eval at <anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/index.js:71:44), <anonymous>:33:109)
at Object.prog [as fn] (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/handlebars/dist/cjs/handlebars/runtime.js:193:15)
at Object.<anonymous> (/Users/theuser/.nvm/versions/node/v4.4.0/lib/node_modules/#bigcommerce/stencil-cli/node_modules/#bigcommerce/stencil-paper/helpers/if.js:85:28)

I think you are looking at the documentation for the wrong replace helper. The helper library you are using is from BigCommerce and its replace helper is a Handlebars Block Helper. The way to use it would be:
{{#replace "Liquid" "Liquid Snake"}}Solid{{/replace}}
Edit
Thanks for pointing out that I had incorrectly wrapped the replacement string in my example in mustache brackets. I have replaced {{"Solid"}} with Solid in my original code block, so the example is now correct.
As to your follow-up about there being no output when the string to be replaced (the needle) is not found in the target string (the haystack): It turns out that this is the designed behavior of the helper. I have checked the source code, and I can confirm that it implements the following rules:
If the haystack contains the needle, return the haystack with all instances of the needle replaced.
If the haystack does not contain the needle, return the else branch of the helper.
This means that if we want to display any output, like the original haystack, when there are no matches of the needle within the haystack, then we must do so with an else branch in our template:
{{#replace "Liquid" "Liquid Snake"}}Solid{{else}}Liquid Snake{{/replace}}
Truthfully, this does seem like a quite awkward implementation of this helper.

Related

Wiremock withQueryParam not matching regex

I'm trying to do this:
stubFor(get("/my/path")
.withQueryParam("paramName", matching(format("^(%s)|(%s)|(%s)$", v1, v2, v3)))
.willReturn(okJson(RESPONSE)));
and I get this error:
GET | GET
/my/path | /my/path?paramName=v1Value <<<<< URL does not match
|
Query: paramName [matches] ^(v1Value)|(v2Value)|(v3Value)$ | paramName: v1Value
I have tested the regex and it works. I also debugged and I saw that the RegexPattern also matches. But for some reason, I still get this error. I believe I'm using it wrongly.
I tried a simpler version that also didn't work:
stubFor(get("/my/path")
.withQueryParam("paramName", equalTo("v1Value"))
.willReturn(okJson(RESPONSE)));
Any ideas? Thanks in advance.
WireMock documentation recommends matching on url path only and matching on query parameters separately.
That would look something like...
stubFor(get(urlPathEqualTo("/my/path"))
.withQueryParam("paramName", matching(format("^(%s)|(%s)|(%s)$", v1, v2, v3)))
.willReturn(okJson(RESPONSE)));
If I recall correctly, just using stubFor(get("my/url")) defaults to using urlEqualTo, which checks equality matching on path and query parameters

How to skip null and empty value while performing Regular/JSON Expression Extract?

From the below response, I need to retrieve all the values of backgroundImage field(which can be found in Debug Sampler) and by using the ForEach Controller I need to call the values fetched from backgroundImage field and pass it in the path of the next request.
I used Regular exp Extract with code "backgroundImage":"(.*?)" to extract all the non-values, but sometimes it returns empty values and in this case, it fetches just an empty "" (double code) and passes in the path of the URL which leads to an error.
How can I avoid null and empty values in this scenario?
Response:
{"data":[{"id":5031,"createdAt":1582657779000,"updatedAt":null,"contentType":"TEXT","author":{"id":32,"teamName":"Team Content","teamShirtNumber":null,"role":"TEAM_LEAD","firstName":"Team Lead Jeremiah","lastName":"Dominguez Gorrin","profilePictureReference":"aca511ec-c552-4bce-90ce-d993684c5854"},"text":"Teeeeest"},{"id":5030,"createdAt":1582657696000,"updatedAt":null,"contentType":"TEXT","author":{"id":32,"teamName":"Team Content","teamShirtNumber":null,"role":"TEAM_LEAD","firstName":"Team Lead Jeremiah","lastName":"Dominguez Gorrin","profilePictureReference":"aca511ec-c552-4bce-90ce-d993684c5854"},"text":"Sassafras"},{"id":5029,"createdAt":1582657466000,"updatedAt":null,"contentType":"TEXT","author":{"id":32,"teamName":"Team Content","teamShirtNumber":null,"role":"TEAM_LEAD","firstName":"Team Lead Jeremiah","lastName":"Dominguez Gorrin","profilePictureReference":"aca511ec-c552-4bce-90ce-d993684c5854"},"text":"Lkdsasdad"},{"id":5028,"createdAt":1582657243000,"updatedAt":null,"contentType":"POLL","author":{"id":32,"teamName":"Team Content","teamShirtNumber":null,"role":"TEAM_LEAD","firstName":"Team Lead Jeremiah","lastName":"Dominguez Gorrin","profilePictureReference":"aca511ec-c552-4bce-90ce-d993684c5854"},"text":"Umfrage mit Bild Preview Upload","minValue":0.0,"maxValue":10.0,"minLabel":null,"maxLabel":null,"sliderIcon":null,"backgroundImage":"f426549f-b1e0-4d07-8786-685fcfa28835","result":{"attendances":0,"averageValue":null,"myAnswer":null},"votingEndAt":1585090800000},{"id":5027,"createdAt":1582657195000,"updatedAt":null,"contentType":"POLL","author":{"id":32,"teamName":"Team Content","teamShirtNumber":null,"role":"TEAM_LEAD","firstName":"Team Lead Jeremiah","lastName":"Dominguez Gorrin","profilePictureReference":"aca511ec-c552-4bce-90ce-d993684c5854"}
Thanks in Advance for your knowledgeable help!
How about this:
"backgroundImage":"([^"]+)"
It repeats non double quotes character at least one.
You can go for a filter operator like:
$.data[?(#.backgroundImage != null && #.backgroundImage != "")].backgroundImage
Demo:
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

How to import Shakespearean Templates in Yesod?

I was using QuasiQuotations in Yesod, and everything worked fine. BUT my file became very large and not nice to look at. Also, my TextEditor does not highlight this syntax correctly. That is why is split my files like so:
getHomeR :: Handler Html
getHomeR = do
webSockets chatApp
defaultLayout $ do
$(luciusFile "templates/chat.lucius")
$(juliusFile "templates/chat.julius")
$(hamletFile "templates/chat.hamlet")
If this is wrong, please do tell. Doing runghc myFile.hs throws many errors like this:
chat_new.hs:115:9:
Couldn't match expected type ‘t0 -> Css’
with actual type ‘WidgetT App IO a0’
The lambda expression ‘\ _render_ajFK
-> (shakespeare-2.0.7:Text.Css.CssNoWhitespace . (foldr ($) ...))
...’
has one argument,
but its type ‘WidgetT App IO a0’ has none
In a stmt of a 'do' block:
\ _render_ajFK
...
And this.
chat_new.hs:116:9:
Couldn't match type ‘(url0 -> [(Text, Text)] -> Text)
-> Javascript’
with ‘WidgetT App IO a1’
Expected type: WidgetT App IO a1
Actual type: JavascriptUrl url0
Probable cause: ‘asJavascriptUrl’ is applied to too few arguments
...
And also one for the HTML (Hamlet).
Thus, one per template.
It seems that hamletFile and others treat templates as self-contained, while yours are referencing something from each other. You can play with order of *File calls, or use widgetFile* from Yesod.Default.Util module:
$(widgetFileNoReload def "chat")
The Reload variant is useful for development - it would make yesod devel to watch for file changes and reload them.

JMeter Response Assertion: incorporating property into pattern test

I'd like to setup a JMeter test plan to suggest whether a web site (URL) is Drupal-based (based completely on the HTTP response from the site) and compare it with existing data that I have on the environment. (I realize that using an HTTP approach, as opposed to say examining the site's file system, is "iffy" but I'm curious how useful the approach is)
The JMeter command line might look like this:
JMeter -t "DrupalAssertions.jmx" -Jurl=http://my.dot.com -Jdrupal=true
where I provide the URL to test and an additional property "drupal" indicating my best guess on whether the site is Drupal-based.
In my test plan, I add an HTTP Request to return the HTML content of the page for the URL. I'm then able to successfully add a Response Assertion that tests a pattern (say (?i)(drupal) for a sadly lacking pattern) to see if it's contained in the response.
That much works fine, or as expected, but what I'd like to do is to compare the value of the "drupal" property against the result of that pattern test in that same Response Assertion. I know I'm missing something simple here, but I'm not seeing how to do that.
I want to try to use an expression like this:
(?i)(drupal) == ${__P(drupal)}
in a pattern, but that doesn't work. The name of the Compare Assertion looks promising, but I don't see how to incorporate the property into a comparison.
Update: The approach suggested by PMD UBIK-INGENIERIE does work. I used a Regular Expression Extractor like this:
<RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="Extract Drupal in Response" enabled="true">
<stringProp name="RegexExtractor.useHeaders">false</stringProp>
<stringProp name="RegexExtractor.refname">drupalInResponse</stringProp>
<stringProp name="RegexExtractor.regex">(.*drupal.*)</stringProp>
<stringProp name="RegexExtractor.template">$0$</stringProp>
<stringProp name="RegexExtractor.default">__false__</stringProp>
<stringProp name="RegexExtractor.match_number">1</stringProp>
</RegexExtractor>
followed by this BeanShell Assertion:
// Variable "drupalInResponse" is "__false__" by default
if ( !(vars.get("drupalInResponse").equals("__false__") ) ) {
vars.put("drupalInResponse","true");
}
else {
vars.put("drupalInResponse","false");
}
print("\n\nThe value of property 'drupal' is: " + props.get("drupal") + "\n");
print("\n\nThe value of variable 'drupalInResponse' is: " + vars.get("drupalInResponse") + "\n");
if (vars.get("drupalInResponse").equals( props.get("drupal") ) ) {
print("Site Drupalness is consistent with your beliefs");
}
else {
print("You're wrong about the site's Drupalness");
Failure = true;
FailureMessage = "Incorrect Drupal assumption";
}
In the Regular Expression Extractor, I'd set a default value that I felt wouldn't be matched by my pattern of interest, then did an ugly verbose Java comparison with the "drupal" property in the BeanShell Assertion.
Wish somehow that the assertion could be made in a single component rather than it having two parts, but you can't argue with "working" :)
You can use a regexp extractir with your first pattern
Then use a Beanshell assertion which will use your variable and compare it to drupal property.

Error: Non-assignable model expression: undefined

I'm trying to write tests for a directive, so I'm constructing it by hand with $compile. (Yes, I know that I can use e2e tests for this, but in this case I want to test some DOM independent logic so I'm using the unit test approach.)
I have a field that I'm binding to with scope: {foo: '='}. When I try to set it on the directive's scope, I get the following error:
Error: Non-assignable model expression: undefined (directive: foo)
at Error (<anonymous>)
at $get.parentSet (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4146:25)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4160:23)
at Object.$get.Scope.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:7693:38)
at Object.$get.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:7894:24)
at SNAKE_CASE_REGEXP (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:930:13)
at Object.invoke (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:2788:25)
at bootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:928:12)
at angularInit (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:904:5)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:14397:5
Reproduced in this fiddle.
In your directive you have:
scope: {
bar: '='
}
This means it's going to look on the directive element (<div foo></div>) for an attribute name "bar" and do a two-way databinding within your directive and the object specified for the attribute. So, first you need to add a bar attribute, and then you need to have something set in your scope with the name of the value of the attribute. See http://jsfiddle.net/u4BTu/6/
Another possible cause of this error is neglecting to account for Angular's normalization of the attributes. For instance, if you have the following directive:
<my-directive parameter-one="foo" parameter_two="bar"></my-directive>
With this scope declaration:
scope: {
'parameter-one': '=',
parameter_two: '=',
}
You'll get this error. To correct, use the normalized versions for the scope declaration:
scope: {
parameterOne: '=',
parameterTwo: '=',
}