-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_test.go
More file actions
121 lines (111 loc) · 2.81 KB
/
Copy pathapi_test.go
File metadata and controls
121 lines (111 loc) · 2.81 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package getit_test
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/block/getit"
)
func TestFetchIntoPipe(t *testing.T) {
tests := []struct {
name string
serverHandler http.HandlerFunc
cmd string
args []string
expectedErr string
cancelContext bool
}{
{
name: "SuccessfulFetch",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("hello world"))
},
cmd: "cat",
args: nil,
},
{
name: "SuccessfulFetchWithArgs",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("line1\nline2\nline3"))
},
cmd: "wc",
args: []string{"-l"},
},
{
name: "HTTPNotFound",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
},
cmd: "cat",
expectedErr: "404 Not Found",
},
{
name: "HTTPInternalServerError",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
cmd: "cat",
expectedErr: "500 Internal Server Error",
},
{
name: "CommandNotFound",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("data"))
},
cmd: "nonexistent-command-that-does-not-exist",
expectedErr: "nonexistent-command-that-does-not-exist failed:",
},
{
name: "CommandFails",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("data"))
},
cmd: "false",
expectedErr: "false failed:",
},
{
name: "CancelledContext",
serverHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("data"))
},
cmd: "cat",
cancelContext: true,
expectedErr: "context canceled",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(tt.serverHandler)
defer server.Close()
u, err := url.Parse(server.URL)
assert.NoError(t, err)
ctx := context.Background()
if tt.cancelContext {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
cancel()
}
err = getit.FetchIntoPipe(ctx, u, tt.cmd, tt.args...)
if tt.expectedErr != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedErr)
} else {
assert.NoError(t, err)
}
})
}
}
func TestFetchIntoPipeInvalidURL(t *testing.T) {
ctx := context.Background()
u := &url.URL{Scheme: "http", Host: "localhost:99999"}
err := getit.FetchIntoPipe(ctx, u, "cat")
assert.Error(t, err)
assert.Contains(t, err.Error(), "fetching")
}