Spring Cloud Contract provider return same as request - spring-cloud

I'm working with two microservices using Spring Cloud Contract. One providing its contract, and the other one consuming it. In one scenario the provider response is the same that the request.
So the provider contract is like this:
Contract.make {
request {
method 'POST'
url '/provider/foo'
body(
"foo": $(regex("[a-zA-Z0-9]{20}"))
)
}
response {
status 200
body(
"fooResponse": fromRequest().body("\$.foo")
)
}
And the generated wiremock mapping:
{
"id" : "a80c0871-f4c0-49e3-8cc1-94de39899669",
"request" : {
"url" : "/provider/foo",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(#.['foo'] =~ /[a-zA-Z0-9]{20}/)]"
} ]
},
"response" : {
"status" : 200,
"body" : "{\"fooResponse\":\"{{{jsonpath this '$.foo'}}}\"}",
"transformers" : [ "response-template" ]
},
"uuid" : "a80c0871-f4c0-49e3-8cc1-94de39899669",
"scenarioName" : "scenarioReturnSameAsRequest",
"requiredScenarioState" : "Started"
}
But when my code calls to the provider, with foo as any text, the wiremock returns:
{
"fooResponse" : "{{{jsonpath this '$.foo'}}}"
}
How can I build a contract that responses the same parameters as the request body?
Edit
I tried with a fixed value on the response and works fine:
Contract.make {
request {
method 'POST'
url '/provider/foo'
body(
"foo": $(regex("[a-zA-Z0-9]{20}"))
)
}
response {
status 200
body(
"fooResponse": "fooValue"
)
}
Now wiremock return:
{
"fooResponse" : "fooValue"
}
Maybe is not supported getting from request a regex value?

I think the mapping should contain request.body instead of this. Also I wonder if you need to use 3 times a { or just 2 times. Or do you need to escape these?
Possible mapping:
"response" : {
"status" : 200,
"body" : "{\"fooResponse\":\"{{jsonpath request.body '$.foo'}}\"}",
"transformers" : [ "response-template" ]
},
See also the chapter JSONPath helper on http://wiremock.org/docs/response-templating

I had the same problem once. You can try to use value() like this:
"fooResponse": value(fromRequest().body('$.foo'))

Related

Sendgrid singlesend error with api : json could not be unmarshalled

I am trying to trigger a singlesend using sendgrid api but getting json could not be unmarshalled error.
Here is what I am trying to send in my payload
{
"name" : "this is a test api 2",
"send_at": "now",
"email_config": {
"sender_id": 4810***,
"design_id": "06daea5d-9470-4cb5-8dc6-******"
},
"send_to": {
"list_ids": [
"8a97936a-8cf8-4e60-bfd3-******"
],
"segment_ids": [],
"all" : false
}
}
I am following this doc : https://docs.sendgrid.com/api-reference/single-sends/create-single-send

How we can ignore the order of query parameter in WireMock

Suppose I have endpoint:
/url?number="321"&name="TEST" but when we are matching a request matching how we want to ignore the order of query parameter(ignore order of query param)
In this case if pass query param in query param tag then I will not distinguish between request, because same endpoint but multiple query param.
In the documentation the section on Regular Expression shows the example for matching on Query Parameters. It is even possible to include the absence of a parameter.
Using your example it might look like:
{
"request" : {
"urlPath" : "/url",
"method" : "GET",
"queryParameters" : {
"number" : {
"equalTo" : "321"
},
"name" : {
"equalTo" : "TEST"
}
}
},
"response" : {
"status" : 200
}
}

JFrog Artifactory API query for object properties does not return requested detail

I am requesting label properties for docker artifact, perhaps the url is not correct? I get response object (json) but label properties are not included. Code example:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json;docker.label.com.company.info.build='*'",
{'Authorization' => 'Bearer <REDACTED>'})
if response.code.to_s == "200"
puts ("Artifactory response "+ response.body)
puts ("response object: "+response.inspect())
else
puts ("Artifactory request returned "+response.code.to_s)
end
Connecting to artifactory
Artifactory response {
"repo" : "dockerv2-local",
"path" : "/anonymizer/functional/manifest.json",
"created" : "2018-03-14T14:52:22.681-07:00",
"createdBy" : "build",
"lastModified" : "2018-03-15T15:52:34.225-07:00",
"modifiedBy" : "build",
"lastUpdated" : "2018-03-15T15:52:34.225-07:00",
"downloadUri" : "http://myrepo:8081/artifactory/dockerv2-local/anonymizer/functional/manifest.json",
"mimeType" : "application/json",
"size" : "1580",
"checksums" : {
"sha1" : "bf2a1f85c7ab8cec14b64d172b7fdaf420804fcb",
"md5" : "9c1bbfc77e2f44d96255f7c1f99d2e8d",
"sha256" : "53e56b21197c57d8ea9838df7cffb3d8f33cd714998d620efd8a34ba5a7e33c0"
},
"originalChecksums" : {
"sha256" : "53e56b21197c57d8ea9838df7cffb3d8f33cd714998d620efd8a34ba5a7e33c0"
},
"uri" : "http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json"
}
response object: #<Net::HTTPOK 200 OK readbody=true>
If I understand you correctly, you want to get the properties of the manifest.json file, "docker.label.com.company.info.build" in particular.
From looking at your command:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json;docker.label.com.company.info.build='*'",
It seems that you are using a semicolon to get the properties, which is not the right way. As you can see in this REST API, in order to use the get properties you should use the ampersand sign, so your command should look like:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json&docker.label.com.company.info.build='*'",

Guzzle 6, put request and description json

I use guzzle 6 with a json file to describe my methods to call.
Bellow, an example with a put request :
in the json descriptor file :
"putObjects" : {
"httpMethod": "PUT",
"uri": "objects",
"summary": "Send objects to the api",
"parameters": {
"objects" : {
"type" : "string",
"location" : "body"
}
}
}
in the symfony controller :
$clientResponse = $client->execute(
$client->getCommand("putObjects", array(
'objects' => $request->getContent()
))
);
Before, with guzzle 3 when the put request was sent, the data sent was formated like this (a valid json) :
{objects: [{....}]}
But now, with guzzle 6, the data is formated as :
objects = {objects: [{....}]}
And of course my api send me an error 'Invalid json message received'.
Someone has an idea about this issue ?
I have found the solution.
In symfony controller :
$content = json_decode($request->getContent(), true);
$clientResponse = $client->execute(
$client->getCommand("putObjects", array(
'objects' => $content['objects']
))
);
In the json descriptor file :
"putObjects" : {
"httpMethod": "PUT",
"uri": "objects",
"summary": "Send objects moderated",
"parameters": {
"objects" : {
"type" : "array",
"location" : "json"
},
}
}

Get Ember Data working with array of objects

I have a simple Ember Data app to list and show various objects.
My /servers.json API (for example) return this kind of format:
[
{
"hosted_domain" : "example.com",
"status" : 1,
"name" : "srv0443",
"id" : 443
},
{
"id" : 392,
"status" : 1,
"name" : "srv0392",
"hosted_domain" : "example.com"
},
{
"hosted_domain" : "example.com",
"id" : 419,
"name" : "srv0419",
"status" : 1
}
]
But I got the following error:
Assertion Failed: The response from a findAll must be an Array, not undefined
Ember Data expects this kind of format:
{
"servers" : [
{
"name" : "srv0443",
"status" : 1,
"id" : 443,
"hosted_domain" : "example.com"
},
{
"status" : 1,
"name" : "srv0392",
"id" : 392,
"hosted_domain" : "example.com"
},
{
"status" : 1,
"name" : "srv0419",
"hosted_domain" : "example.com",
"id" : 419
},
]
}
I know I can override the payload with the extractArray of the RESTSerializer.
It's works by doing payload = { servers: payload } but how get it working in a generic way?
How can I do to catch the needed key of an model type?
In a more general way, what is the good REST format, by convention?
Thanks.
Ember Data works by having the data follow a certain convention ({servers: payload}). So the data either needs to conform, or you have to extend the serializer as you mentioned (or some other customization like overriding the model's findAll() method). There isn't anyway around it, if you want to use Ember Data. Of course, you don't have to use Ember Data. Here is a good article about not using it: http://eviltrout.com/2013/03/23/ember-without-data.html
To customize the serializer you can extend it like this:
App.ServerSerializer = DS.RESTSerializer.extend({
extractArray: function(store, type, payload) {
this._super(store, type, {servers: payload});
},
});
Extract array is automatically called by ember after it gets a response from the server. This will put in the format ember data expects, then pass it on to continue processing as usual. But you will have to do that for each type of model. If you override App.ApplicationSerializer instead you might be able to use the type paramter to figure out which key should go in the modified payload, so it will work for any model, but I can't check it right now.
Finally found a solution by using primaryType.typeKey and ember-inflector tool on the RESTSerializer:
App.ApplicationSerializer = DS.RESTSerializer.extend
extractArray: (store, primaryType, payload) ->
# Payload reload with { type.pluralize: hash }
payloadKey = Ember.Inflector.inflector.pluralize primaryType.typeKey
payloadReloaded = []
payloadReloaded[payloadKey] = payload
#_super store, primaryType, payloadReloaded
In a nutshell:
Get the type key (e.g. server)
Pluralize it (e.g. servers)
Add it as payload master key (e.g. { servers: payload }
And that's it!
Please feel free to comment this solution if you have a better proposition.