Spring MockMvc and @AutoConfigureMockMvc() set headers in request

mikeb

I'm using Spring boot and have Spring Security setup to use token authorization. I have my test is setup like so:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
})
@AutoConfigureMockMvc()
@EnableAutoConfiguration(exclude = {
})
public class ApplicationTests {
@Test
        public void shouldReturnRepositoryIndex() throws Exception {

            mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).andExpect(
                    jsonPath("$._links.people").exists());
        }
}

I need get("/") to send a header "X-AUTH: SOMETOKEN"

How do I do that?

Alex M981

.header("X-AUTH" , "SOMETOKEN") should work.

Here is the modified mockMvc code:

mockMvc.perform(get("/").header("X-AUTH" , "SOMETOKEN")).andDo(print()).andExpect(status().isOk()).andExpect(
            jsonPath("$._links.people").exists());

The output is:

MockHttpServletRequest:
  HTTP Method = GET
  Request URI = /
   Parameters = {}
      Headers = {X-AUTH=[SOMETOKEN]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Empty response headers from Spring MockMvc with springSecurityFilterChain

How do I set the request server name using Spring's MockMvc framework?

Spring Test MockMvc perform request on external URL

How to add a request body to spring mockmvc

Set headers for AJAX request to Django

Set headers for scrapy shell request

Set headers in JSON get request

Angular - Set headers for every request

How to set headers for forwarded request

need to set headers on guzzle request

HTTPS with NodeJS set request headers

spring gateway: how to dynamically set URI and path based on request headers and path

Spring MockMvc - How to test delete request of REST controller?

Can not attach body to my POST request using Spring MockMvc

Spring MockMvc Fails : No mapping found for HTTP request with URI

spring 4.1.1, mockmvc and do not want url encoding of HTTP GET request

Spring MockMvc: How to access Request Object while initializing test

Spring 5.0.0 MockMvc: Unsupported media type, even with correct MediaType set

How to set headers in post request? - Angular

Ajax cross domain request fail to set headers

Set custom headers to websocket request (ktor)

Why are Cookies not being set in request headers?

How to set headers in http get request?

Set authorization in Node request.headers

How to set Rest request Headers parameters in pydocumentdb

Set Cookie in Request Headers Angular2

CURL request headers seemingly not being set in PHP

Create custom annotation for request mapping headers spring

Spring fo Android : request authorization headers (not basic)