mardi 29 décembre 2015

Spring Test MockMvc perform request on external URL

I'm trying to perform a POST request on URL outside the current context, and it looks like Spring cannot understand it.

Test code:

        String content = mvc
            .perform(post("http://ift.tt/1R6P7HB)
                    .header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    .param("username", username)
                    .param("password", password)
            .andExpect(status().isOk())
            .andReturn().getResponse().getContentAsString();

Works like a charm on current context. But remote service cannot be reached at all. It seems like "http://ift.tt/1R6P7HD" part is ignored.

Mock creation code:

mvc = MockMvcBuilders.webAppContextSetup(context).build();

Is there any way to make it work? Because using standard Java HttpUrlConnection class is a huge pain.

Aucun commentaire:

Enregistrer un commentaire