bangs/test.nix

56 lines
1.4 KiB
Nix

let
port = 8082;
in
import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, ... }:
{
nodes = {
server = {
imports = [ ./module.nix ];
services.bang-evaluator = {
enable = true;
listenAddress = ":${toString port}";
};
networking.firewall.allowedTCPPorts = [ port ];
};
client = { };
};
testScript = ''
from urllib.parse import urlencode
def evalSearch(query, engine="https://duckduckgo.com/?q=%s"):
query_params = {
"query": query,
"engine": engine,
}
return client.succeed(
" ".join(
[
"${pkgs.curl}/bin/curl",
"-s",
"-o/dev/null",
"-w",
"%{redirect_url}",
f"'http://server:${toString port}/eval?{urlencode(query_params)}'",
]
)
)
start_all()
server.wait_for_open_port(${toString port})
client.wait_for_unit("multi-user.target")
assert evalSearch("foo") == "https://duckduckgo.com/?q=foo"
assert evalSearch("foo !wde") == "https://de.wikipedia.org/w/index.php?search=foo"
assert evalSearch("!wde foo") == "https://de.wikipedia.org/w/index.php?search=foo"
assert (
evalSearch("foo", engine="https://startpage.com/sp/search?query=%s")
== "https://startpage.com/sp/search?query=foo"
)
'';
})