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

有的接口需要携带 token 才能成功,如何测试? #1

Closed
dkvirus opened this issue Jan 7, 2019 · 1 comment
Closed

有的接口需要携带 token 才能成功,如何测试? #1

dkvirus opened this issue Jan 7, 2019 · 1 comment
Labels
requirement Functions to be implemented

Comments

@dkvirus
Copy link
Owner

dkvirus commented Jan 7, 2019

  • 这就需要先登录;
  • 拿到返回的 token,每个项目返回的数据结构也不尽相同,因此没法统一拿 token;
  • token 作为统一的 header 值,往后每个请求都要携带。
@dkvirus dkvirus added the requirement Functions to be implemented label Jan 7, 2019
@dkvirus dkvirus closed this as completed in e6f7652 Jan 8, 2019
@dkvirus
Copy link
Owner Author

dkvirus commented Jan 8, 2019

需要携带 token 的,配置文件导出函数。

由于获取 token 的接口与返回值都是不可控的,这部分交给用户自己处理,最终将获得的 token 值塞给 headers 属性即可,tein 内部会处理每个请求都会带上这个 token 的。

module.exports = async function () {
  const result = await axios({
    url: "http://localhost:8081/api/v1/oauth/token",
    method: "post",
    data: qs.stringify({
      grant_type: "password",
      username: "dk",
      password: "admin"
    }),
    headers: {
      Authorization: "Basic c3dhZ2dlcxpzd2FnZ2Vy",
      "Content-Type": "application/x-www-form-urlencoded"
    }
  });

  // 拿到 token
  const { access_token } = result.data;

  return {
    apiPrefix: "http://localhost:8081/api/v1",
    headers: {
      Authorization: `Bearer ${access_token}`
    },
    apis: [
      {
        req: {
          url: "/user"
        },
        res: {
          code: String,
          message: String,
          data: {
            content: [
              {
                name: String,
                desc: String
              }
            ]
          }
        }
      }
    ]
  };
};

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

No branches or pull requests

1 participant