Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building simple http-header for Junit test

I'm trying to test a HttpServletRequest and for that I have used Mockito as follow:

HttpServletRequest  mockedRequest = Mockito.mock(HttpServletRequest.class);

now before putting the http-request in assert methods I just want to build a simple http header as below without starting a real server:

x-real-ip:127.0.0.1
host:example.com
x-forwarded-for:127.0.0.1
accept-language:en-US,en;q=0.8
cookie:JSESSIONID=<session_ID>

can some one help how can I build such a test header? thanks.

like image 398
tokhi Avatar asked Sep 19 '13 08:09

tokhi


1 Answers

You can just stub the calls to request.getHeaders etc. or if you can add a dependency, Spring-test has a MockHttpServletRequest that you could use (see here)

MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("x-real-ip","127.0.0.1");

Or you could build your own implementation which allows you to set headers.

like image 148
blank Avatar answered Nov 02 '22 02:11

blank