mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
63cd906cfc
- Merge all files inside openflare-server to the repository root directory. - Relocate agent, relay, and flared subprojects from internal/ to internal/apps/. - Combine docker-compose files and update build context paths to root. - Update GitHub workflows and Dockerfiles to refer to new directories and package names. - Rewrite Go package imports across all files. - Resolve database renew test race condition and clean up docs.
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|