Referencing request from response (#238)

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:

- `fromRequest().url()` - return the request URL
- `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().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

fixes #237
This commit is contained in:
Marcin Grzejszczak
2017-03-02 10:14:10 +01:00
committed by GitHub
parent 7340f2893d
commit d6f2227f89
41 changed files with 1407 additions and 156 deletions

View File

@@ -5,7 +5,6 @@ import org.junit.Before;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
public class FraudBase {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new FraudDetectionController(),

View File

@@ -0,0 +1,17 @@
package com.example.fraud;
import org.junit.Before;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
public class FraudnameBase {
private static final String FRAUD_NAME = "fraud";
FraudVerifier fraudVerifier = FRAUD_NAME::equals;
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new FraudNameController(this.fraudVerifier));
}
}

View File

@@ -0,0 +1,25 @@
package contracts.fraudname
org.springframework.cloud.contract.spec.Contract.make {
request {
// highest priority
priority(1)
method PUT()
url '/frauds/name'
body([
name: "fraud"
])
headers {
contentType("application/vnd.fraud.v1+json")
}
}
response {
status 200
body([
result: "Sorry ${fromRequest().body('$.name')} but you're a fraud"
])
headers {
header(contentType(), "${fromRequest().header(contentType())};charset=UTF-8")
}
}
}

View File

@@ -0,0 +1,23 @@
package contracts.fraudname
org.springframework.cloud.contract.spec.Contract.make {
request {
method PUT()
url '/frauds/name'
body([
name: $(anyAlphaUnicode())
])
headers {
contentType("application/vnd.fraud.v1+json")
}
}
response {
status 200
body([
result: "Don't worry ${fromRequest().body('$.name')} you're not a fraud"
])
headers {
header(contentType(), "${fromRequest().header(contentType())};charset=UTF-8")
}
}
}