سؤال

I am writing a Java based Web application, which, in the actual production environment would be front-ended by another application which would set certain HTTP request headers before the request hits my application.

However, in the development environment I do not have the front-ending application, for which I need to create a mock web application that simulates the same behavior. i.e. this mock application should set the request headers and redirect or forward or whatever that I do not know :) to a certain page in my application.

How can I accomplish this?

هل كانت مفيدة؟

المحلول

The following articles may help you:


P.S.
I am sorry I provided only links, that was one of my early answer on SO ))

نصائح أخرى

In case you don't want to modify your code as suggested by @user1979427 you can use a proxy server to modify headers or add headers on the fly.

For example in Apache HTTPD you would add something like below and proxy the

Header add HEADER "HEADERVALUE"
RequestHeader set HEADER "HEADERVALUE"   

Refer to HTTPD doc

You should create a AddReqHeaderForFrowardWrapper request wrapper passing the headername and header values. And, override the request header related methods to return your custom header.

You can use Tracer to implement this. There are frameworks available to support this implementation. Spring has Sleuth, Zipkin, OpenTracing available. I find OpenTracing to be easy to use without worrying about dependency conflicts. Read more about it here: https://opentracing.io/guides/java/

Instead of writing a mock application, I used a browser add-on that allowed me to add custom headers!

For setting header in java, you can use:

request.setHeader(attributeName, attributeValue);

And for redirecting to another page, you can use:

request.sendRedirect(URL);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top