Hello everyone, I am currently trying to create an...
# help
n
Hello everyone, I am currently trying to create an small WireMock extension. The reason? the program I am listening sometimes sends in the header, in the "Host" weird content that seems to be accepted in general by the real server. In the case of 'Host' seems wrong, WireMock instantly rejects this message with <h1>Bad Message 400</h1><pre>reason: Bad HostPort</pre> So I created a really small extension and overrided the match function from RequestMatcherExtension (and hope this would bypass this 400 error message). I did everything in command line for creating my .jar extension. The issue occurs when I try to start the wiremock standalone with the extension: java -cp "wiremock-standalone-3.10.0.jar:./IgnoreHostHeaderMatcher.jar" -jar wiremock-standalone-3.10.0.jar --port 80 --bind-address 192.168.1.80 --verbose --extensions mypackage.IgnoreHostHeaderMatcher It results as a ClassNotFoundException: mypackage.IgnoreHostHeaderMatcher. For being sure it's not because of my .jar generated, I also tried with the example 'test-extension.jar' from the documentation (https://github.com/wiremock/wiremock/tree/master/test-extension) sudo java -cp "wiremock-standalone-3.10.0.jar:./test-extension.jar" -jar wiremock-standalone-3.10.0.jar --port 80 --bind-address 192.168.1.80 --verbose --extensions org.wiremock.InstanceLoaderTestExtension Same issue: ClassNotFoundException: org.wiremock.InstanceLoaderTestExtension I am sure I do something wrong but I cannot figure out the reason. Java is not something that I master well, especially regarding Maven or gradle. That's the reason I do everything in command line. Thanks in advance for your help!
l
Did you implement the services interface in your extension? If you did then WireMock should load the extension just by it being on the classpath. An example of an extension that uses this is the faker extension - https://github.com/wiremock/wiremock-faker-extension/blob/main/src/main/resources/[…]NF/services/com.github.tomakehurst.wiremock.extension.Extension
Implementing that should allow you to not have to add this part to the startup parameters -
--extensions mypackage.IgnoreHostHeaderMatcher
Other than that, without looking at your extension it is a little tricky to know what the problem might be. I would be interested to know the content of the header that WireMock is rejecting. Would you be able to send an example?
n
without going too deep in details, it's xml datas for an ioT device that also is in the body. For some weird reason of the dev, they also added it in the Host... For example: Host: <LEDControl><control>true</control><blue>false</blue><red>false</red></LEDControl> a curl command with 'Host' containing this will also be rejected by wiremock. For the extension, I tried without the --extensions but I don't see any change when shooting a curl with that 'Host' (still this 400 error message)
cat META-INF/services/mypackage.extension.Extension mypackage.IgnoreHostHeaderMatcher
l
The name of the file in the
services
directory needs to be
com.github.tomakehurst.wiremock.extension.Extension
n
ok, I will try that
the results from the curl still seem to be same than before. I in case just added a System.err.println in my overrided "match" function but it does not seem like to be displayed in the terminal when I shoot a wrong or correct curl message. I wonder it that way is enough to implement my custom "match" function overrided
Also, while I am at it: is the way to override the match from RequestMatcherExtension to return true every time the good approach for accepting any http request, regardless of the 'Host' content? If not, is there an other solution to bypass the 'Host' format check?
For the compilation, I do it in that way: javac -cp "./wiremock-standalone-3.10.0.jar" mypackage/IgnoreHostHeaderMatcher.java jar cf IgnoreHostHeaderMatcher.jar -C . mypackage/ -C . META-INF/ my extension file looks like that:
l
I am just having a play around with the host header example you sent. My concern is that it is actually Jetty that is kicking this out rather than WireMock. If that is the case then the request won't even make it to your matcher. I have just tested this out and there are no requests in the request log so it looks like it is Jetty
n
ok thank you for your answer, I will investigate into Jetty if there is a way to ignore the content of 'Host'
l
I will take a look as well
t
Have you tried the Jetty 12 JAR? Might be that this is something that’s been fixed/changed between 11 and 12.
(there isn’t a standalone version of -jetty12 unfortunately so you’d need to bundle your own, but still worth a look IMO)
n
seems like Jetty 12 enforced a stricter validation of malformed Host compared to Jetty11. I don't think it will help me that much x)
https://github.com/jetty/jetty.project/issues/11287 With Jetty11 seems like I can bypass by adding a HttpConfiguration.Customizer. It seems to be also possible with Jessy12 since ~march. Upgrading does not seem like a solution. I just have to rebuild a wiremock solution with Jetty configuration xml file
t
I strongly suspect there’ll be a way to do it programmatically without having to resort to XML.
BTW you can provide an alternative HTTP server as an extension to WireMock, so you could modify Jetty by cloning just that part and making your changes rather than having to fork the whole project.
n
I am currently seeing the easiest / simpliest way to do it. Indeed I am trying to avoid forking the whole WireMock. It seems too overkill.
t
n
unfortunately, I am not good enough regarding java subclass / extension and cannot spend extra time on that right now :s. I will temporary update the code from the software I am testing and come back to Jetty when I will have more time to spend on it.
Thanks a lot for all of your support. It helped me to understand the reason of my issues. I will probably read again that thread later on 🙂
👍 1