这篇文章没什么技术含量,只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅。

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AccountControllerTests {
@Autowired
private TestRestTemplate restTemplate;
private HttpEntity httpEntity; /**
* 登录
* @throws Exception
*/
private void login() throws Exception {
String expectStr = "{\"code\":0,\"msg\":\"success\"}";
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", "183xxxxxxxx");
map.add("password", "123456");
ResponseEntity responseEntity = restTemplate.postForEntity("/api/account/sign_in", map, String.class);
//添加cookie以保持状态
HttpHeaders headers = new HttpHeaders();
String headerValue = responseEntity.getHeaders().get("Set-Cookie").toString().replace("[", "");
headerValue = headerValue.replace("]", "");
headers.set("Cookie", headerValue);
httpEntity = new HttpEntity(headers);
assertThat(responseEntity.getBody()).isEqualTo(expectStr);
} /**
* 登出
* @throws Exception
*/
private void logout() throws Exception {
String expectStr = "{\"code\":0,\"msg\":\"success\"}";
String result = restTemplate.postForObject("/api/account/sign_out", null, String.class, httpEntity);
httpEntity = null;
assertThat(result).isEqualTo(expectStr);
} /**
* 获取信息
* @throws Exception
*/
private void getUserInfo() throws Exception {
Detail detail = new Detail();
detail.setNickname("疯狂的米老鼠");
detail.setNicknamePinyin("fengkuangdemilaoshu");
detail.setSex(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
detail.setCreatedAt(sdf.parse("2017-11-03 16:43:27"));
detail.setUpdatedAt(sdf.parse("2017-11-03 16:43:27"));
Role role = new Role();
role.setName("ROLE_USER_NORMAL");
Set<Role> roles = new HashSet<>();
roles.add(role);
User user = new User();
user.setId(1L);
user.setPhone("183xxxxxxxx");
user.setEmail("xxxxxx@gmail.com");
user.setDetail(detail);
user.setRoles(roles);
ResultBean<User> resultBean = new ResultBean<>();
resultBean.setData(user);
ObjectMapper om = new ObjectMapper();
String expectStr = om.writeValueAsString(resultBean);
ResponseEntity<String> responseEntity = restTemplate.exchange("/api/user/get_user_info", HttpMethod.GET, httpEntity, String.class);
assertThat(responseEntity.getBody()).isEqualTo(expectStr);
} @Test
public void testAccount() throws Exception {
login();
getUserInfo();
logout();
}

  

spring boot使用TestRestTemplate集成测试 RESTful 接口的更多相关文章

  1. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

  2. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  3. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  4. Spring Boot的TestRestTemplate使用

    文章目录 添加maven依赖 TestRestTemplate VS RestTemplate 使用Basic Auth Credentials 使用HttpClientOption 使用RestTe ...

  5. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  6. Spring Boot 2.x 编写 RESTful API (三) 程序层次 & 数据传输

    用Spring Boot编写RESTful API 学习笔记 程序的层次结构 相邻层级的数据传输 JavaBean 有一个 public 的无参构造方法 属性 private,且可以通过 get.se ...

  7. Spring Boot 2.x 编写 RESTful API (二) 校验

    用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...

  8. 使用spring boot+mybatis+mysql 构建RESTful Service

    开发目标 开发两个RESTful Service Method Url Description GET /article/findAll POST /article/insert 主要使用到的技术 j ...

  9. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

随机推荐

  1. setting-url配置

    参考:  diango1 一.  mvc和mvt模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负 ...

  2. jav实验二

    实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验内容 1.参考Intellj ID ...

  3. spring cloud (一、服务注册demo_eureka)

    首先我的博客记理论知识很少,大家对spring boot.spring cloud  .分布式 .微服务什么的一点概念都没有的还请先去百度看看理论,知道了是做什么用的,然后再写下demo ,这样学起来 ...

  4. Machine Learning 算法可视化实现1 - 线性回归

    一.原理和概念 1.回归 回归最简单的定义是,给出一个点集D,用一个函数去拟合这个点集.而且使得点集与拟合函数间的误差最小,假设这个函数曲线是一条直线,那就被称为线性回归:假设曲线是一条二次曲线,就被 ...

  5. 关于HttpModule和HttpHandler以及HttpApplication

    HttpRuntime打交道的是http协议跟IIS层面的东西,HttpApplication则具体到应用程序这一级别(也就是一个网站,这个跟web.config关系是基本一一对应的,像Module跟 ...

  6. 使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)

    使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization) 前言: 这是 qt for python 的语言国际化,基于 UI 的,python 也有 ...

  7. poi设置打印页页脚和页数设置

    之前在网上搜了很久,也没有搜到具体页脚页数的答案,最后还是在官方api文档上找到了答案: HSSFPrintSetup printSetup = (HSSFPrintSetup) sheet.getP ...

  8. 用jquery设置的值,miniui.getData取不到

    用jquery设置的值,miniui.getData取不到

  9. Codeforces 992C Nastya and a Wardrobe (思维)

    <题目链接> 题目大意: 你开始有X个裙子 你有K+1次增长机会 前K次会100%的增长一倍 但是增长后有50%的机会会减少一个 给你X,K(0<=X,K<=1e18), 问你 ...

  10. hadoop安装过程中出现的错误

    此次来记录一下我在安装Hadoop安装过程中出现的错误,安装过程参照慕课网林子雨教程进行安装,在尝试过程中出现的错误如下: 1.在安装Ubuntu时,新建虚拟电脑时,并没有在版本的输入框中有Ubunt ...