Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit 9263aee

Browse files
committed
v1.0.0
1 parent 8130895 commit 9263aee

File tree

10 files changed

+806
-2
lines changed

10 files changed

+806
-2
lines changed

README.md

+340-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,340 @@
1-
# qiniutest
2-
Qiniu httptest tool: qiniutest
1+
qiniutest manual
2+
=========================
3+
4+
[![Build Status](https://travis-ci.org/qiniu/qiniutest.svg?branch=develop)](https://travis-ci.org/qiniu/qiniutest) [![GoDoc](https://godoc.org/qiniupkg.com/qiniutest?status.svg)](https://godoc.org/qiniupkg.com/qiniutest)
5+
6+
[![Qiniu Logo](http://open.qiniudn.com/logo.png)](http://www.qiniu.com/)
7+
8+
# 下载
9+
10+
```
11+
go get -u qiniupkg.com/qiniutest
12+
```
13+
14+
# 基础原理
15+
16+
* [httptest.v1/README.md](https://github.com/qiniu/httptest.v1)
17+
18+
# 命令行
19+
20+
```bash
21+
qiniutest -v <QiniutestFile.qtf>
22+
```
23+
24+
由 qiniutest 程序解释并执行 QTF 文件描述的测试脚本。指定 `-v` 参数表示 verbose,会获得更多的调试信息输出。
25+
26+
如果我们在 QTF 文件开头加上这样一行:
27+
28+
```bash
29+
#!/usr/bin/env qiniutest
30+
```
31+
32+
并把 `<QiniutestFile.qtf>` 文件设置为可执行:
33+
34+
```bash
35+
chmod +x <QiniutestFile.qtf>
36+
```
37+
38+
如此就可以直接运行它:
39+
40+
```bash
41+
./<QiniutestFile.qtf>
42+
```
43+
44+
# QTF 语言手册
45+
46+
## 命令
47+
48+
### match
49+
50+
匹配两个object。语法:
51+
52+
```bash
53+
match <expected-object> <source-object>
54+
```
55+
56+
关于 match 的详细解释,参见:
57+
58+
* [httptest.v1/README.md](https://github.com/qiniu/httptest.v1)
59+
60+
### host
61+
62+
样例1:使用环境变量来选择 stage 还是 product
63+
64+
```bash
65+
match $(testenv) `env QiniuTestEnv`
66+
match $(env) `envdecode QiniuTestEnv_$(testenv)`
67+
68+
host rs.qiniu.com $(env.RSHost)
69+
```
70+
71+
这样,测试人员只需要配置环境:
72+
73+
```bash
74+
export QiniuTestEnv_stage='{
75+
"RSHost": "192.168.1.10:9999",
76+
"AK": "...",
77+
"SK": "..."
78+
}'
79+
80+
export QiniuTestEnv_product='{
81+
"RSHost": "rs.qiniu.com",
82+
"AK": "...",
83+
"SK": "..."
84+
}'
85+
```
86+
87+
然后:
88+
89+
```bash
90+
QiniuTestEnv=stage qiniutest <QiniutestFile.qtf> # 测试stage环境
91+
QiniuTestEnv=product qiniutest <QiniutestFile.qtf> # 测试product环境
92+
```
93+
94+
### auth
95+
96+
定义 auth 信息:
97+
98+
```bash
99+
auth <auth-name> <auth-interface>
100+
```
101+
102+
在某次请求时引用 auth:
103+
104+
```bash
105+
# 这里的 <auth-information> 可以是之前已经定义好的 <auth-name>,也可以直接是某个 <auth-interface>
106+
auth <auth-information>
107+
```
108+
109+
auth 信息通常用 AccessKey/SecretKey,或者 Username/Password,都是很敏感的信息,一般通过 env 传入,避免随着脚本入库。
110+
111+
样例1:
112+
113+
```bash
114+
match $(testenv) `env QiniuTestEnv`
115+
match $(env) `envdecode QiniuTestEnv_$(testenv)`
116+
117+
host rs.qiniu.com $(env.RSHost)
118+
auth qboxtest `qbox $(env.AK) $(env.SK)`
119+
120+
post http://rs.qiniu.com/stat/`base64 testqiniu:ecug-2014-place.png`
121+
auth qboxtest
122+
ret 200
123+
echo $(resp)
124+
```
125+
126+
它等价于:
127+
128+
```bash
129+
match $(testenv) `env QiniuTestEnv`
130+
match $(env) `envdecode QiniuTestEnv_$(testenv)`
131+
132+
host rs.qiniu.com $(env.RSHost)
133+
134+
post http://rs.qiniu.com/stat/`base64 testqiniu:ecug-2014-place.png`
135+
auth `qbox $(env.AK) $(env.SK)`
136+
ret 200
137+
echo $(resp)
138+
```
139+
140+
### echo/println
141+
142+
echo/println功能相同,都用于调试、打印信息。语法:
143+
144+
```bash
145+
echo <object1> <object2> ...
146+
```
147+
148+
### req/post/get/delete/put
149+
150+
req 发起一个请求:
151+
152+
```bash
153+
req <http-method> <url>
154+
```
155+
156+
而 post/get/delete/put 是 `http-method` 分别为 POST/GET/DELETE/PUT 时的简写。如:
157+
158+
```bash
159+
post <url>
160+
```
161+
162+
### header
163+
164+
用于指定请求包或返回包的某个头部信息。语法:
165+
166+
```bash
167+
header <key> <value1> <value2> ...
168+
```
169+
170+
需要注意的是,在返回包匹配时,语句:
171+
172+
```bash
173+
header Content-Type $(mime)
174+
```
175+
176+
如果 $(resp.header.Content-Type) 是 ["application/json"],那么得到的 $(mime) 并不是 ["application/json"],而是 "application/json"。如果希望是 ["application/json"],则应该这样写:
177+
178+
```bash
179+
match $(mime) $(resp.header.Content-Type)
180+
```
181+
182+
### body/json/form/text/binary
183+
184+
body 用于指定请求包或返回包的正文内容。语法:
185+
186+
```bash
187+
body <content-type> <content-data>
188+
```
189+
190+
其中 `<content-type>` 可以是 "application/json"、"application/text" 这样的全称,也可以简写为 "json"、"form"、"text"。
191+
192+
而 json/form 指令是 `<content-type>` 为 json/form 时的简写。如:
193+
194+
```bash
195+
json <json-data>
196+
```
197+
198+
等价于:
199+
200+
```bash
201+
body json <json-data>
202+
```
203+
204+
而 binary 指令是 `<content-type>` 为 "application/octet-stream" 的简写。如:
205+
206+
```bash
207+
binary <binary-data>
208+
```
209+
210+
等价于:
211+
212+
```bash
213+
body application/octet-stream <binary-data>
214+
```
215+
216+
### ret
217+
218+
ret 用来获得返回包。语法:
219+
220+
```bash
221+
ret [<status-code>]
222+
```
223+
224+
在指定了 `<status-code>` 时,会要求返回的 $(resp.code) 与该 status code 相符,否则测试失败。语句:
225+
226+
```bash
227+
ret <status-code>
228+
```
229+
230+
等价于:
231+
232+
```bash
233+
ret
234+
match <status-code> $(resp.code)
235+
```
236+
237+
### clear
238+
239+
clear 用来清除已经绑定的变量。语法:
240+
241+
```bash
242+
clear <var-name1> <var-name2> ...
243+
```
244+
245+
### let
246+
247+
let 用于变量赋值,和主流命令式编程语言的 `=` 最为接近。例如:
248+
249+
```bash
250+
let $(var-name) <expression>
251+
```
252+
253+
等价于:
254+
255+
```bash
256+
clear var-name
257+
match $(var-name) <expression>
258+
```
259+
260+
### equal/equalSet
261+
262+
equal 要求两个 object 的内容精确相等:
263+
264+
```bash
265+
equal <object1> <object2>
266+
```
267+
268+
equalSet 要求两个 object 都是 array,并且把两个 array 看作集合,要求两个集合精确相等:
269+
270+
```bash
271+
equalSet <array-object1> <array-object2>
272+
```
273+
274+
也就是两个 array 的元素排序后应该精确相同。
275+
276+
## 子命令
277+
278+
### base64
279+
280+
```bash
281+
base64 -d -std <text>
282+
```
283+
284+
对一段文本进行 base64 编码(encode)。如果指定了 `-d` 参数则为解码(decode)。如果指定了 `-std` 则使用 base64.StdEncoding(默认使用的是 UrlSafe 的 base64.URLEncoding)进行编解码。
285+
286+
### env
287+
288+
```bash
289+
env <var-name>
290+
```
291+
292+
取得环境变量对应的文本。
293+
294+
### decode
295+
296+
```bash
297+
decode <text>
298+
```
299+
300+
对一段 json 文本进行解码(decode)。
301+
302+
### envdecode
303+
304+
```bash
305+
envdecode <var-name>
306+
```
307+
308+
取得一个环境变量对应的文本,并且进行解码(json decode)。等价于:
309+
310+
```bash
311+
match $(__auto_var1) `env <var-name>`
312+
decode $(__auto_var1)
313+
```
314+
315+
### qbox
316+
317+
```bash
318+
qbox <AccessKey> <SecretKey>
319+
```
320+
321+
返回由七牛云存储 qbox 规范定义的 `auth interface`,可被 `auth` 命令使用。如:
322+
323+
```bash
324+
auth `qbox <AccessKey> <SecretKey>`
325+
```
326+
327+
### authstub
328+
329+
```bash
330+
authstub -uid <Uid> -utype <Utype>
331+
```
332+
333+
返回由七牛内部定义的 mock authorization 授权的 `auth interface`,可被 `auth` 命令使用。如:
334+
335+
```bash
336+
auth `authstub -uid 1 -utype 4`
337+
```
338+
339+
这样就模拟了一个 uid 为 1 的标准用户。
340+

0 commit comments

Comments
 (0)