package openresty import ( "strings" "testing" ) func TestRenderPagesAPIProxyLocationBlock(t *testing.T) { tests := []struct { name string deployment *PagesDeployment expected []string unexpected []string }{ { name: "nil deployment", deployment: nil, expected: []string{""}, }, { name: "disabled proxy", deployment: &PagesDeployment{ APIProxyEnabled: false, APIProxyPath: "/api", APIProxyPass: "http://127.0.0.1:8080", }, expected: []string{""}, }, { name: "enabled proxy without rewrite", deployment: &PagesDeployment{ APIProxyEnabled: true, APIProxyPath: "/api", APIProxyPass: "http://127.0.0.1:8080", APIProxyRewrite: "", }, expected: []string{ "location /api {", "proxy_pass http://127.0.0.1:8080;", "proxy_http_version 1.1;", "proxy_set_header Host $http_host;", }, unexpected: []string{ "rewrite", }, }, { name: "enabled proxy with rewrite to root", deployment: &PagesDeployment{ APIProxyEnabled: true, APIProxyPath: "/api", APIProxyPass: "http://127.0.0.1:8080", APIProxyRewrite: "/", }, expected: []string{ "location /api {", "rewrite ^/api/(.*)$ /$1 break;", "rewrite ^/api$ / break;", "proxy_pass http://127.0.0.1:8080;", }, }, { name: "enabled proxy with rewrite to subpath", deployment: &PagesDeployment{ APIProxyEnabled: true, APIProxyPath: "/api", APIProxyPass: "http://127.0.0.1:8080", APIProxyRewrite: "/v2", }, expected: []string{ "location /api {", "rewrite ^/api/(.*)$ /v2/$1 break;", "rewrite ^/api$ /v2 break;", "proxy_pass http://127.0.0.1:8080;", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := renderPagesAPIProxyLocationBlock(tt.deployment) if len(tt.expected) == 1 && tt.expected[0] == "" { if got != "" { t.Fatalf("expected empty output, got: %q", got) } return } for _, exp := range tt.expected { if !strings.Contains(got, exp) { t.Errorf("expected output to contain %q, but got:\n%s", exp, got) } } for _, unexp := range tt.unexpected { if strings.Contains(got, unexp) { t.Errorf("expected output NOT to contain %q, but got:\n%s", unexp, got) } } }) } }