I'm using Zuul as an Api-Gateway. In this way I'm injecting various headers into the original request to the backend service.
Is there any way to integration-test this behavior?
I already came along SimpleHostRoutingFilter which seems to do the actual call to the backend service. But I don't get a mocking-foot into the door, since it constructs the HttpClient without injection.
I'm using Spock for testing, in this way there is no support for #MockBean right now.
Edit: Picture for better understanding.
I want to test the call to the origin server, because I don't have this one on build.
Related
I want to mock api calls from my application, and host the mock, so my tests can work without calls to real api. There is a service called restbird which does exactly that, but it is far from ideal for me. If you want to collaborate you have to host the service by your self. Also it has some errors like not displaying history of calls, or when it sends server errors for no reason. I want a service more robust than this one.
The only service that I think might be a good fit is SwaggerHub, it seems robust, it has virtual servers, and overall it is very popular. But the only problem is that I cannot find a way to record api calls from my application. So how can I record api calls for SwaggerHub?
There does not currently exist any functionality within SwaggerHub itself to record API calls made from the Swagger UI module within the tool. This is a limitation of the open-source Swagger UI tool.
What I can recommend is you use the Swagger Inspector tool. The Swagger Inspector can be used to make API calls from a client, save both the request and the response, and even generate an OpenAPI file for you based off the request/responses. If you create an account and sign in, you can even save your API calls to a collection to use later.
Swagger Inspector: https://inspector.swagger.io/builder
It may also be worth considering using ReadyAPI's Virtualization module to handle this use case. With ReadyAPI Virtualization you can record transactions from a browser, build mock services from the recorded transaction or an existing API definition, and then host the mock service using VirtServer.
ReadyAPI is a part of SmartBears API lifecycle products, so there are integrations between the two tools. For instance, you can port APIs from Swaggerhub into ReadyAPI directly and you can use mock services built in ReadyAPI to do dynamic mocking in Swaggerhub.
You can find more information about ReadyAPI Virtualization here: https://smartbear.com/product/ready-api/api-virtualization/
I realise this is a very late response to this thread, but hopefully this information comes in handy.
I have a spring boot 2.0.2 web application behind a zuul gateway. My application uses spring hateoas links. Also my application has context-path set lets say xxx.
When queries are made through zuul gateway, hateoas links become invalid. Reason is the following:
Lets say a make a call https://localhost/serviceName/service-context-path/path. I would require my heteoas links become i.e.
https://localhost/serviceName/service-context-path/path/
however, these links become: https://localhost/service-context-path/path/.
Ihave made search and found in stack overflow this link (HATEOAS paths are invalid when using an API Gateway in a Spring Boot app)
Indeed, add-proxy-headers by default true sends X-FORWARDED-PREFIX (allong host, etc) which i guess it was communitys intention to fix this problem. But... when i debug my application i reached this method org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromContextPath, with java doc part the following:
As of 4.3.15, this method replaces the contextPath with the value "X-Forwarded-Prefix" rather than prepending, thus aligning with {#code ForwardedHeaderFiller}.
This means the following, when X-FORWARDED-PREFIX is present, application context is replaced from the zuul's prefix which is the serviceName. I guess uul developers on their side cannot know the application context and on the other side, org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromContextPath is compatible with the corresponding specification.
Question: How do i proceed? i for sure wish my hateoas links to be correct when i use zuul
My quess is that there is no solution here.
Proper way is to avoid usage of zuul and move to Spring cloud gateway.
I've been trying to find a simple all purpose unit test framework for Spring MVC based Rest Services I've written.
I've been searching online and narrowed it down to:
RestFuse (http://developer.eclipsesource.com/restfuse/)
Rest Assured (https://github.com/jayway/rest-assured)
MockMVC (http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/)
I like RestFuse because it's mostly annotation based, but rest assured seems to have a easier way of passing parameters and checking responses. And finally being a Spring MVC Rest Service project, I'm wondering if I should just stick with the already established way of testing Rest Services in Spring with MockMVC.
Interested to get any feedback, as well as performance, past experiences and if there's anything else I should take into consideration.
Rest-Assured is gaining acceptance over the other frameworks to test REST services in Java. Having BDD style fluent interface, its easy to read the test scripts also, with minimal learning curve.
I am using this framework for verifying REST services as an end user, and it has been easier to implement test scripts for them. Hence I cannot comment much on Spring MVC part of REST-assured.
However, this blog post gives you more details on RestAssured v2.2 which includes spring-mock-mvc module built on top of MockMVC giving BDD style fluent interface flavor through REST assured. The blog post also cautions:
When not to use it:
RestAssuredMockMvc is not to be considered a complete replacement to
vanilla MockMvc since it contains more specific features coupled to
Spring MVC. For example right now there’s no first class support for
things like flash attributes and principals. You can how ever add
those by using an interceptor. Standard REST Assured also supports a
lot of different authentication schemes and filters which are not
available in the RestAssuredMockMvc API. Another reason for why you
may want to use the standard REST Assured API is if it’s important to
your organization to test the REST API in a real container (for
example if you’ve configured authentication or authorization in a
container specific manner) or if you’re using JAX-RS (or any other
framework regardless of language).
Finally, have a look at REST-Assured spring-mvc-webapp examples in REST-assured codebase, and decide, whether you like to give a try for it and make the best use of both REST-assured and MockMVC frameworks.
The beauty of MockMVC is that it provides a mock servlet container, allowing you to integration-test your REST services without deploying to a web server. I believe that you can still leverage this power when using REST Assured with the spring-mock-mvc module.
Another framework that I just learned of is Karate. Its author is currently experimenting with a mechanism to allow execution in a mock servlet container (see Peter Thomas' answer to Is there a mechanism for integration testing JAX-RS services without deploying (a la MockMVC)?).
I'm building a Servicestack project replacing an old WCF service node. There is a binary program that has an URL hardcoded on it and I'm not able to get/mainteain the source code.
It goes to an URL like "http://toto.com/boo.asmx".
I wrote the services in servicestack with ease and everything works great using the default endpoint "/soap12".
So to finish implementing the brillant Servicestack I just need to somehow add a route or rename the default soap12 endpoint so it listens to /boo.asmx
After hours of trying, inspecting code and googling, I was wondering if it is simply possible ?
I want to implement REST service in gwt .But I don't know how to go about it. I read some documents where they have implemented it using RestyGwt and jercy. But I have one app which is deployed on tomcat. Then situation is that my client side app is calling the methods on the application present on tomcat.
I have to implement it using REST so that my client side call will first go to Proxy service on client then it make REST call to the application on tomcat and fetch result and return. How can I do this in gwt. ?
As mentioned, you can only communicate with REST service.
Anyway, maybe take a look on dispatch concept in GWTPlatform and their way of implementing it. (https://github.com/ArcBees/GWTP/wiki/Rest-Dispatch).
Idea is easy, you have an action on client side which is registered to deal with rest url. You can define action interface with some additional annotations to tell what is excepected to be send and received. They are using piriti library for json serialization.
It is up to you if you need only client side implementation or you would like to use server side service creation too.
You can NOT implement a REST service in gwt, since gwt is thought as a client-side solution.
What the GWT kit provides for server side are a few utilities to facilitate the comunication between client-side and server-side when both are written in java (RPC, RF).
So you can consume a REST service from gwt (RequestBuilder, gwtquery-ajax, etc), but if you want to provide REST services you need a 3party solution for your server side like Jersey, CXF, etc.
There are, though, 3party solutions which provides the server side and client simultaneously like restygwt, errai-jax, etc.
If you are looking for a simple and reliable solution to query rest services from the client, in this question you have a client implementation done with gwtquery (ajax, databinding, promises)