-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcurl_api_test.go
56 lines (47 loc) · 1.9 KB
/
pcurl_api_test.go
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
package pcurl
import (
"fmt"
"testing"
"github.com/antlabs/pcurl/body"
"github.com/stretchr/testify/assert"
)
func TestParseAndObj(t *testing.T) {
// TODO 如果没有加右'接尾,报错
all, err := ParseAndObj(`curl https://api.openai.com/v1/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer YOUR_API_KEY' -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'`)
assert.NoError(t, err)
assert.Equal(t, all.Encode.Body, body.EncodeJSON)
assert.Equal(t, all.Method, "POST")
fmt.Printf("%#v\n", all)
}
func TestParseAndJSON(t *testing.T) {
// TODO 如果没有加右'接尾,报错
all, err := ParseAndJSON(`curl https://api.openai.com/v1/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer YOUR_API_KEY' -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'`)
assert.NoError(t, err)
fmt.Printf("%s\n", all)
}
type testCaseObj struct {
curl string
url string
body string
}
func TestPaserAndObj(t *testing.T) {
tab := []testCaseObj{
{
curl: `curl -X POST -d '{"a":"b"}' www.qq.com/test`,
url: `www.qq.com/test`,
},
{
curl: `curl -X POST -i 'http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <YourAppToken>' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"}}'`,
url: `http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users`,
},
{
curl: `curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer <YourAppToken> ' https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`,
url: `https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`,
},
}
for _, tc := range tab {
all, err := ParseAndObj(tc.curl)
assert.Equal(t, all.URL, tc.url)
assert.NoError(t, err)
}
}