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:
committed by
GitHub
parent
7340f2893d
commit
d6f2227f89
@@ -1,5 +1,7 @@
|
||||
package com.example.fraud;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -8,8 +10,6 @@ import com.example.fraud.model.FraudCheck;
|
||||
import com.example.fraud.model.FraudCheckResult;
|
||||
import com.example.fraud.model.FraudCheckStatus;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
|
||||
|
||||
@RestController
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.example.fraud;
|
||||
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@RestController
|
||||
class FraudNameController {
|
||||
|
||||
private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json";
|
||||
|
||||
private final FraudVerifier fraudVerifier;
|
||||
|
||||
FraudNameController(FraudVerifier fraudVerifier) {
|
||||
this.fraudVerifier = fraudVerifier;
|
||||
}
|
||||
|
||||
@PutMapping(
|
||||
value = "/frauds/name",
|
||||
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
||||
public NameResponse checkByName(@RequestBody NameRequest request) {
|
||||
boolean fraud = this.fraudVerifier.isFraudByName(request.getName());
|
||||
if (fraud) {
|
||||
return new NameResponse("Sorry " + request.getName() + " but you're a fraud");
|
||||
}
|
||||
return new NameResponse("Don't worry " + request.getName() + " you're not a fraud");
|
||||
}
|
||||
}
|
||||
|
||||
interface FraudVerifier {
|
||||
boolean isFraudByName(String name);
|
||||
}
|
||||
|
||||
class NameRequest {
|
||||
private String name;
|
||||
|
||||
public NameRequest(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public NameRequest() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
class NameResponse {
|
||||
private String result;
|
||||
|
||||
public NameResponse(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public NameResponse() {
|
||||
}
|
||||
|
||||
public String getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
@@ -55,4 +55,3 @@ class Response {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user