Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend WireMock with Mokka Groovy transformer #111

Closed
kulasekp opened this issue Feb 20, 2020 · 0 comments · Fixed by #117
Closed

Extend WireMock with Mokka Groovy transformer #111

kulasekp opened this issue Feb 20, 2020 · 0 comments · Fixed by #117
Assignees
Labels
Milestone

Comments

@kulasekp
Copy link
Contributor

kulasekp commented Feb 20, 2020

Mokka provides a feature to define response body as a Groovy scripts.
It is particularly handled by GroovyMockHandler.
Exemplary response body can for example look as follows:

result='''<responseBody>'''
result+=ctx.uri
result+='''</responseBody>'''

return result

Send a request on path defined in the mock configuration and you should receive something like
<responseBody><path></responseBody>

On the other hand WireMock response templating does not support Groovy templates but we may try to achieve the same using WireMock extension mechanism (http://wiremock.org/docs/extending-wiremock): just define custom ResponseDefinitionTransformer or ResponseTransformer with "groovy-transformer" name. ResponseDefinitionTransformer seems to be more proper but to be verified if it will work at this point
Then new extension should be registered in WireMockServerConfiguration via o.extensions.

When implementing Groovy transformer for WireMock please consider the following:

  • GroovyMockHandler uses MockContext in Binding definition (as "ctx" variable). In WireMock case we need to support it also. We do not have to (and in fact we do not want to) reuse MockContext class here but the semantic (fields naming and structure) is important so currently existing scripts could work. That means we should consider some kind of abstraction that will take com.github.tomakehurst.wiremock.http.Request as an input and prepare object similar to MockContext but only with fields: uri, httpMethod, requestBody and from.
  • variables like 'mockConfiguration' and 'httpHelper' - to be removed from GroovyMockHandler
  • WireMock uses RequestTemplateModel as binding variable in its "Response Templating". Would be good to if we could introduce the same model (in parallel to "ctx") in WireMock Groovy Transformer. This simply means:
.put("request", RequestTemplateModel.from(request))
  • finally, RequestTemplateModel should also be introduced in GroovyMockHandler - under the same binding variable ("request").

Apart of JUnit tests the implementation can be also tested manually on the running server by:

  1. sending POST on POST http://localhost:8082/__admin/mappings with the following body:
POST http://localhost:8082/__admin/mappings

{
	"priority": 1,
	"request": {
		"method": "GET",
		"url": "/body"
	},
	"response": {
		"status": 200,
		"body": "result='''<responseBody>'''\r\nresult+=ctx.uri\r\nresult+='''<\/responseBody>'''\r\n\r\nreturn result",
		"headers": {
			"Content-Type": "text/plain"
		},
		"transformers": ["groovy-transformer"]
	}
}
  1. sending GET on http://localhost:8082/body - you should receive the same response as for Mokka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants