-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.api.spec.ts
57 lines (52 loc) · 1.49 KB
/
index.api.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import axios from 'axios'
import FormData from 'form-data'
import path from 'path'
import fs from 'fs'
const API_PREFIX = 'http://127.0.0.1:3000/pet'
describe('接口测试', () => {
test('get', async () => {
const { data: list } = await axios.get(API_PREFIX)
expect(list).toEqual([
{
id: 1,
name: 'WangWang'
},
{
id: 2,
name: 'MiMi'
}
])
const { data: item } = await axios.get(`${API_PREFIX}/1`)
expect(item).toEqual({
id: 1,
name: 'WangWang'
})
})
test('post', async () => {
// json 类型的
const { data: id } = await axios.post(API_PREFIX, {
name: 'Lala'
})
const { data: item } = await axios.get(`${API_PREFIX}/${id}`)
expect((item as any).name).toBe('Lala')
// form Data 类型的
// const bodyFormData = new FormData()
// bodyFormData.append('name', 'xx')
// const {data: formDataRes} = await axios.post(`${API_PREFIX}/byFormData`,{
// data: bodyFormData,
// headers: { 'Content-Type': 'multipart/form-data' },
// })
// 上传文件
const fileFormData = new FormData()
const formHeaders = fileFormData.getHeaders()
fileFormData.append('image', fs.createReadStream(path.resolve(__dirname, './demo.jpeg')))
// console.log(fileFormData)
const { data: uploadRes } = await axios.post(`${API_PREFIX}/upload`, {
data: fileFormData,
headers: {
...formHeaders
}
})
expect(uploadRes).toBe('success')
})
})