-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
194 lines (174 loc) · 4.4 KB
/
main_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestCommand404(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handler))
res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
str := `<form action="/" method="GET" target="">
<label for="command">command:</label>
<input type="text" id="command" name="command" size=100 autofocus><br><br>
</form>
<form
enctype="multipart/form-data"
action="/upload"
method="post"
>
<input type="file" name="myFile" />
<input type="submit" value="upload" />
</form>missing arguments`
if string(greeting) != str {
fmt.Printf("%s", greeting)
log.Fatal("missmatch greeting string")
}
fmt.Printf("%s", greeting)
defer ts.Close()
}
func TestCommandLs(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handler))
fmt.Println(ts.URL + "/?command=ls")
res, err := http.Get(ts.URL + "/?command=ls")
if err != nil {
log.Fatal(err)
}
greeting, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
str := `<form action="/" method="GET" target="">
<label for="command">command:</label>
<input type="text" id="command" name="command" size=100 autofocus><br><br>
</form>
<form
enctype="multipart/form-data"
action="/upload"
method="post"
>
<input type="file" name="myFile" />
<input type="submit" value="upload" />
</form><textarea disabled="true" style="border: none;background-color:white;width:100%;height:100%;">assets
go.mod
go.sum
main.go
main_test.go
README.md
uploads
</textarea>`
if string(greeting) != str {
log.Fatal("missmatch greeting string")
}
fmt.Printf("%s", greeting)
defer ts.Close()
}
func TestCommandFailed(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handler))
fmt.Println(ts.URL + "/?command=kkkkkks")
res, err := http.Get(ts.URL + "/?command=kkkkkks")
if err != nil {
log.Fatal(err)
}
greeting, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
str := `<form action="/" method="GET" target="">
<label for="command">command:</label>
<input type="text" id="command" name="command" size=100 autofocus><br><br>
</form>
<form
enctype="multipart/form-data"
action="/upload"
method="post"
>
<input type="file" name="myFile" />
<input type="submit" value="upload" />
</form>Error : exec: "kkkkkks": executable file not found in $PATH`
if string(greeting) != str {
fmt.Printf("%s", greeting)
log.Fatal("missmatch greeting string")
}
fmt.Printf("%s", greeting)
defer ts.Close()
}
func call(urlPath, filename string) (*http.Response, error) {
client := &http.Client{
Timeout: time.Second * 10,
}
// New multipart writer.
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fw, err := writer.CreateFormFile("myFile", filename)
if err != nil {
return nil, err
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
_, err = io.Copy(fw, file)
if err != nil {
return nil, err
}
writer.Close()
req, err := http.NewRequest("POST", urlPath, bytes.NewReader(body.Bytes()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
rsp, _ := client.Do(req)
if rsp.StatusCode != http.StatusOK {
log.Printf("Request failed with response code: %d", rsp.StatusCode)
}
return rsp, nil
}
func TestCommandUpload(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(uploadFile))
fmt.Println(ts.URL + "/upload")
res, err := call(ts.URL+"/upload", "main.go")
if err != nil {
log.Fatal(err)
}
greeting, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
str := `<form action="/" method="GET" target="">
<label for="command">command:</label>
<input type="text" id="command" name="command" size=100 autofocus><br><br>
</form>
<form
enctype="multipart/form-data"
action="/upload"
method="post"
>
<input type="file" name="myFile" />
<input type="submit" value="upload" />
</form>Success uploaded file`
if string(greeting) != str {
fmt.Printf("%s", greeting)
log.Fatal("missmatch greeting string")
}
os.RemoveAll("uploads")
fmt.Printf("%s", greeting)
defer ts.Close()
}