java - 如何在 HttpServletRequest 中设置标头
问题描述
我正在为我的一个班级编写 Junit Test 班级,班级有 POST 休息方法。我需要将一些标头发送到 POST 方法。我正在使用 HttpServletRequest 发送请求。下面是我的测试类方法:
void setup () throws Exception {
LOGGER.info(" Setting up the test data....");
System.out.println(" Method setup : Start");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");
headers.put("cnt", "leader");
Enumeration<String> headerNames = Collections.enumeration(headers.keySet());
mockRequest = mock( HttpServletRequest.class );
when(mockRequest.getHeaderNames()).thenReturn(headerNames);
when(mockRequest.getHeaders("content-type")).thenReturn("application/json");
mockResponse = mock(HttpServletResponse.class);
mockResponse.setContentType("application/json");
try {
JsonReader jsonReader = Json.createReader(new StringReader(strContent));
jsonRequestObject = jsonReader.readObject();
jsonReader.close();
System.out.println(" jsonRequestObject : {} "+ jsonRequestObject);
myService.callMyService(mockRequest, jsonRequestObject);
} catch (Exception e) {
throw e;
}
}
解决方案
推荐阅读
- postgresql - 如何读取查询参数列表
- go - Golang如何删除静态服务的最后一个斜杠
- oracle - Kafka-connect 无法连接到 oracle 数据库
- python-3.x - 预定义每小时采样的记录数的示例数据框
- forms - “App\Entity\CategoryImage”、“array”类型的预期参数在属性路径“images”处给出
- python - 将循环输出中的所有数字相乘
- install4j - install4j 8 和 mac 公证
- python - Remove an item from a list if it only contains one word
- amazon-web-services - aws - 如何全局设置 aws 编辑器字体大小?
- python - 如果有偶数个项目,如何找到列表的中间值并返回较高的值