Skip to content
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

当请求 Header 中引用 Long 类型变量时,出现类型转换错误 #10

Open
ly1012 opened this issue Mar 21, 2022 · 0 comments
Open

Comments

@ly1012
Copy link

ly1012 commented Mar 21, 2022

测试用例 login.xml 如下:

config:
  name: 登录测试用例
  base_url: https://reqres.in
teststeps:
  - name: 获取Token信息
    base_url: https://reqres.in
    variables:
      password: "${2+3}"
    request:
      url: /api/login
      method: POST
      headers:
        contentType: "application/json"
        password: "${password}"
      json:
        email: "[email protected]"
        password: "${password}"
    validate:
      - equalTo: [status_code,200]
    extract:
      token: body.token

报错信息:

...
2022-03-21 21:28:33.329 [main] INFO  vip.lematech.hrun4j.helper.LogHelper - 当前步骤:获取Token信息 
2022-03-21 21:32:45.730 [main] INFO  vip.lematech.hrun4j.helper.LogHelper - 请求地址:https://reqres.in/api/login 
2022-03-21 21:32:45.732 [main] INFO  vip.lematech.hrun4j.helper.LogHelper - 请求类型:POST 
2022-03-21 21:32:47.088 [main] INFO  vip.lematech.hrun4j.helper.LogHelper - 请求头:{password=5, contentType=application/json} 
2022-03-21 21:32:55.875 [main] ERROR vip.lematech.hrun4j.helper.LogHelper - Unknown exception occurred in test case  execution. Exception information:java.lang.Long cannot be cast to java.lang.String 



vip.lematech.hrun4j.common.DefinedException
	at vip.lematech.hrun4j.core.runner.TestCaseRunner.execute(TestCaseRunner.java:122)
	at com.liyun.qa.edu.hrun4j.testcases.GetStartedTest.login(GetStartedTest.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

初步分析:headers 字段 Value 存入时使用的是 Object 类型,取用时使用的是 String 类型,存在类型转换风险。

  1. RequestEntity 类中 headers 字段声明为如下类型: private Map<String, Object> headers;,其中 Value 为 Object 类型。
  2. ExpProcessor#dynHandleContainsExpObject(T t) 方法在解析 Header Value 中的表达式时赋值代码如下:
    public <T> T dynHandleContainsExpObject(T t) {
              ...
                Object handledValue = handleStringExp(value);
               // headers 字段存入时,Value 为 Object 类型,本案例中为 Long 类型
                result.put(key, handledValue);
              ...
    }
  1. OkHttpsHelper#initRequestParameter 中取值代码逻辑如下:
        if (MapUtil.isNotEmpty(headers)) {
            LogHelper.info(String.format(I18NFactory.getLocaleMessage("request.header"), headers));
            if (headers.containsKey(Constant.HEADER_COOKIE)) {
                LogHelper.info(String.format(I18NFactory.getLocaleMessage("request.cookie"), LittleHelper.emptyIfNull(requestEntity.getCookies())));
            }
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                String key = entry.getKey();
                //这里取值时存在类型转换风险,本案例中 entry.getValue() 返回的是 Long 类型
                String value = entry.getValue();
                headers.put(key, getValueEncoded(value));
            }
            syncHttpTask.addHeader(headers);
        }
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

1 participant