Wednesday 15 April 2015

Empty response headers from Spring MockMvc with springSecurityFilterChain -



Empty response headers from Spring MockMvc with springSecurityFilterChain -

i write unit tests spring mvc application.

this mockmvc initialization code:

@configuration public class springmockfactory { @autowired private webapplicationcontext wac; @autowired private filterchainproxy springsecurityfilterchain; @bean @scope("prototype") public mockmvc getmockmvc() { homecoming webappcontextsetup(wac) .addfilter(springsecurityfilterchain) .build(); } }

everything works good, when add together springsecurityfilterchain response headers empty (it's of import cause want check set-cookie header).

i utilize code perform action:

resultactions = mockmvc.perform(post("/api/login") .contenttype(mediatype.application_form_urlencoded) .param("username", "sysadmin") .param("password", "password") ).anddo(print());

and result of anddo(print()):

mockhttpservletresponse: status = 200 error message = null headers = {} content type = null body = {"status":200} forwarded url = null redirected url = null cookies = []

the question why response headers empty when utilize springsecurityfilterchain mockmvc? , how read them?

you may need add together spring security configuration mockmvc object.

example:

mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext).apply(springsecurity()).build();

the springsecurity() initializer available in org.springframework.security.test.web.servlet.setup.securitymockmvcconfigurers package. can conveniently static import it. note need org.springframework.security:spring-security-test dependency (i 4.0.1.release).

see illustration here (didactic project).

i didn't need manually spring security filter chain.

spring spring-mvc spring-security spring-test

No comments:

Post a Comment