How to change host in curl snippet in Spring REST Docs
Asked Answered
D

2

7

Spring REST Docs produces a curl snippet which is very convenient when testing. It is equivalent to the MockMvc call as said in its docs but it would be good if its host part can be replaced with a test server's hostname (including port) instead of localhost. Is it possible to achieve it with the current version of it?

Desirae answered 31/10, 2015 at 6:59 Comment(0)
S
17

You can configure the host (and scheme and port) when you create the MockMvc instance:

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
        .apply(documentationConfiguration(this.restDocumentation).uris()
                .withScheme("https")
                .withHost("example.com")
                .withPort(443))
        .build();
Stative answered 31/10, 2015 at 7:48 Comment(1)
Thanks! I missed it already documented in docs.spring.io/spring-restdocs/docs/1.0.0.BUILD-SNAPSHOT/…Desirae
K
8

Using SpringBoot and autoconfiguration it could be:

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc    
@AutoConfigureRestDocs(uriScheme = "https", uriHost = "myhost", uriPort = "80")
public class Test { 

    @Autowired
    MockMvc mockMvc;

    ...
Kielty answered 21/6, 2017 at 19:10 Comment(1)
For some reason it doesn't worked for me, while manual configuration do the trick.Maddie

© 2022 - 2024 — McMap. All rights reserved.