bangs/test.nix

62 lines
1.5 KiB
Nix
Raw Permalink Normal View History

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