Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change host in curl snippet in Spring REST Docs

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?

like image 396
Johnny Lim Avatar asked Oct 31 '15 06:10

Johnny Lim


Video Answer


2 Answers

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();
like image 191
Andy Wilkinson Avatar answered Oct 02 '22 14:10

Andy Wilkinson


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;

    ...
like image 39
Max Farsikov Avatar answered Oct 02 '22 15:10

Max Farsikov