Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock http header in MockRestServiceServer?

I'm using MockRestServiceServer to mock an external webservice xml response. That already works fine, but how can I also mock the http header inside the response, not only the response body?

    @MockBean
    private RestTemplate restTemplate;

    private MockRestServiceServer mockServer;

    @Before
    public void createServer() throws Exception {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void test() {
        String xml = loadFromFile("productsResponse.xml");
        mockServer.expect(MockRestRequestMatchers.anything()).andRespond(MockRestResponseCreators.withSuccess(xml, MediaType.APPLICATION_XML));
    }
like image 292
membersound Avatar asked Dec 08 '17 14:12

membersound


1 Answers

Just follow your withSuccess method with headers method.

mockServer
       .expect(...)
       .andRespond(withSuccess().headers(...));
like image 148
Gorazd Rebolj Avatar answered Sep 30 '22 15:09

Gorazd Rebolj