mockmvc加载重定向URL

mockmvc加载重定向URL

本文介绍了mockmvc加载重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Spring Rest Service编写测试,该测试将URL重定向到另一个spring服务.因此,目标是使用书签名称查找书签".第一个服务使用书签名称获取BookmarkId,然后根据BookmarkId重定向URL以加载对象

I am writing test for a Spring Rest Service which redirects the url to another spring service. So the goal is to find the Bookmark using a bookmark name. The first service gets the BookmarkId using the Bookmark name and then it redirects the url to load the object based on the BookmarkId

我可以轻松地使用来测试url重定向,以下工作正常

I can easily test the url redirect using, the below works fine

 mockMvc.perform(get("/bookmarks/name/" + "sample"))
                .andExpect(status().isMovedPermanently()).andExpect(redirectedUrl("/bookmarks/" + bookmark.getKey()));

我想要做的是下一步并调用下一个URL并加载书签对象,该怎么做?

what I want is to do the next step and call the next url and load the bookmark object, how do I do that ??

推荐答案

就像浏览器一样-通过请求您在Location标头中获得的URL来遵循重定向:

Just do what a web browser would do - follow the redirection by requesting the URL you get in Location header:

mockMvc.perform(get("/bookmarks/" + bookmark.getKey())).andExpect(...)

这是一个单独的测试用例.因此,一个TC应该测试您是否真正被重定向,而另一个TC测试bookmarks/{id}端点.

This is a separate testcase though. So one TC should test if you actually get redirected, and the other one that tests the bookmarks/{id} endpoint.

Spring Framework的bugtracker中有一个功能请求,允许MockMVC跟随重定向( SPR-14342改进MockMvc以遵循重定向和转发).它在今年初被拒绝,但有以下评论:

There is a feature request in Spring Framework's bugtracker to allow MockMVC to follow redirects (SPR-14342 Improve MockMvc to follow redirects and forwards). It's been rejected early this year though with the following comment:

这篇关于mockmvc加载重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:52