I am writing Spring Boot Integration test. For this, we boot up the empty application (context) and call multiple services as part of test.
One of the requirements is to call the external REST endpoint with payload that contains an URL to notify (call back) and that service, after some business logic, calls the URL received in the payload.
From the test, I can call the REST endpoint, which is external service. But I want to be able to test the call back from that service for the given URL. URL can be random.
Can I do with MockClientServer? OR MockRestServiceServer Or some other day to test this?
Essentially, I want the test to be able to get an external call and verify it.
After trying out a couple things, WireMock actually does what I wanted. It takes the rest call from outside application(s). Very useful for testing.
I have microservice based app, and I need to mock one of the service.
So when Im requesting one microservice http://service1/src it triggers another one by link lets say
http://service2/src
I cannot change this link to the mocked one but I want to start wiremock somehow and fake this link, so when I will send request to http://service1/src it will trigger http://service2/src but mocked one
Im able to setup wiremock but I cannot get it directly by link http://service2/src I need to add http://localhost:8080/http://service2/src
Or maybe you can advice some another framework for such purpose
thanks for helping in advance
Background
I'm writing a web service that makes calls to an external api. This api has not yet been put into production. As such, when I make a call to my web service in my dev environment, I want to stub out the responses that it returns. Note: this is not a unit testing question.
My Solution So Far
The api calls are made using RestTemplate from the lovely Spring people, the url of which is held in application.properties. This has allowed me to set different urls for different environments using Active Profiles. So, for example, application-dev.properties holds a different url.
The dev url is ideally a pointer to a json file under resources/.
My Issue
I can't seem to get RestTemplate to pick up the local json file. The url I'm using is:
url = "file://staticJson.json"
However that comes up with a
Object of class [sun.net.www.protocol.ftp.FtpURLConnection] must be an instance of class java.net.HttpURLConnection
And now I'm unsure of how to proceed, or if this is even possible without extending RestTemplate.
Any directions to try or solutions would be fantastic.
If any more information is required I'll do my best to provide it asap.
I am monitoring my web application(CXF, Spring, Hibernate) running on Tomcat 7, using New Relic Java agent 2.18.0.
My services has end point URLs such that they are same for GET & PUT request only HTTP method is different, but when I look at "web transactions" I see only 1 URL instead of separate URLs for separate methods because of which I cannot find whether GET request is slow or PUT request . Is there any config/hack using which I can divide transactions further by HTTP methods instead of just URL strings?
Unfortunately your best bet is to make an API call to set the transaction name to what you want.
https://newrelic.com/docs/java/naming-web-transactions
If you think adding the method automatically is a valuable feature, I suggest submiting a feature request to New Relic support.
I have a class that I want to hook and redirect HTTP requests in.
I also have a loader class already written, but all it does it replace the functions that contain the HTTP requests I want to change.
Is there a way to hook HTTP requests in Java so that I can redirect them all more easily?
Sort of like a proxy-wrapper.
Clarification:
The app sends out a GET or POST request to a URL.
I need the content to remain the same, just change the URL.
DNS redirects won't work, the Host HTTP header needs to be correct for the new server.
PS: This is a Desktop App, not a server script.
A cumbersome but reliable way of doing this would be to make your application use a proxy server, and then write a proxy server which makes the changes you need. The proxy server could be in-process in your application; it wouldn't need to be a separate program.
To use a proxy, set a couple of system properties - http.proxyHost and http.proxyPort. Requests made via HttpURLConnection will then use that proxy (unless they specifically override the default proxy settings). Requests made using some other method like Apache HttpClient will not, i think, be affected, but hopefully, all your requests are using HttpURLConnection.
To implement the proxy, if you're using a Sun JRE, then you should probably use the built-in HTTP server; set up a single handler mapped to the path "/", and this will pick up all requests being sent by your app, and can then determine the right URL to send them to, and make a connection to that URL (with all the right headers too). To make the connection, use URL.openConnection(Proxy.NO_PROXY) to avoid making a request to the proxy and so getting caught in an infinite loop. You'll then need to pump input and output between the two sockets.
The only other way i can think of to do this would be to override HttpURLConnection with a new handler which steers requests to your desired destination; you'd need to find a way to persuade the URL class to use your handler instead of the default one. I don't know how you'd do that in a clean way.
While an older post, this should give some ideas of some kinds of bytecode injects which can be peformed: Java Programming: Bytecode Injection. Another tool is Javassist and you may be able to find some links from the Aspected-oriented programming wiki article (look at the bytecode weavers section).
There are some products which extensively dynamically modify code.
Depending upon what is desired, there may be ... less painful ... methods. If you simply want to 'hook' HTTP requests, another option is just to use a proxy (which could be an external process) and funnel through that. Using a proxy would likely require control over the name resolution used.
you can use servlet filters which intercept the requests, the requests can further be wrapped, redirected, forwarded or completed from here.
http://www.oracle.com/technetwork/java/filters-137243.html
Do you control all of the code? If so, I suggest using Dependency Injection to inject the concrete implementation you want, which would allow you to instead inject a proxy class.
If you can change the source code, just change it and add your extra code on each HTTP request.
If you can't change the source code, but it uses dependency injection, perhaps you can inject something to catch requests.
Otherwise: use aspect-oriented programming and catch to URL class, or whatever you use to do HTTP requests. #AspectJ (http://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj.html ) is quite easy and powerful.