summaryrefslogtreecommitdiffstats
path: root/nixos/tests/nginx-grpc-error-pages.nix
blob: 284ef345100ecf8b6d8a8420c14be8a70f19106d (plain)
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
{ ... }:
{
  name = "nginx-grpc-error-pages";

  containers = {
    webserver =
      { pkgs, ... }:
      {
        services.nginx = {
          enable = true;

          virtualHosts.test = {
            locations = {
              "=/".return = "200 blub";
              "/grpc.reflection.v1.ServerReflection" = {
                useGrpcErrorPages = true;
                extraConfig = ''
                  grpc_pass unix:/run/some-service.sock;
                  # Let's pretend there was a more sophisticated check here
                  return 401;
                '';
              };
            };
          };
        };
      };
  };

  testScript = ''
    webserver.wait_for_unit("nginx")
    webserver.wait_for_open_port(80)

    # regular HTTP requests should behave normally
    webserver.succeed("curl --fail http://localhost")
    t.assertEqual(webserver.succeed("curl -s -o /dev/null -w '%{http_code}' http://localhost/404").strip(), "404")

    # a gRPC 401 becomes HTTP code 2xx, with grpc-message and grpc-status headers set.
    t.assertEqual(webserver.succeed("curl -s -o /dev/null --fail -w '%header{grpc-status}_%header{grpc-message}' http://localhost/grpc.reflection.v1.ServerReflection").strip(), "16_unauthenticated")
  '';
}