Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Fixture templating: adds the ability to create fixtures from templates files using dynamic values #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mcassiano
Copy link

This PR adds a templating mechanism to requestmatcher. It works by replacing keys that are in the ${KEY} format by the values specified using the fluent API exposed to users of the library, like this:

server.addTemplate(404, "json_error_template.json")
                .withValueForKey("error", "Resource not found")
                .ifRequestMatches()
                //...

I believe no change is breaking! I chose to change the method name from addFixture to addTemplate in this case to make the intentions clearer. The delimiters ($,{,}) are open for discussion!

Copy link
Contributor

@cs-victor-nascimento cs-victor-nascimento left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some nitpicks form myself but this LGTM.

README.md Outdated

server.addTemplate("json_template.json")
.withValueForKey("current_date", timestamp)
.ifRequestMatches("/get_current_date");
Copy link
Contributor

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?

@@ -0,0 +1,3 @@
{
"error_message": "${error}"
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

@@ -0,0 +1,3 @@
{
"current_date": "${current_date}"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@@ -0,0 +1,3 @@
{
"error_message": "${error}"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line here too please.

@@ -0,0 +1,3 @@
{
"current_date": "${current_date}"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another new line :)

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What triggers dynamically IllegalArgument?

Copy link
Author

@mcassiano mcassiano Mar 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IOReader's read throws IllegalArgumentException when it receives a null arg, so if someone tries to open a file that does not exist, IllegalArgumentException is thrown without much detail. If you catch it, the exception is rethrown as a RuntimeException and with a helpful message (stating the resolved path for the file).

Copy link
Author

@mcassiano mcassiano Mar 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to go around this is to throw a FileNotFoundException when getResourceAsStream returns null inside LocalTestRequestMatcherRule's open. Let me know what you think is best!

@mcassiano
Copy link
Author

Should’ve ran the quality suite 🧐

@mcassiano
Copy link
Author

Friendly ping

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants