Creating a simple http header for a Junit test

I am trying to check the HttpServletRequest , and for this I used Mockito as follows:

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

now before putting the http request into the assert methods, I just want to create a simple HTTP header, as shown below, without starting the 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 anyone help how I can build such a test header? thanks.

+6
source share
1 answer

You can simply drown out calls to request.getHeaders, etc., or if you can add a dependency, Spring-test has a MockHttpServletRequest that you can use (see here )

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

Or you can create your own implementation that allows you to set headers.

+8
source

Source: https://habr.com/ru/post/954149/


All Articles