Decathlon / tzatziki

Decathlon library to ease and promote Test Driven Development of Java microservices!
Apache License 2.0
58 stars 27 forks source link

[tzatziki-http] Invalid regex throwing PatternSyntaxException in callback function for interaction and is not caught #119

Closed rbarronDecathlon closed 1 year ago

rbarronDecathlon commented 1 year ago

When creating a mock for an Interaction, if the url string is an invalid regex, compiling it will throw a PatternSyntaxException which will only be caught by MockServer which defaults to a 404 instead of the return status and body from the Given step.

Error occurs in HttpSteps: https://github.com/Decathlon/tzatziki/blob/45876446663d2e7ea420f316c70fc9d328747d88/tzatziki-http/src/main/java/com/decathlon/tzatziki/steps/HttpSteps.java#L163

For example:

Given that getting on "/products?search[]=123" will return a status OK_200 and:
"""
product:
  id:1
"""

When my application then makes a call to "/products?search[]=123", the callback function throws an error which is caught by Mockserver:

2022-12-06 20:08:51,276 [WARN ] [MockServer-EventLog0] o.m.l.MockServerEventLog - 59964 returning { "statusCode" : 404, "reasonPhrase" : "Not Found" } because client 6a591f01-8d7d-491a-b4df-2e907b24fef7 response callback throw an exception java.util.regex.PatternSyntaxException: Unclosed character class near index 21 /products?search[]=123 ^

This is caused due to the empty square brackets [] which will cause Pattern.compile to throw the exception

Proposed solution:

Only compile the mocked url to a regex if it is one, or if the return body is a template ( with $1 $2 etc ... )

This whole block can also be skipped (?) if the url and return body are literals string https://github.com/Decathlon/tzatziki/blob/45876446663d2e7ea420f316c70fc9d328747d88/tzatziki-http/src/main/java/com/decathlon/tzatziki/steps/HttpSteps.java#L186-L204

brian-mulier commented 1 year ago

Hello, I can understand it, however I'm questioning about how to know if it's a regex or not. What we can do is try catch the compile and in case of exception mock the raw path, what do you think ?

rbarronDecathlon commented 1 year ago

Yes, if we catch the exception, it should work. I'm unsure what you mean by mocking the raw path? With a try/catch, we need to assign a value to urlPattern in the catch clause

brian-mulier commented 1 year ago

What I mean is we take the raw input of the user and put it onto the mocked url

brian-mulier commented 1 year ago

Closed due to resolution in #121