Skip to content

Provide easy way to add context path to sample curl request #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kpb opened this issue Sep 22, 2015 · 4 comments
Closed

Provide easy way to add context path to sample curl request #135

kpb opened this issue Sep 22, 2015 · 4 comments

Comments

@kpb
Copy link

kpb commented Sep 22, 2015

I have a project named 'myapp'. When I deploy to a web server, the context path is '/myapp'.

Requests to a Controller with a RequestMapping(value="/some/resource") will be accessed with:

$ curl http://example.com/myapp/some/resource ...

The generated snippet will create:

$ curl http://example.com/some/resource

which is only useful for apps deployed to the root context. I don't see an easy way to change this via config. (setting the contextPath on the MockMvc.perform() won't work, because the RequestMapping doesn't include the contextPath).

@wilkinsona
Copy link
Member

This is already possible. As described in the documentation, you should use contextPath on MockHttpServletRequestBuilder to configure the context path. I've just added a test in 5051429 that verifies the behaviour.

@kpb
Copy link
Author

kpb commented Sep 22, 2015

Don't you get a

java.lang.IllegalArgumentException: requestURI [/some/resource] does not start with contextPath [/context]

Not sure how this should work in practice.

@wilkinsona
Copy link
Member

Did you look at the test that I added? You need to provide a request URI that begins with the context path. Mock MVC is then smart enough to strip off the context path before it figures out which request mapping will handle the request, just as would happen in a real servlet container. In your case, you should set the context path to /myapp and use /myapp/some/resource as the request URI:

mockMvc.perform(get("/myapp/some/resource/").contextPath("/myapp"))
        .andExpect(status().isOk()).andDo(document("custom-context-path"));

You might also like to read the relevant section of the Spring MVC Test documentation.

@kpb
Copy link
Author

kpb commented Sep 22, 2015

Thanks. I did look at the tests you added. What I was missing was adding the context path to the url as well as via contextPath().

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants