-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_test.go
300 lines (258 loc) · 6.38 KB
/
ph_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2021 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the GNU GPL v3
// license that can be found in the LICENSE file.
package ph // import "github.com/wabarc/telegra.ph"
import (
"context"
"image"
"image/color"
"image/png"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
"github.com/wabarc/helper"
"github.com/wabarc/screenshot"
)
// nolint:errcheck
func genImage() *os.File {
width := 200
height := 10000
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
// Colors are defined by Red, Green, Blue, Alpha uint8 values.
cyan := color.RGBA{100, 200, 200, 0xff}
// Set color for each pixel.
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
switch {
case x < width/2 && y < height/2: // upper left quadrant
img.Set(x, y, cyan)
case x >= width/2 && y >= height/2: // lower right quadrant
img.Set(x, y, color.White)
default:
// Use zero value.
}
}
}
// Encode as PNG.
f, _ := os.Create(os.TempDir() + "/image.png")
png.Encode(f, img)
return f
}
// nolint:errcheck
func writeHTML(content string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, strings.TrimSpace(content))
})
}
func TestPost(t *testing.T) {
f := genImage()
defer os.Remove(f.Name())
arc := &Archiver{}
client, err := arc.newClient()
if err != nil {
t.Error(err)
}
arc.client = client
sub := subject{title: []rune("testing"), source: "http://example.org"}
dest, err := arc.post(sub, "", f.Name())
if err != nil {
t.Fatal(err)
}
resp, err := http.Get(dest)
if err != nil {
t.Log("URL:", dest)
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
t.Fail()
}
}
func TestWayback(t *testing.T) {
binPath := helper.FindChromeExecPath()
if _, err := exec.LookPath(binPath); err != nil {
t.Skip("Chrome headless browser no found, skipped")
}
ts := httptest.NewServer(writeHTML(`
<html>
<head>
<title>Example Domain</title>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`))
defer ts.Close()
input, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
arc := &Archiver{}
dst, err := arc.Wayback(context.Background(), input)
if err != nil {
t.Fatal(err)
}
if dst == "" {
t.Fatal("destination url empty")
}
resp, err := http.Get(dst)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected wayback to telegra.ph, got status code %d instead of 200", resp.StatusCode)
}
}
func TestWaybackByRemote(t *testing.T) {
ts := httptest.NewServer(writeHTML(`
<html>
<head>
<title>Example Domain</title>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`))
defer ts.Close()
binPath := helper.FindChromeExecPath()
if _, err := exec.LookPath(binPath); err != nil {
t.Skip("Chrome headless browser no found, skipped")
}
host := "127.0.0.1"
port := "9222"
tempDir := t.TempDir()
procCtx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := exec.CommandContext(procCtx, binPath,
"--no-first-run",
"--no-default-browser-check",
"--headless",
"--disable-gpu",
"--no-sandbox",
"--user-data-dir="+tempDir,
"--remote-debugging-address="+host,
"--remote-debugging-port="+port,
"about:blank",
)
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
defer stderr.Close()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
input, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
arc := New(nil).ByRemote(net.JoinHostPort(host, port))
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
dst, err := arc.Wayback(ctx, input)
if err != nil {
t.Fatal(err)
}
if dst == "" {
t.Fatal("destination url empty")
}
resp, err := http.Get(dst)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected wayback to telegra.ph by remote headless, got status code %d instead of 200", resp.StatusCode)
}
}
func TestWaybackWithShots(t *testing.T) {
binPath := helper.FindChromeExecPath()
if _, err := exec.LookPath(binPath); err != nil {
t.Skip("Chrome headless browser no found, skipped")
}
ts := httptest.NewServer(writeHTML(`
<html>
<head>
<title>Example Domain</title>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`))
defer ts.Close()
input, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
dirname, err := os.MkdirTemp(os.TempDir(), "telegraph")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirname)
arc := &Archiver{}
ctx := context.Background()
files := screenshot.Files{
Image: path.Join(dirname, "image.png"),
HTML: path.Join(dirname, "html.html"),
}
shot, err := screenshot.Screenshot[screenshot.Path](ctx, input, screenshot.Quality(100), screenshot.AppendToFile(files))
if err != nil {
t.Fatal(err)
}
ctx = arc.WithShot(ctx, shot)
dst, err := arc.Wayback(context.Background(), input)
if err != nil {
t.Fatal(err)
}
if dst == "" {
t.Fatal("destination url empty")
}
resp, err := http.Get(dst)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected wayback to telegra.ph with shots, got status code %d instead of 200", resp.StatusCode)
}
}
func TestSplitImage(t *testing.T) {
file := genImage()
defer os.Remove(file.Name())
paths, err := splitImage(file.Name(), 8976)
if err != nil {
t.Log(err)
t.Log(paths)
t.Fail()
}
}