Skip to content

Commit

Permalink
feat: rename http header to headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 6, 2021
1 parent f0b3066 commit e58284c
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 56 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Write test file `httpbin.jsona`
req: {
url: "https://httpbin.org/post",
method: "post",
header: {
headers: {
'content-type':'application/json',
},
body: {
Expand Down Expand Up @@ -231,7 +231,7 @@ The following test cases can use all the data of the previous test cases.
},
test2: { @describe("create article")
req: {
header: {
headers: {
// We access the response data of the previous test case test1.
authorization: `"Bearer " + test1.res.body.token`, @eval
},
Expand Down Expand Up @@ -386,7 +386,7 @@ First create a file to store the file defined by Mixin
},
auth1: { // Extract authorization
req: {
header: {
headers: {
authorization: `"Bearer " + test1.res.body.token`, @eval
}
}
Expand Down Expand Up @@ -864,15 +864,15 @@ The `http` client handles http/https requests/responses.
params: {
id: 33,
},
header: {
headers: {
'x-key': 'v1'
},
body: { // request body
}
},
res: {
status: 200,
header: {
headers: {
'x-key': 'v1'
},
body: { // response body
Expand All @@ -888,8 +888,6 @@ The `http` client handles http/https requests/responses.
```js
{
// `baseURL` will be prepended to `url` unless `url` is absolute.
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
// to methods of that instance.
baseURL: 'https://some-domain.com/api/',
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.
Expand Down
20 changes: 8 additions & 12 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Apitest工具是单可执行文件,不需要安装,放到`PATH`路径下面
req: {
url: "https://httpbin.org/post",
method: "post",
header: {
headers: {
'content-type': 'application/json',
},
body: {
Expand Down Expand Up @@ -230,7 +230,7 @@ Apitest 的工作原理就是根据`req`部分的描述构造请求传给后端
},
test2: { @describe("发布文章")
req: {
header: {
headers: {
authorization: `"Bearer " + test1.res.body.token`, @eval // 此处访问了前面测试用例 test1 的响应数据
},
}
Expand Down Expand Up @@ -383,7 +383,7 @@ exports.isDate = function (date) {
},
auth1: { // 抽离鉴权到minxin
req: {
header: {
headers: {
authorization: `"Bearer " + test1.res.body.token`, @eval
}
}
Expand Down Expand Up @@ -880,15 +880,15 @@ Apitest 提供两种客户端。
params: {
id: 33, // 路径占位变量 `/anything/{id}` => `/anything/33`
},
header: {
headers: {
'x-key': 'v1'
},
body: { // 请求数据
}
},
res: {
status: 200, // 状态码
header: {
headers: {
'x-key': 'v1'
},
body: { // 响应数据
Expand All @@ -902,15 +902,11 @@ Apitest 提供两种客户端。

```js
{
// `baseURL` will be prepended to `url` unless `url` is absolute.
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
// to methods of that instance.
// `baseURL` 相对路径
baseURL: 'https://some-domain.com/api/',
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.
// `timeout` 指定请求超时前的毫秒数。 如果请求时间超过`timeout`,请求将被中止。
timeout: 1000, // default is `0` (no timeout)
// `withCredentials` indicates whether or not cross-site Access-Control requests
// should be made using credentials
// `withCredentials` 表示是否跨站访问控制请求
withCredentials: false, // default
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin.jsona
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
req: {
url: "https://httpbin.org/post",
method: "post",
header: {
headers: {
'content-type':'application/json',
},
body: {
Expand Down
4 changes: 2 additions & 2 deletions examples/realworld/auth.jsona
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
},
curentUser: { @describe("Current User") @mixin(["getUser", "userRes"])
req: {
header: {
headers: {
Authorization: `"Token " + login.res.body.user.token` @eval
}
},
},
updateUser: { @describe("Update User") @mixin(["updateUser", "userRes"])
req: {
header: {
headers: {
Authorization: `"Token " + login.res.body.user.token` @eval
},
body: {
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld/mixin.jsona
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
},
auth1: {
req: {
header: {
headers: {
Authorization: `"Token " + auth.login.res.body.user.token` @eval
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/Clients/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ export default class HttpClient implements Client {
if (req.query) {
opts.params = req.query;
}
if (req.header) {
opts.headers = req.header;
if (req.headers) {
opts.headers = req.headers;
}
if (req.params) {
for (const key in req.params) {
opts.url = opts.url.replace(new RegExp(`{${key}}`, "g"), req.params[key]);
}
}
if (req.body) {
if (!(req.header && (req.header["content-type"] || req.header["Content-Type"]))) {
if (!(req.headers && (req.headers["content-type"] || req.headers["Content-Type"]))) {
_.set(opts, ["headers", "content-type"], "application/json; charset=utf-8");
}
opts.data = req.body;
Expand All @@ -52,17 +52,17 @@ export default class HttpClient implements Client {
let needStatus = false;
if (unit.res) {
const res_ = unit.res as JsonaObject;
needHeader = !!res_.properties.find(v => v.key === "header");
needHeader = !!res_.properties.find(v => v.key === "headers");
needStatus = !!res_.properties.find(v => v.key === "status");
}
try {
const axiosRes = await axios(opts);
if (needHeader) result.header = axiosRes.headers;
if (needHeader) result.headers = axiosRes.headers;
if (needStatus) result.status = axiosRes.status;
result.body = axiosRes.data;
} catch (err) {
if (err.response) {
if (needHeader) result.header = err.response.headers;
if (needHeader) result.headers = err.response.headers;
if (needStatus) result.status = err.response.status;
result.body = err.response.data;
} else {
Expand All @@ -87,8 +87,8 @@ export default class HttpClient implements Client {
},
{ paths: ["params"], type: "Object" },
{ paths: ["params", "*"], type: "Scalar", required: true },
{ paths: ["header"], type: "Object" },
{ paths: ["header", "*"], type: "Scalar", required: true },
{ paths: ["headers"], type: "Object" },
{ paths: ["headers", "*"], type: "Scalar", required: true },
{ paths: ["query"], type: "Object" },
{ paths: ["query", "*"], type: "Scalar", required: true },
]);
Expand Down Expand Up @@ -120,8 +120,8 @@ export default class HttpClient implements Client {
checkValue(paths, res, [
{ paths: [], type: "Object", required: true },
{ paths: ["status"], type: "Integer" },
{ paths: ["header"], type: "Object" },
{ paths: ["header", "*"], type: "Scalar", required: true },
{ paths: ["headers"], type: "Object" },
{ paths: ["headers", "*"], type: "Scalar", required: true },
]);
}
}
16 changes: 8 additions & 8 deletions tests/__snapshots__/http.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ exports[`http invaid params type 1`] = `
"
`;

exports[`http invalid header prop type 1`] = `
"main.test1.req.header.key1: should be scalar value at line 6 col 15
exports[`http invalid headers prop type 1`] = `
"main.test1.req.headers.key1: should be scalar value at line 6 col 15
"
`;

exports[`http invalid header type 1`] = `
"main.test1.req.header: should be object value at line 5 col 15
exports[`http invalid headers type 1`] = `
"main.test1.req.headers: should be object value at line 5 col 16
"
`;

Expand Down Expand Up @@ -55,13 +55,13 @@ exports[`http invalid req value 1`] = `
"
`;

exports[`http invalid res header prop type 1`] = `
"main.test1.res.header.key: should be scalar value at line 8 col 14
exports[`http invalid res headers prop type 1`] = `
"main.test1.res.headers.key: should be scalar value at line 8 col 14
"
`;

exports[`http invalid res header type 1`] = `
"main.test1.res.header: should be object value at line 7 col 15
exports[`http invalid res headers type 1`] = `
"main.test1.res.headers: should be object value at line 7 col 16
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
test1: {
req: {
url: "https://httpbin.org/get",
header: {
headers: {
key1: {}
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
test1: {
req: {
url: "https://httpbin.org/get",
header: [],
headers: [],
}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
url: "https://httpbin.org/get",
},
res: {
header: {
headers: {
key: {}
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
url: "https://httpbin.org/get",
},
res: {
header: [],
headers: [],
}
},
}
4 changes: 2 additions & 2 deletions tests/fixtures/http/main.jsona
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
params: {
id: 33,
},
header: {
headers: {
'x-key': 'v1'
},
body: {
Expand All @@ -43,7 +43,7 @@
},
res: {
status: 200,
header: { @partial
headers: { @partial
},
body: { @partial
json: {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/http/no-check-trans.jsona
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
req: {
url: {}, @trans(`"https://httpbin.org/anything/{id}"`)
method: {}, @trans(`"post"`)
header:{
headers:{
'xkey': {}, @trans(`"abc"`)
},
params:{
Expand Down
16 changes: 8 additions & 8 deletions tests/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ describe("http", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("invalid header prop type", async () => {
const { stdout, code } = await spwanTest("http/invalid-header-prop-type.jsona", ["--ci"]);
test("invalid headers prop type", async () => {
const { stdout, code } = await spwanTest("http/invalid-headers-prop-type.jsona", ["--ci"]);
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("invalid header type", async () => {
const { stdout, code } = await spwanTest("http/invalid-header-type.jsona", ["--ci"]);
test("invalid headers type", async () => {
const { stdout, code } = await spwanTest("http/invalid-headers-type.jsona", ["--ci"]);
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
Expand Down Expand Up @@ -56,13 +56,13 @@ describe("http", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("invalid res header prop type", async () => {
const { stdout, code } = await spwanTest("http/invalid-res-header-prop-type.jsona", ["--ci"]);
test("invalid res headers prop type", async () => {
const { stdout, code } = await spwanTest("http/invalid-res-headers-prop-type.jsona", ["--ci"]);
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("invalid res header type", async () => {
const { stdout, code } = await spwanTest("http/invalid-res-header-type.jsona", ["--ci"]);
test("invalid res headers type", async () => {
const { stdout, code } = await spwanTest("http/invalid-res-headers-type.jsona", ["--ci"]);
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
Expand Down

0 comments on commit e58284c

Please sign in to comment.