implemented request body matching
This commit is contained in:
parent
9b70d03814
commit
abe2f7f5ab
3 changed files with 54 additions and 7 deletions
|
|
@ -81,10 +81,13 @@ public class PredictionJsonFileRepository extends PredictionMemoryRepository {
|
|||
try {
|
||||
DirectoryStream<Path> directoryStream = newDirectoryStream(get(dataDirectoryName));
|
||||
for (Path pathForJsonFile : directoryStream) {
|
||||
Prediction requestResponse = readRequestResponseFromJson(pathForJsonFile);
|
||||
requestResponse.setName(filename(pathForJsonFile));
|
||||
cache.add(requestResponse);
|
||||
log.info("reading {} into memory, mapping path {}", pathForJsonFile, requestResponse.getUrl());
|
||||
if (pathForJsonFile.toString().endsWith(".json")) {
|
||||
log.info("reading {}", pathForJsonFile);
|
||||
Prediction requestResponse = readRequestResponseFromJson(pathForJsonFile);
|
||||
requestResponse.setName(filename(pathForJsonFile));
|
||||
cache.add(requestResponse);
|
||||
log.info("reading {} into memory, mapping path {}", pathForJsonFile, requestResponse.getUrl());
|
||||
}
|
||||
}
|
||||
} catch (IOException x) {
|
||||
throw new RuntimeException(x);
|
||||
|
|
|
|||
|
|
@ -18,13 +18,14 @@ public class Prediction {
|
|||
private String url;
|
||||
private String method;
|
||||
private Headers requestHeaders;
|
||||
private String requestBody = "";
|
||||
|
||||
private String response;
|
||||
private int responseStatus;
|
||||
private Optional<Headers> responseHeaders = empty();
|
||||
|
||||
public boolean requestMatches(String requesturl) {
|
||||
return urlPattern.matcher(requesturl).matches();
|
||||
public boolean requestUrlMatches(String requesturl) {
|
||||
return urlPattern.matcher(requesturl).find();
|
||||
}
|
||||
|
||||
public Prediction() {
|
||||
|
|
@ -37,6 +38,7 @@ public class Prediction {
|
|||
setUrl(predictionDto.url);
|
||||
this.method = predictionDto.method;
|
||||
this.requestHeaders = new Headers(predictionDto.requestHeaders);
|
||||
this.requestBody = predictionDto.requestBody;
|
||||
this.response = predictionDto.response;
|
||||
this.responseStatus = predictionDto.responseStatus;
|
||||
this.responseHeaders = predictionDto.getResponseHeaders() != null ? Optional.of(new Headers(predictionDto.getResponseHeaders())) : empty();
|
||||
|
|
@ -106,6 +108,14 @@ public class Prediction {
|
|||
this.responseHeaders = Optional.ofNullable(responseHeaders);
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
|
@ -155,6 +165,7 @@ public class Prediction {
|
|||
|
||||
private String url;
|
||||
private String method;
|
||||
private String requestBody;
|
||||
private Map<String, List<String>> requestHeaders;
|
||||
|
||||
private String response;
|
||||
|
|
@ -217,6 +228,14 @@ public class Prediction {
|
|||
this.requestHeaders = requestHeaders;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
*/
|
||||
package nl.wehkamp.everest.service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -32,18 +34,41 @@ public class ResponseFinder {
|
|||
|
||||
Set<Prediction> all = predictionJsonFileRepository.findAll();
|
||||
for (Prediction prediction : all) {
|
||||
response = matchRequestBody(request, response, prediction);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private Optional<Prediction> matchRequestBody(HttpServletRequest request, Optional<Prediction> response, Prediction prediction) {
|
||||
String requestbody = getBody(request);
|
||||
if (prediction.getRequestBody().equals(requestbody)) {
|
||||
response = matchUrlAndMethodAndHeaders(request, response, prediction);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private Optional<Prediction> matchUrlAndMethodAndHeaders(HttpServletRequest request, Optional<Prediction> response, Prediction prediction) {
|
||||
if (prediction.requestMatches(request.getPathInfo())) {
|
||||
if (prediction.requestUrlMatches(request.getPathInfo())) {
|
||||
response = matchMethodAndHeaders(request, response, prediction);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private String getBody(HttpServletRequest request) {
|
||||
BufferedReader reader;
|
||||
try {
|
||||
reader = request.getReader();
|
||||
String line = null;
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
while ((line = reader.readLine()) != null) {
|
||||
buffer.append(line);
|
||||
}
|
||||
return buffer.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Prediction> matchMethodAndHeaders(HttpServletRequest request, Optional<Prediction> response, Prediction prediction) {
|
||||
if (prediction.getMethod().equals(request.getMethod())) {
|
||||
if (prediction.containsRequestHeaders()) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue