implemented request body matching

This commit is contained in:
Sander Hautvast 2015-07-07 15:28:01 +02:00
parent 9b70d03814
commit abe2f7f5ab
3 changed files with 54 additions and 7 deletions

View file

@ -81,10 +81,13 @@ public class PredictionJsonFileRepository extends PredictionMemoryRepository {
try { try {
DirectoryStream<Path> directoryStream = newDirectoryStream(get(dataDirectoryName)); DirectoryStream<Path> directoryStream = newDirectoryStream(get(dataDirectoryName));
for (Path pathForJsonFile : directoryStream) { for (Path pathForJsonFile : directoryStream) {
Prediction requestResponse = readRequestResponseFromJson(pathForJsonFile); if (pathForJsonFile.toString().endsWith(".json")) {
requestResponse.setName(filename(pathForJsonFile)); log.info("reading {}", pathForJsonFile);
cache.add(requestResponse); Prediction requestResponse = readRequestResponseFromJson(pathForJsonFile);
log.info("reading {} into memory, mapping path {}", pathForJsonFile, requestResponse.getUrl()); requestResponse.setName(filename(pathForJsonFile));
cache.add(requestResponse);
log.info("reading {} into memory, mapping path {}", pathForJsonFile, requestResponse.getUrl());
}
} }
} catch (IOException x) { } catch (IOException x) {
throw new RuntimeException(x); throw new RuntimeException(x);

View file

@ -18,13 +18,14 @@ public class Prediction {
private String url; private String url;
private String method; private String method;
private Headers requestHeaders; private Headers requestHeaders;
private String requestBody = "";
private String response; private String response;
private int responseStatus; private int responseStatus;
private Optional<Headers> responseHeaders = empty(); private Optional<Headers> responseHeaders = empty();
public boolean requestMatches(String requesturl) { public boolean requestUrlMatches(String requesturl) {
return urlPattern.matcher(requesturl).matches(); return urlPattern.matcher(requesturl).find();
} }
public Prediction() { public Prediction() {
@ -37,6 +38,7 @@ public class Prediction {
setUrl(predictionDto.url); setUrl(predictionDto.url);
this.method = predictionDto.method; this.method = predictionDto.method;
this.requestHeaders = new Headers(predictionDto.requestHeaders); this.requestHeaders = new Headers(predictionDto.requestHeaders);
this.requestBody = predictionDto.requestBody;
this.response = predictionDto.response; this.response = predictionDto.response;
this.responseStatus = predictionDto.responseStatus; this.responseStatus = predictionDto.responseStatus;
this.responseHeaders = predictionDto.getResponseHeaders() != null ? Optional.of(new Headers(predictionDto.getResponseHeaders())) : empty(); this.responseHeaders = predictionDto.getResponseHeaders() != null ? Optional.of(new Headers(predictionDto.getResponseHeaders())) : empty();
@ -106,6 +108,14 @@ public class Prediction {
this.responseHeaders = Optional.ofNullable(responseHeaders); this.responseHeaders = Optional.ofNullable(responseHeaders);
} }
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -155,6 +165,7 @@ public class Prediction {
private String url; private String url;
private String method; private String method;
private String requestBody;
private Map<String, List<String>> requestHeaders; private Map<String, List<String>> requestHeaders;
private String response; private String response;
@ -217,6 +228,14 @@ public class Prediction {
this.requestHeaders = requestHeaders; this.requestHeaders = requestHeaders;
} }
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public String getResponse() { public String getResponse() {
return response; return response;
} }

View file

@ -8,6 +8,8 @@
*/ */
package nl.wehkamp.everest.service; package nl.wehkamp.everest.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -32,18 +34,41 @@ public class ResponseFinder {
Set<Prediction> all = predictionJsonFileRepository.findAll(); Set<Prediction> all = predictionJsonFileRepository.findAll();
for (Prediction prediction : all) { 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); response = matchUrlAndMethodAndHeaders(request, response, prediction);
} }
return response; return response;
} }
private Optional<Prediction> matchUrlAndMethodAndHeaders(HttpServletRequest request, Optional<Prediction> response, Prediction prediction) { 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); response = matchMethodAndHeaders(request, response, prediction);
} }
return response; 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) { private Optional<Prediction> matchMethodAndHeaders(HttpServletRequest request, Optional<Prediction> response, Prediction prediction) {
if (prediction.getMethod().equals(request.getMethod())) { if (prediction.getMethod().equals(request.getMethod())) {
if (prediction.containsRequestHeaders()) { if (prediction.containsRequestHeaders()) {