Wiremock request.query not working with a paramter that contains dots - spring-boot

I'm using sprint-boot with wiremock-jre8, what i'm trying to to is to get a value from URL params, but this value contains dots "product.productCharacteristic.value"
{
"request": {
"urlPath": "/exampleOfPath",
"method": "GET",
"queryParameters": {
"product.productCharacteristic.name": {
"equalTo": "MSISDN"
},
"product.productCharacteristic.value": {
"matches": ".*"
}
}
},
"response": {
"status": 200,
"jsonBody": {
"key": "the value should be here {{request.query['product.productCharacteristic.value']}}"
},
"transformers": [
"response-template"
],
"headers": {
"Content-Type": "application/json"
}
}
}
i have tested all of those
{{request.query.['product.productCharacteristic.value']}}
{{request.query['product.productCharacteristic.value']}}
{{request.query['product%2EproductCharacteristic%2Evalue']}}
{{request.query.['product%2EproductCharacteristic%2Evalue']}}
{{request.query.product%2EproductCharacteristic%2Evalue}}
{{request.query.product.productCharacteristic.value}}
{{lookup request.query 'product.productCharacteristic.value'}}
{{lookup request.query 'product%2EproductCharacteristic%2Evalue'}}

You can use this format:
{{request.query.[product.productCharacteristic.value]}}
See WireMock - Response Templating - The request model:
request.headers.[<key>] - Header with awkward characters e.g. request.headers.[$?blah]

Related

How to execute a bulk ElasticSearch operation from an AppSync resolver?

I'm trying to execute a bulk operation over an ElasticSearch index from a VTL AppSync resolver.
Specifically this mutation:
input CreateTopicsInput {
language: String!
texts: [String!]!
}
type Mutation {
createTopics(input: CreateTopicsInput!): [Topic!]
}
And I created the following resolver:
Mutation.createTopics.req.vtl
#set( $body = "" )
#set( $q = '"' )
#foreach( $text in $ctx.args.input.texts )
#set( $body = $body.concat("{ ${q}index${q}: { ${q}_id${q}: ${q}${text}${q} } }
") )
#set( $body = $body.concat("{ ${q}text${q}: ${q}$text${q}, ${q}suggest${q}: { ${q}input${q}: ${q}$text${q} } }
") )
#end
{
"version": "2017-02-28",
"operation": "POST",
"path": "/topic-${ctx.args.input.language}/doc/_bulk",
"params": {
"headers" : [ { "Content-Type" : "application/x-ndjson" } ],
"body": "$body"
}
}
And when executed for example with this data:
mutation CreateTopic {
createTopics(input: {
language: "en",
texts: [ "COVID", "Election" ]}) {
text
}
}
Seems to output a correct invocation:
{
"version": "2017-02-28",
"operation": "POST",
"path": "/topic-en/doc/_bulk",
"params": {
"headers" : [ { "Content-Type" : "application/x-ndjson" } ],
"body": "{ "index": { "_id": "COVID" }
{ "text": "COVID" }
{ "index": { "_id": "Election" }
{ "text": "Election" }
"
}
}
but it doesn't work. Specifically it throws:
{
"data": {
"createTopics": null
},
"errors": [
{
"path": [
"createTopics"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Unexpected character ('i' (code 105)): was expecting comma to separate Object entries\n at [Source: (String)\"{\n \"version\": \"2017-02-28\",\n \"operation\": \"POST\",\n \"path\": \"/topic-en/doc/_bulk\",\n \"params\": {\n \"headers\" : { \"Content-Type\" : \"application/x-ndjson\" },\n \"body\": \"{ \"index\": { \"_id\": \"CODID\" } }\n{ \"text\": \"CODID\", \"suggest\": { \"input\": \"CODID\" } }\n{ \"index\": { \"_id\": \"Election\" } }\n{ \"text\": \"Election\", \"suggest\": { \"input\": \"Election\" } }\n\"\n }\n}\n\"; line: 7, column: 18]"
}
]
}
Some idea?
i played a little bit around and figure out that you would need to
quote your double quotes
write all in one line, divided by a "\n"
the last part in that line needs to be an "\n" again
As example:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/_msearch/template",
"params": {
"headers": {
"Content-Type": "application/x-ndjson"
},
"body": "{ \"index\": { \"_id": \"COVID\" }\n{ \"text\": \"COVID\" }\n"
}
}
Of course, this is not very convinced and for your use case this could work better:
#set($ndjson = [])
$util.qr($ndjson.add({ "index": "COVID" }))
$util.qr($ndjson.add({ "text": "COVID", "Param2": "vaccine" } }))
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/_msearch/template",
"params": {
"headers": {
"Content-Type": "application/x-ndjson"
},
"body": "#foreach ($line in $ndjson)$util.escapeJavaScript($util.toJson($line))\n#end"
}
}
The (maybe) problematic part could be the JavaScript Escaping. Not sure how your data looks like but it could result into some more quoted data as needed.
P.s: i switched my implementation to the last mentioned example and it works fine for the multi search.

How to select scenario when using AutoConfigureWireMock in a spring boot application

Using the following example of AutoConfigureWireMock, how can I select the scenario I want to use in my test?
Assuming that scenario1 is used in another test and cannot be changed
#AutoConfigureWireMock(port = 0, stubs = "classpath:/stubs")
#SpringBootTest
public class ExampleTest {
#Test
public void test(){
//set scenario scenario2
//assert get is 404
}
}
{
"request": {
"scenarioName": "scenario1",
"urlPattern": "/items",
"method": "GET"
},
"response": {
"status": 200
}
}
{
"request": {
"scenarioName": "scenario2",
"urlPattern": "/items",
"method": "GET"
},
"response": {
"status": 404
}
}
When a WireMock server starts, all Scenarios are in a state of "Started" You'll need to specify that as the starting state for the scenario you want first.
{
"request": {
"scenarioName": "My Scenario",
"requiredScenarioState": "Started",
"newScenarioState": "State 2",
"urlPattern": "/items",
"method": "GET"
},
"response": {
"status": 200
}
}
{
"request": {
"scenarioName": "My Scenario",
"requiredScenarioState": "State 2",
"newScenarioState": "Started",
"urlPattern": "/items",
"method": "GET"
},
"response": {
"status": 404
}
}
In the above, the first response will be returned, changing the scenario state. The next time you hit that endpoint, the second response will be returned, changing the scenario state again. (Changing the scenario state is optional -- you can leave a scenario in "Started" or a custom scenario state after a mapping is served up.)
If your requests were slightly different, you could instead differentiate them through the use of priority:
{
"priority": 1,
"request": {
"urlPattern": "/items",
"method": "GET",
"queryParameters": {
"myParam": {
"equals": "200"
}
}
},
"response": {
"status": 200
}
}
{
"priority": 2,
"request": {
"urlPattern": "/items",
"method": "GET",
},
"response": {
"status": 404
}
}
Finally, if none of those solutions work for you, you can use the admin endpoints to modify the mappings once you have started WireMock.

Location response header returned from web-api to Swagger-ui

I am returning a Uri as the location header of the response from my web-api controller, as shown below:
[HttpPost]
public HttpResponseMessage PostTenant([FromBody] JObject value)
{
string tenantName;
try
{
tenantName = value.GetValue("name").ToString();
}
catch (Exception e)
{
throw new HttpResponseException(
this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
}
try
{
DbInteract.InsertTenant(tenantName.Replace(" ", string.Empty));
var uri = Url.Link("TenantHttpRoute2", new { tenantName });
var response = new HttpResponseMessage(HttpStatusCode.Created);
response.Headers.Location = new Uri(uri);
return response;
}
catch...
}
The swagger-ui client makes the POST request, but the response headers don't contain the Location uri. It appears like this:
POST request from Swagger-ui
As you can see in the image, the RESPONSE HEADERS are
{
"content-type": null
}
The swagger JSON for this post request is:
"post": {
"tags": [
"Tenant"
],
"summary": "Add a new tenant",
"description": "Adds a tenant and returns admin user account information to be used in further calls.",
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "Tenant",
"description": "Name of the tenant must be alphanumeric without any special characters.",
"required": true,
"schema": {
"$ref": "#/definitions/CreateTenant"
}
}
],
"responses": {
"201": {
"description": "Tenant Inserted",
"headers": [
{
"description": "Location",
"type": "string"
}
]
},
"400": {
"description": "Invalid JSON Format of input"
},
"405": {
"description": "Tenant by this name already exists; Use PUT instead"
},
"500": {
"description": "InternalServerError"
}
}
}
What's wrong here? Why can't I see the response header in swagger-ui? Please let me know if you need more information. Thanks
The intent seems clear enough: a successful response returns a 201 Created with a Location header pointing at the newly created resource. Should work ...
I might remove the "produces" attribute, since you haven't defined any response anywhere.
Try with:
"headers" : { "Location" : { "description": "...", "type" : "string" } }
Instead of:
"headers" : []
so object instead of arrays.

