-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurly_test.go
81 lines (57 loc) · 2.26 KB
/
curly_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"github.com/m1x0n/curly/pkg"
"os"
"strings"
"testing"
)
// Via the article on how to mock os.Stdin and os.Stdout
// https://eli.thegreenplace.net/2020/faking-stdin-and-stdout-in-go/
// This might be done via faking app.Writer and app.Reader
func TestSimpleCurlConversion(t *testing.T) {
curl := `curl -X GET https://example.com`
fakeIO, _ := fakestdio.New(curl)
fakeIO.CloseStdin()
app := createApp()
os.Args = []string{"curly", "-d"}
err := app.Run(os.Args)
if err != nil {
t.Fatalf("app Run error: %s", err)
}
result, err := fakeIO.ReadAndRestore()
if err != nil {
t.Fatalf("Output read error: %s", err)
}
resultString := string(result)
if !strings.Contains(resultString, `http.Get("https://example.com")`) {
t.Fatalf("cURL to go conversion failed. Resuled in %s", resultString)
}
}
func TestComplexCurlConversion(t *testing.T) {
curl := `curl -H "Content-Type: application/json" -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' "https://api.digitalocean.com/v2/domains/example.com/records"`
fakeIO, _ := fakestdio.New(curl)
fakeIO.CloseStdin()
app := createApp()
os.Args = []string{"curly", "-d"}
err := app.Run(os.Args)
if err != nil {
t.Fatalf("app Run error: %s", err)
}
result, err := fakeIO.ReadAndRestore()
if err != nil {
t.Fatalf("Output read error: %s", err)
}
resultString := string(result)
if !strings.Contains(resultString, `http.NewRequest("POST", "https://api.digitalocean.com/v2/domains/example.com/records", body)`) {
t.Fatalf("cURL to go conversion failed. No request created. Resuled in %s", resultString)
}
if !strings.Contains(resultString, `http.DefaultClient.Do`) {
t.Fatalf("cURL to go conversion failed. No http client call found. Resuled in %s", resultString)
}
if !strings.Contains(resultString, `req.Header.Set("Content-Type", "application/json")`) {
t.Fatalf("cURL to go conversion failed. No Content-Type header found. Resuled in %s", resultString)
}
if !strings.Contains(resultString, `req.Header.Set("Authorization", "Bearer b7d03a6947b217efb6f3ec3bd3504582")`) {
t.Fatalf("cURL to go conversion failed. No Authorization header found. Resuled in %s", resultString)
}
}