Matcher methodAndPathMatcher = Pattern.compile("(GET|POST|PUT|DELETE|HEAD|OPTIONS) (.+)").matcher(definition);
if (methodAndPathMatcher.matches()) {
String then = checkInstanceOf("then", whenThen.get("then"), String.class).trim();
HttpStatus code = HttpStatus.OK;
int endLineIndex = then.indexOf("\n");
if (endLineIndex == -1) {
endLineIndex = then.length();
}
String firstLine = then.substring(0, endLineIndex);
Matcher respMatcher = Pattern.compile("^(\\d{3}).*$").matcher(firstLine);
if (respMatcher.matches()) {
code = HttpStatus.havingCode(Integer.parseInt(respMatcher.group(1)));
then = then.substring(endLineIndex).trim();
}
whens.add(whenHttpBuilder
.withMethod(methodAndPathMatcher.group(1))
.withPath(methodAndPathMatcher.group(2))
.withBody(body)
.withThen(new ThenHttpResponse(code.getCode(), then))
.build());
} else {
throw new IllegalArgumentException("unrecognized 'when' format: it must begin with " +
"a HTTP declaration of the form 'VERB resource/path'\nEg: GET users/johndoe\n. Was: '" + ws + "'\n");
}