-
Notifications
You must be signed in to change notification settings - Fork 11
Fixture templating: adds the ability to create fixtures from templates files using dynamic values #16
base: master
Are you sure you want to change the base?
Fixture templating: adds the ability to create fixtures from templates files using dynamic values #16
Changes from 4 commits
b0e0d5e
053f948
f084334
50e1718
1d21c82
f859251
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"error_message": "${error}" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a new line at the end of the file? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"current_date": "${current_date}" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,7 +126,7 @@ public MockWebServer getMockWebServer() { | |
public String readFixture(final String fixturePath) { | ||
try { | ||
return IOReader.read(open(fixturesRootFolder + "/" + fixturePath)) + "\n"; | ||
} catch (IOException e) { | ||
} catch (IOException | IllegalArgumentException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What triggers dynamically IllegalArgument? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IOReader's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way to go around this is to throw a |
||
throw new RuntimeException("Failed to read asset with path " + fixturePath, e); | ||
} | ||
} | ||
|
@@ -187,25 +187,32 @@ public IfRequestMatches<RequestMatchersGroup> addFixture(String fixturePath) { | |
*/ | ||
public IfRequestMatches<RequestMatchersGroup> addFixture(int statusCode, String fixturePath) { | ||
|
||
final MockResponse mockResponse = new MockResponse() | ||
.setResponseCode(statusCode) | ||
.setBody(readFixture(fixturePath)); | ||
|
||
if (guessMimeType) { | ||
final String mimeType = IOReader.mimeTypeFromExtension(fixturePath); | ||
String body = readFixture(fixturePath); | ||
return addResponse(prepareMockResponse(statusCode, fixturePath, body)); | ||
} | ||
|
||
if (mimeType != null) { | ||
mockResponse.addHeader("Content-Type", mimeType); | ||
} | ||
} | ||
/** | ||
* Adds a template to be used during the test case, later turning it into a fixture to be used. | ||
* | ||
* @param templatePath The path of the fixture inside the fixtures folder. | ||
* @return A dsl instance {@link DynamicIfRequestMatches} for chaining | ||
*/ | ||
public DynamicIfRequestMatches<RequestMatchersGroup> addTemplate(String templatePath) { | ||
return addTemplate(200, templatePath); | ||
} | ||
|
||
if (!defaultHeaders.isEmpty()) { | ||
for (String headerKey : defaultHeaders.keySet()) { | ||
mockResponse.addHeader(headerKey, defaultHeaders.get(headerKey)); | ||
} | ||
} | ||
/** | ||
* Adds a template to be used during the test case, later turning it into a fixture to be used. | ||
* | ||
* @param templatePath The path of the fixture inside the fixtures folder. | ||
* @param statusCode The status of the mocked response. | ||
* @return A dsl instance {@link DynamicIfRequestMatches} for chaining | ||
*/ | ||
public DynamicIfRequestMatches<RequestMatchersGroup> addTemplate(int statusCode, String templatePath) { | ||
|
||
return addResponse(mockResponse); | ||
String templateBody = readFixture(templatePath); | ||
return new DynamicIfRequestMatches<>(new RequestMatchersGroup(), dispatcher, | ||
templateBody, prepareMockResponse(statusCode, templatePath, "")); | ||
} | ||
|
||
/** | ||
|
@@ -258,6 +265,54 @@ public T ifRequestMatches() { | |
} | ||
} | ||
|
||
public static class DynamicIfRequestMatches<T extends RequestMatchersGroup> extends IfRequestMatches<T> { | ||
|
||
private final HashMap<String, String> values = new HashMap<>(); | ||
private final String templateBody; | ||
private final MockResponse response; | ||
private final MatcherDispatcher dispatcher; | ||
|
||
private DynamicIfRequestMatches(T group, MatcherDispatcher dispatcher, | ||
String templateBody, MockResponse mockResponse) { | ||
super(group); | ||
this.templateBody = templateBody; | ||
this.dispatcher = dispatcher; | ||
this.response = mockResponse; | ||
} | ||
|
||
@Override | ||
public T ifRequestMatches() { | ||
return dispatchDynamic(super.ifRequestMatches(), values); | ||
} | ||
|
||
public DynamicIfRequestMatches<T> withValueForKey(String key, String value) { | ||
values.put(key, value); | ||
return this; | ||
} | ||
|
||
private T dispatchDynamic(T group, HashMap<String, String> values) { | ||
response.setBody(replaceValues(values)); | ||
dispatcher.addFixture(response, group); | ||
return group; | ||
} | ||
|
||
private String replaceValues(HashMap<String, String> values) { | ||
String body = templateBody; | ||
|
||
for (String key : values.keySet()) { | ||
String keyFinder = "${" + key + "}"; | ||
|
||
if (!body.contains(keyFinder)) | ||
fail("Could not find any template key named " + key); | ||
|
||
body = body.replace(keyFinder, values.get(key)); | ||
} | ||
|
||
return body; | ||
} | ||
|
||
} | ||
|
||
private void after(Exception exception, boolean success) throws Exception { | ||
|
||
if (dispatcher.getAssertionException() != null) { | ||
|
@@ -324,4 +379,27 @@ public void evaluate() throws Throwable { | |
} | ||
}; | ||
} | ||
|
||
private MockResponse prepareMockResponse(int statusCode, String path, String body) { | ||
|
||
final MockResponse mockResponse = new MockResponse() | ||
.setResponseCode(statusCode) | ||
.setBody(body); | ||
|
||
if (guessMimeType) { | ||
final String mimeType = IOReader.mimeTypeFromExtension(path); | ||
|
||
if (mimeType != null) { | ||
mockResponse.addHeader("Content-Type", mimeType); | ||
} | ||
} | ||
|
||
if (!defaultHeaders.isEmpty()) { | ||
for (String headerKey : defaultHeaders.keySet()) { | ||
mockResponse.addHeader(headerKey, defaultHeaders.get(headerKey)); | ||
} | ||
} | ||
|
||
return mockResponse; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"error_message": "${error}" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New line here too please. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"current_date": "${current_date}" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another new line :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
ifRequestMatches()
does not take arguments right?