# Network pass (S007): downloads. A link by its file type and one by its Content-Disposition header land in the
# "Save to" folder; a slow 4 MiB file pauses and resumes with a Range request; another is cancelled; the external
# download managers get the link (aria2, AB Download Manager and JDownloader stubs in tests/smoke/net/server.py, a
# dummy FDM program, tests/smoke/net/fake-fdm.sh) and Nacre downloads it itself when the manager isn't there; the
# user's own manager by port (S065, D-050: POST / on 8767 with D-022's JSON) takes one and falls back when it's down.
# Offline. Never Tor or proxies. Start the server first:
#   python3 tests/smoke/net/server.py &   then   tools/smoke.sh tests/smoke/net-downloads.txt
# Afterwards: <outdir>/home/saved/ has file.zip, report.txt, their (1) copies, named.zip (<a download>), made.txt (blob:),
# file (2).zip (manager fallback), file (3).zip (custom manager fallback), big.bin (4194304 bytes, byte i = i % 251) and no .part;
# <outdir>/home/fdm-args.txt has the FDM link.
wait 3
set /downloads/dir "@HOME@/saved"
set /downloads/ask false
set /downloads/auto_open false
set /downloads/manager "builtin"
open http://127.0.0.1:8766/
waitfor 20 document.title === "Net test http"
# A link whose address says "file": saved, the page stays.
open http://127.0.0.1:8766/file.zip
wait 3
status zip
expect /downloads/0/name "file.zip"
expect /downloads/0/state "Done"
expect /downloads/0/received 22
expect /active/url "http://127.0.0.1:8766/"
# A link whose headers say "attachment": the probe finds it, the name comes from the header.
open http://127.0.0.1:8766/attachment
wait 3
status attachment
expect /downloads/0/name "report.txt"
expect /downloads/0/state "Done"
# The same from links on a page.
open http://127.0.0.1:8766/
waitfor 20 document.title === "Net test http"
locate #zip
wait 0.5
click @
wait 3
expect /downloads/0/name "file (1).zip"
expect /downloads/0/state "Done"
locate #attachment
wait 0.5
click @
wait 3
status links
expect /downloads/0/name "report (1).txt"
expect /downloads/0/state "Done"
expect /active/url "http://127.0.0.1:8766/"
# <a download> (S050): a web link goes to Nacre's downloader under the page's name, a page's blob: file is
# saved by WebKit under the name the page gave it.
js const a = document.createElement("a"); a.href = "/file.zip?download-attr"; a.download = "named.zip"; document.body.append(a); a.click()
wait 3
status download-attr
expect /downloads/0/name "named.zip"
expect /downloads/0/state "Done"
expect /downloads/0/url "http://127.0.0.1:8766/file.zip?download-attr"
js const b = document.createElement("a"); b.href = URL.createObjectURL(new Blob(["made by the page"], { type: "text/plain" })); b.download = "made.txt"; document.body.append(b); b.click()
wait 3
status blob
expect /downloads/0/name "made.txt"
expect /downloads/0/state "Done"
expect /downloads/0/received 16
expect /active/url "http://127.0.0.1:8766/"
# Pause and resume: the rest comes with a Range request.
cmd download http://127.0.0.1:8766/big.bin
wait 2
cmd pause_download
wait 1
status paused
expect /downloads/0/state "Paused"
expect /downloads/0/received > 0
wait 1
cmd resume_download
wait 9
status resumed
expect /downloads/0/state "Done"
expect /downloads/0/received 4194304
flyout downloads
wait 1
shot downloads
flyout none
# Cancel: the part file goes, the item says cancelled.
cmd download http://127.0.0.1:8766/big.bin
wait 2
cmd cancel_download
wait 1
status cancelled
expect /downloads/0/state "Cancelled"
# External download managers: each gets the link; the list shows it as handed off.
set /downloads/managers ["aria2","abdm","jdownloader","fdm"]
set /downloads/aria2_port 8766
set /downloads/abdm_port 8766
set /downloads/jd_port 8766
set /downloads/fdm_path "@PAGESDIR@/../net/fake-fdm.sh"
set /downloads/manager "aria2"
cmd download http://127.0.0.1:8766/file.zip?to=aria2
wait 2
set /downloads/manager "abdm"
cmd download http://127.0.0.1:8766/file.zip?to=abdm
wait 2
set /downloads/manager "jdownloader"
cmd download http://127.0.0.1:8766/file.zip?to=jdownloader
wait 2
set /downloads/manager "fdm"
cmd download http://127.0.0.1:8766/file.zip?to=fdm
wait 2
status handed
expect /downloads/0/state "HandedOff"
expect /downloads/1/state "HandedOff"
expect /downloads/2/state "HandedOff"
expect /downloads/3/state "HandedOff"
open http://127.0.0.1:8766/
waitfor 20 document.title === "Net test http"
js fetch("http://127.0.0.1:8766/log").then(r => r.json()).then(l => { window.netlog = l; })
waitfor 5 window.netlog !== undefined
js JSON.stringify(window.netlog.dlm) + " ranges " + JSON.stringify(window.netlog.ranges)
assert window.netlog.ranges.length > 0
assert window.netlog.dlm.some(c => c.manager === "aria2" && c.method === "aria2.addUri" && c.params[0][0].endsWith("to=aria2"))
assert window.netlog.dlm.some(c => c.manager === "abdm" && c.body.items[0].link.endsWith("to=abdm"))
assert window.netlog.dlm.some(c => c.manager === "jdownloader" && c.form.urls[0].endsWith("to=jdownloader"))
# The manager isn't running: Nacre downloads it itself.
set /downloads/manager "aria2"
set /downloads/aria2_port 9
cmd download http://127.0.0.1:8766/file.zip?to=fallback
wait 6
status fallback
expect /downloads/0/state "Done"
# The user's own manager (S065): a name and a port, chosen as custom:<port>, gets D-022's JSON on POST /.
set /downloads/custom_managers [{"name":"Test manager","port":8767,"enabled":true},{"name":"Down manager","port":9,"enabled":true}]
set /downloads/manager "custom:8767"
cmd download http://127.0.0.1:8766/file.zip?to=custom
wait 2
status custom
expect /downloads/0/state "HandedOff"
expect /downloads/0/manager "Test manager"
js fetch("http://127.0.0.1:8766/log").then(r => r.json()).then(l => { window.netlog2 = l; })
waitfor 5 window.netlog2 !== undefined
js JSON.stringify(window.netlog2.dlm.filter(c => c.manager === "custom"))
assert window.netlog2.dlm.some(c => c.manager === "custom" && c.port === 8767 && c.type === "application/json" && c.body.url.endsWith("to=custom") && c.body.filename === "file.zip" && c.body.referrer === "http://127.0.0.1:8766/" && c.body.headers.Referer === c.body.referrer && "cookies" in c.body && c.body.user_agent.length > 0 && c.body.headers["User-Agent"] === c.body.user_agent && c.body.size === null && c.body.mime === null)
# Down: Nacre downloads it itself.
set /downloads/manager "custom:9"
cmd download http://127.0.0.1:8766/file.zip?to=custom-fallback
wait 6
status custom-fallback
expect /downloads/0/state "Done"
expect /downloads/0/name "file (3).zip"
# The settings page lists both with their status; the add row checks the port.
cmd ping_managers
page nacre://settings/managers
wait 3
shot managers-custom
status done
quit
