diff --git a/multi/multi__contract_dsl.html b/multi/multi__contract_dsl.html index 202c961199..0078b58a40 100644 --- a/multi/multi__contract_dsl.html +++ b/multi/multi__contract_dsl.html @@ -556,7 +556,7 @@ It would more or less like this:

// then:
  assertThat(response.statusCode()).isEqualTo(200);

7.5.5 Referencing request from response

The best situation is to provide fixed values but sometimes you need to reference a request in your response. In order to do this you can profit from the fromRequest() method that allows you to reference a bunch -of elements from the HTTP request. You can use the following options:

Let’s take a look at the following contract

Contract contractDsl = Contract.make {
+of elements from the HTTP request. You can use the following options:

  • fromRequest().url() - return the request URL and query parameters
  • fromRequest().query(String key) - return the first query parameter with a given name
  • fromRequest().query(String key, int index) - return the nth query parameter with a given name
  • fromRequest().path() - return the full path
  • fromRequest().path(int index) - return the nth path element
  • fromRequest().header(String key) - return the first header with a given name
  • fromRequest().header(String key, int index) - return the nth header with a given name
  • fromRequest().body() - return the full request body
  • fromRequest().body(String jsonPath) - return the element from the request that matches the JSON Path

Let’s take a look at the following contract

Contract contractDsl = Contract.make {
 	request {
 		method 'GET'
 		url('/api/v1/xxxx') {
@@ -607,15 +607,17 @@ of elements from the HTTP request. You can use the following options:

"Authorization")).isEqualTo("foo secret bar"); // and: DocumentContext parsedJson = JsonPath.parse(response.getBody().asString()); - assertThatJson(parsedJson).field("url").isEqualTo("/api/v1/xxxx"); - assertThatJson(parsedJson).field("fullBody").isEqualTo("{\"foo\":\"bar\",\"baz\":5}"); - assertThatJson(parsedJson).field("paramIndex").isEqualTo("bar2"); - assertThatJson(parsedJson).field("responseFoo").isEqualTo("bar"); - assertThatJson(parsedJson).field("authorization2").isEqualTo("secret2"); - assertThatJson(parsedJson).field("responseBaz").isEqualTo(5); - assertThatJson(parsedJson).field("responseBaz2").isEqualTo("Bla bla bar bla bla"); - assertThatJson(parsedJson).field("param").isEqualTo("bar"); - assertThatJson(parsedJson).field("authorization").isEqualTo("secret");

As you can see elements from the request have been properly referenced in the response.

The generated WireMock stub will look more or less like this:

{
+ assertThatJson(parsedJson).field("['fullBody']").isEqualTo("{\"foo\":\"bar\",\"baz\":5}");
+ assertThatJson(parsedJson).field("['authorization']").isEqualTo("secret");
+ assertThatJson(parsedJson).field("['authorization2']").isEqualTo("secret2");
+ assertThatJson(parsedJson).field("['path']").isEqualTo("/api/v1/xxxx");
+ assertThatJson(parsedJson).field("['param']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['paramIndex']").isEqualTo("bar2");
+ assertThatJson(parsedJson).field("['pathIndex']").isEqualTo("v1");
+ assertThatJson(parsedJson).field("['responseBaz']").isEqualTo(5);
+ assertThatJson(parsedJson).field("['responseFoo']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['url']").isEqualTo("/api/v1/xxxx?foo=bar&foo=bar2");
+ assertThatJson(parsedJson).field("['responseBaz2']").isEqualTo("Bla bla bar bla bla");

As you can see elements from the request have been properly referenced in the response.

The generated WireMock stub will look more or less like this:

{
   "request" : {
     "urlPath" : "/api/v1/xxxx",
     "method" : "POST",
@@ -630,22 +632,24 @@ of elements from the HTTP request. You can use the following options:

} }, "bodyPatterns" : [ { - "matchesJsonPath" : "$[?(@.baz == 5)]" + "matchesJsonPath" : "$[?(@.['baz'] == 5)]" }, { - "matchesJsonPath" : "$[?(@.foo == 'bar')]" + "matchesJsonPath" : "$[?(@.['foo'] == 'bar')]" } ] }, "response" : { "status" : 200, - "body" : "{\"url\":\"{{{request.url}}}\",\"param\":\"{{{request.query.foo.[0]}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\",\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\"}", + "body" : "{\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"path\":\"{{{request.path}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"param\":\"{{{request.query.foo.[0]}}}\",\"pathIndex\":\"{{{request.path.[1]}}}\",\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"url\":\"{{{request.url}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\"}", "headers" : { - "Authorization" : "{{{request.headers.Authorization.[0]}}}" + "Authorization" : "{{{request.headers.Authorization.[0]}}};foo" }, "transformers" : [ "response-template" ] } }

So sending a request as the one presented in the request part of the contract will lead in sending the following response body

{
   "url" : "/api/v1/xxxx?foo=bar&foo=bar2",
+  "path" : "/api/v1/xxxx",
+  "pathIndex" : "v1",
   "param" : "bar",
   "paramIndex" : "bar2",
   "authorization" : "secret",
diff --git a/single/spring-cloud-contract.html b/single/spring-cloud-contract.html
index 59dde71db2..46e433d0aa 100644
--- a/single/spring-cloud-contract.html
+++ b/single/spring-cloud-contract.html
@@ -2618,7 +2618,7 @@ It would more or less like this:

// then:
  assertThat(response.statusCode()).isEqualTo(200);

7.5.5 Referencing request from response

The best situation is to provide fixed values but sometimes you need to reference a request in your response. In order to do this you can profit from the fromRequest() method that allows you to reference a bunch -of elements from the HTTP request. You can use the following options:

Let’s take a look at the following contract

Contract contractDsl = Contract.make {
+of elements from the HTTP request. You can use the following options:

  • fromRequest().url() - return the request URL and query parameters
  • fromRequest().query(String key) - return the first query parameter with a given name
  • fromRequest().query(String key, int index) - return the nth query parameter with a given name
  • fromRequest().path() - return the full path
  • fromRequest().path(int index) - return the nth path element
  • fromRequest().header(String key) - return the first header with a given name
  • fromRequest().header(String key, int index) - return the nth header with a given name
  • fromRequest().body() - return the full request body
  • fromRequest().body(String jsonPath) - return the element from the request that matches the JSON Path

Let’s take a look at the following contract

Contract contractDsl = Contract.make {
 	request {
 		method 'GET'
 		url('/api/v1/xxxx') {
@@ -2669,15 +2669,17 @@ of elements from the HTTP request. You can use the following options:

"Authorization")).isEqualTo("foo secret bar"); // and: DocumentContext parsedJson = JsonPath.parse(response.getBody().asString()); - assertThatJson(parsedJson).field("url").isEqualTo("/api/v1/xxxx"); - assertThatJson(parsedJson).field("fullBody").isEqualTo("{\"foo\":\"bar\",\"baz\":5}"); - assertThatJson(parsedJson).field("paramIndex").isEqualTo("bar2"); - assertThatJson(parsedJson).field("responseFoo").isEqualTo("bar"); - assertThatJson(parsedJson).field("authorization2").isEqualTo("secret2"); - assertThatJson(parsedJson).field("responseBaz").isEqualTo(5); - assertThatJson(parsedJson).field("responseBaz2").isEqualTo("Bla bla bar bla bla"); - assertThatJson(parsedJson).field("param").isEqualTo("bar"); - assertThatJson(parsedJson).field("authorization").isEqualTo("secret");

As you can see elements from the request have been properly referenced in the response.

The generated WireMock stub will look more or less like this:

{
+ assertThatJson(parsedJson).field("['fullBody']").isEqualTo("{\"foo\":\"bar\",\"baz\":5}");
+ assertThatJson(parsedJson).field("['authorization']").isEqualTo("secret");
+ assertThatJson(parsedJson).field("['authorization2']").isEqualTo("secret2");
+ assertThatJson(parsedJson).field("['path']").isEqualTo("/api/v1/xxxx");
+ assertThatJson(parsedJson).field("['param']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['paramIndex']").isEqualTo("bar2");
+ assertThatJson(parsedJson).field("['pathIndex']").isEqualTo("v1");
+ assertThatJson(parsedJson).field("['responseBaz']").isEqualTo(5);
+ assertThatJson(parsedJson).field("['responseFoo']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['url']").isEqualTo("/api/v1/xxxx?foo=bar&foo=bar2");
+ assertThatJson(parsedJson).field("['responseBaz2']").isEqualTo("Bla bla bar bla bla");

As you can see elements from the request have been properly referenced in the response.

The generated WireMock stub will look more or less like this:

{
   "request" : {
     "urlPath" : "/api/v1/xxxx",
     "method" : "POST",
@@ -2692,22 +2694,24 @@ of elements from the HTTP request. You can use the following options:

} }, "bodyPatterns" : [ { - "matchesJsonPath" : "$[?(@.baz == 5)]" + "matchesJsonPath" : "$[?(@.['baz'] == 5)]" }, { - "matchesJsonPath" : "$[?(@.foo == 'bar')]" + "matchesJsonPath" : "$[?(@.['foo'] == 'bar')]" } ] }, "response" : { "status" : 200, - "body" : "{\"url\":\"{{{request.url}}}\",\"param\":\"{{{request.query.foo.[0]}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\",\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\"}", + "body" : "{\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"path\":\"{{{request.path}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"param\":\"{{{request.query.foo.[0]}}}\",\"pathIndex\":\"{{{request.path.[1]}}}\",\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"url\":\"{{{request.url}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\"}", "headers" : { - "Authorization" : "{{{request.headers.Authorization.[0]}}}" + "Authorization" : "{{{request.headers.Authorization.[0]}}};foo" }, "transformers" : [ "response-template" ] } }

So sending a request as the one presented in the request part of the contract will lead in sending the following response body

{
   "url" : "/api/v1/xxxx?foo=bar&foo=bar2",
+  "path" : "/api/v1/xxxx",
+  "pathIndex" : "v1",
   "param" : "bar",
   "paramIndex" : "bar2",
   "authorization" : "secret",
diff --git a/spring-cloud-contract.xml b/spring-cloud-contract.xml
index 30fc4ee10c..66639f7831 100644
--- a/spring-cloud-contract.xml
+++ b/spring-cloud-contract.xml
@@ -4311,7 +4311,7 @@ In order to do this you can profit from the fromRequest() met
 of elements from the HTTP request. You can use the following options:
 
 
-fromRequest().url() - return the request URL
+fromRequest().url() - return the request URL and query parameters
 
 
 fromRequest().query(String key) - return the first query parameter with a given name
@@ -4320,6 +4320,12 @@ of elements from the HTTP request. You can use the following options:
 fromRequest().query(String key, int index) - return the nth query parameter with a given name
 
 
+fromRequest().path() - return the full path
+
+
+fromRequest().path(int index) - return the nth path element
+
+
 fromRequest().header(String key) - return the first header with a given name
 
 
@@ -4386,15 +4392,17 @@ of elements from the HTTP request. You can use the following options:
  assertThat(response.header("Authorization")).isEqualTo("foo secret bar");
 // and:
  DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
- assertThatJson(parsedJson).field("url").isEqualTo("/api/v1/xxxx");
- assertThatJson(parsedJson).field("fullBody").isEqualTo("{\"foo\":\"bar\",\"baz\":5}");
- assertThatJson(parsedJson).field("paramIndex").isEqualTo("bar2");
- assertThatJson(parsedJson).field("responseFoo").isEqualTo("bar");
- assertThatJson(parsedJson).field("authorization2").isEqualTo("secret2");
- assertThatJson(parsedJson).field("responseBaz").isEqualTo(5);
- assertThatJson(parsedJson).field("responseBaz2").isEqualTo("Bla bla bar bla bla");
- assertThatJson(parsedJson).field("param").isEqualTo("bar");
- assertThatJson(parsedJson).field("authorization").isEqualTo("secret");
+ assertThatJson(parsedJson).field("['fullBody']").isEqualTo("{\"foo\":\"bar\",\"baz\":5}");
+ assertThatJson(parsedJson).field("['authorization']").isEqualTo("secret");
+ assertThatJson(parsedJson).field("['authorization2']").isEqualTo("secret2");
+ assertThatJson(parsedJson).field("['path']").isEqualTo("/api/v1/xxxx");
+ assertThatJson(parsedJson).field("['param']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['paramIndex']").isEqualTo("bar2");
+ assertThatJson(parsedJson).field("['pathIndex']").isEqualTo("v1");
+ assertThatJson(parsedJson).field("['responseBaz']").isEqualTo(5);
+ assertThatJson(parsedJson).field("['responseFoo']").isEqualTo("bar");
+ assertThatJson(parsedJson).field("['url']").isEqualTo("/api/v1/xxxx?foo=bar&foo=bar2");
+ assertThatJson(parsedJson).field("['responseBaz2']").isEqualTo("Bla bla bar bla bla");
 As you can see elements from the request have been properly referenced in the response.
 The generated WireMock stub will look more or less like this:
 {
@@ -4412,16 +4420,16 @@ of elements from the HTTP request. You can use the following options:
       }
     },
     "bodyPatterns" : [ {
-      "matchesJsonPath" : "$[?(@.baz == 5)]"
+      "matchesJsonPath" : "$[?(@.['baz'] == 5)]"
     }, {
-      "matchesJsonPath" : "$[?(@.foo == 'bar')]"
+      "matchesJsonPath" : "$[?(@.['foo'] == 'bar')]"
     } ]
   },
   "response" : {
     "status" : 200,
-    "body" : "{\"url\":\"{{{request.url}}}\",\"param\":\"{{{request.query.foo.[0]}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\",\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\"}",
+    "body" : "{\"authorization\":\"{{{request.headers.Authorization.[0]}}}\",\"path\":\"{{{request.path}}}\",\"responseBaz\":{{{jsonpath this '$.baz'}}} ,\"param\":\"{{{request.query.foo.[0]}}}\",\"pathIndex\":\"{{{request.path.[1]}}}\",\"responseBaz2\":\"Bla bla {{{jsonpath this '$.foo'}}} bla bla\",\"responseFoo\":\"{{{jsonpath this '$.foo'}}}\",\"authorization2\":\"{{{request.headers.Authorization.[1]}}}\",\"fullBody\":\"{{{escapejsonbody}}}\",\"url\":\"{{{request.url}}}\",\"paramIndex\":\"{{{request.query.foo.[1]}}}\"}",
     "headers" : {
-      "Authorization" : "{{{request.headers.Authorization.[0]}}}"
+      "Authorization" : "{{{request.headers.Authorization.[0]}}};foo"
     },
     "transformers" : [ "response-template" ]
   }
@@ -4430,6 +4438,8 @@ of elements from the HTTP request. You can use the following options:
 response body
 {
   "url" : "/api/v1/xxxx?foo=bar&foo=bar2",
+  "path" : "/api/v1/xxxx",
+  "pathIndex" : "v1",
   "param" : "bar",
   "paramIndex" : "bar2",
   "authorization" : "secret",