bangs/test.nix

62 lines
1.5 KiB
Nix

{ nixpkgs, system, nixosModules }:
with import (nixpkgs + "/nixos/lib/testing-python.nix")
{
inherit system;
};
let
port = 8082;
in
makeTest {
nodes = {
server = {
imports = [ nixosModules.bang-evaluator ];
services.bang-evaluator = {
enable = true;
listenAddress = ":${toString port}";
};
networking.firewall.allowedTCPPorts = [ port ];
};
client = { pkgs, ... }: {
environment.systemPackages = with pkgs; [ curl ];
};
};
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(
[
"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"
)
'';
}