1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package curl
17
18 import (
19 "net/http"
20 "testing"
21
22 . "github.com/onsi/gomega"
23 )
24
25
26
27 func TestRoundTrip(t *testing.T) {
28 RegisterTestingT(t)
29 tests := []struct {
30 name string
31 flags CommandFlags
32 }{
33 {
34 name: "simple get",
35 flags: CommandFlags{
36 Method: http.MethodGet,
37 URL: "github.com/chaos-mesh/chaos-mesh",
38 Header: nil,
39 Body: "",
40 FollowLocation: false,
41 JsonContent: false,
42 },
43 }, {
44 name: "get with header",
45 flags: CommandFlags{
46 Method: http.MethodGet,
47 URL: "https://github.com/chaos-mesh/chaos-mesh",
48 Header: Header{
49 "User-Agent": []string{"Go-http-client/1.1"},
50 },
51 Body: "",
52 FollowLocation: false,
53 JsonContent: false,
54 },
55 }, {
56 name: "post json",
57 flags: CommandFlags{
58 Method: http.MethodPost,
59 URL: "https://jsonplaceholder.typicode.com/posts",
60 Header: nil,
61 Body: "{\"foo\": \"bar\"}",
62 FollowLocation: false,
63 JsonContent: true,
64 },
65 }, {
66 name: "post json with custom header",
67 flags: CommandFlags{
68 Method: http.MethodPost,
69 URL: "https://jsonplaceholder.typicode.com/posts",
70 Header: Header{
71 "User-Agent": []string{"Go-http-client/1.1"},
72 },
73 Body: "{\"foo\": \"bar\"}",
74 FollowLocation: false,
75 JsonContent: true,
76 },
77 }, {
78 name: "get with following location",
79 flags: CommandFlags{
80 Method: http.MethodGet,
81 URL: "www.google.com",
82 Header: nil,
83 Body: "",
84 FollowLocation: true,
85 JsonContent: false,
86 },
87 },
88 }
89 for _, test := range tests {
90 t.Run(test.name, func(t *testing.T) {
91 commands, err := renderCommands(test.flags)
92 Expect(err).ShouldNot(HaveOccurred())
93 parsedFlags, err := parseCommands(commands)
94 Expect(err).ShouldNot(HaveOccurred())
95 Expect(parsedFlags).To(Equal(&test.flags), "rendered commands %+v", commands)
96 })
97 }
98 }
99