Can't get merge tags to work - Mandrill/Parse

Here's my Parse Cloud Code call:
Mandrill.sendTemplate({
"template_name": "start-conversation",
"template_content": [{
"name": "example name",
"content": "example content" //Those are required but they are ignored
}],
"message": {
"to": [{
"email": request.params.toUserEmail,
"name": request.params.toUserName
}],
"important": true,
"merge": true,
"global_merge_vars": [
{
"rcpt": request.params.toUserEmail,
"vars": [
{
"name": "TOUSERNAME",
"content": request.params.toUserName
},
{
"name": "FROMUSERNAME",
"content": request.params.fromUserName
},
{
"name": "TOPICNAME",
"content": request.params.topicName
},
{
"name": "LANGUAGE",
"content": request.params.language
}
]
}
],
},
"async": true
},{
success: function(httpResponse) {
console.log(httpResponse);
response.success("mandrillStartConvoRequest -- success -- Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("mandrillStartConvoRequest -- error -- Uh oh, something went wrong");
}
});
Here's the <span> with the tags in my Mandrill Template:
<span style="line-height:20.7999992370605px">
*|TOUSERNAME|*
<br><br>
*|FROMUSERNAME|* would like to start a conversation with you about *|TOPICNAME|* in *|LANGUAGE|* </span>
The e-mail sends fine but no merge =(:
as far as I know the built-in Mandrill.sendTemplate method does not work.
so you should try to call mandrill API yourself, just do a HTTP POST
https://parse.com/questions/send-mandrill-template-email-from-cloud-code-example-code-required
UPDATE:
what I am using in my project is something like this, notice that I am using merge_vars but you are using global_merge_vars
var params = {
key: "xxxxxxxxxxxx",
template_name: "$template_name",
template_content: [],
message: {
to: [
{
email: email
}
],
merge_vars : [{
rcpt: email,
vars:[
{
"name" : "from",
"content" : "Test"
}
]
}]
},
async: true
};
Parse.Cloud.httpRequest({
method: "POST",
headers: {
"Content-Type": "application/json",
},
url: "https://mandrillapp.com/api/1.0/messages/send-template.json",
body: params,
success: function(httpResponse) {
response.success("email sent");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});

AJAX call to REST service doesn't display results in page but a call to the same response in a flat file does

I'm trying to make a call to WCF Data Services and display the results in a GridPanel.
The call works and returns the correct JSON except the GridPanel doesn't display any results.
I tried copying the returned json into a file also on the webserver and replacing the destination url for that. This worked correctly and displays the results.
So as far as I can tell the JSON, the code and the service are all correct but they don't work together properly.
The Ext JS
Ext.define('Customer', {
extend: 'Ext.data.Model',
fields: ['Id', 'CustomerName'],
proxy: {
headers: {
'Accept' : 'application/json'
},
type: 'rest',
url: 'Service.svc/Customers',
reader: {
type: 'json',
root: 'd'
}
}
});
The JSON back from the service
{
"d" : [
{
"__metadata": {
"uri": "http://localhost:52332/testservice.svc/Customers(1)",
"type": "PierbridgeShinePlatformTestModel.Customer"
},
"Id": 1,
"CustomerName": "fred",
"Invoices": {
"__deferred": {
"uri": "http://localhost:52332/testservice.svc/Customers(1)/Invoices"
}
}
},
{
"__metadata": {
"uri": "http://localhost:52332/testservice.svc/Customers(2)",
"type": "PierbridgeShinePlatformTestModel.Customer"
},
"Id": 2,
"CustomerName": "Mr Fred",
"Invoices": {
"__deferred": {
"uri": "http://localhost:52332/testservice.svc/Customers(2)/Invoices"
}
}
}
]
}

Resources