feat: fix composer
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package composer
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/any-hub/any-hub/internal/proxy/hooks"
|
||||
@@ -24,9 +24,20 @@ func TestResolveDistUpstream(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteResponseUpdatesURLs(t *testing.T) {
|
||||
func TestResolveMirrorDistUpstream(t *testing.T) {
|
||||
resetComposerDistRegistry()
|
||||
registerComposerDist("cache.example", "vendor/pkg", "abc123", "zip", "https://github.com/org/repo.zip")
|
||||
ctx := &hooks.RequestContext{Domain: "cache.example"}
|
||||
body := []byte(`{"packages":{"a/b":{"1.0.0":{"dist":{"url":"https://repo.packagist.org/dist/package.zip"}}}}}`)
|
||||
url := resolveDistUpstream(ctx, "", "/composer/dists/vendor/pkg/abc123.zip", nil)
|
||||
if url != "https://github.com/org/repo.zip" {
|
||||
t.Fatalf("unexpected upstream %s", url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteResponseUpdatesURLs(t *testing.T) {
|
||||
resetComposerDistRegistry()
|
||||
ctx := &hooks.RequestContext{Domain: "cache.example"}
|
||||
body := []byte(`{"packages":{"a/b":{"1.0.0":{"dist":{"url":"https://api.github.com/repos/org/repo/zipball/ref","reference":"abc123","type":"zip"}}}}}`)
|
||||
_, headers, rewritten, err := rewriteResponse(ctx, 200, map[string]string{}, body, "/p2/a/b.json")
|
||||
if err != nil {
|
||||
t.Fatalf("rewrite failed: %v", err)
|
||||
@@ -37,7 +48,51 @@ func TestRewriteResponseUpdatesURLs(t *testing.T) {
|
||||
if headers["Content-Type"] != "application/json" {
|
||||
t.Fatalf("expected json content type")
|
||||
}
|
||||
if !strings.Contains(string(rewritten), "/dist/package.zip") {
|
||||
t.Fatalf("expected stripped packagist host, got %s", string(rewritten))
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rewritten, &payload); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
pkgs := payload["packages"].(map[string]any)
|
||||
versions := pkgs["a/b"].(map[string]any)
|
||||
version := versions["1.0.0"].(map[string]any)
|
||||
dist := version["dist"].(map[string]any)
|
||||
distURL := dist["url"].(string)
|
||||
expected := "https://cache.example/dist/https/api.github.com/repos/org/repo/zipball/ref"
|
||||
if distURL != expected {
|
||||
t.Fatalf("expected dist url %s, got %s", expected, distURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewritePackagesRoot(t *testing.T) {
|
||||
resetComposerDistRegistry()
|
||||
ctx := &hooks.RequestContext{Domain: "cache.example"}
|
||||
body := []byte(`{"metadata-url":"https://repo.packagist.org/p2/%package%.json","providers-url":"/p/%package%$%hash%.json"}`)
|
||||
_, headers, rewritten, err := rewriteResponse(ctx, 200, map[string]string{}, body, "/packages.json")
|
||||
if err != nil {
|
||||
t.Fatalf("rewrite failed: %v", err)
|
||||
}
|
||||
if headers["Content-Type"] != "application/json" {
|
||||
t.Fatalf("expected json content type")
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rewritten, &payload); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if payload["metadata-url"] != "https://cache.example/composer/p2/%package%.json" {
|
||||
t.Fatalf("metadata URL not rewritten: %v", payload["metadata-url"])
|
||||
}
|
||||
if payload["providers-url"] != "https://cache.example/composer/p/%package%$%hash%.json" {
|
||||
t.Fatalf("providers URL not rewritten: %v", payload["providers-url"])
|
||||
}
|
||||
mirrors, _ := payload["mirrors"].([]any)
|
||||
if len(mirrors) == 0 {
|
||||
t.Fatalf("mirrors missing")
|
||||
}
|
||||
entry, _ := mirrors[0].(map[string]any)
|
||||
if entry["dist-url"] != "https://cache.example/composer/dists/%package%/%reference%.%type%" {
|
||||
t.Fatalf("unexpected mirror dist-url: %v", entry["dist-url"])
|
||||
}
|
||||
if pref, _ := entry["preferred"].(bool); !pref {
|
||||
t.Fatalf("mirror preferred flag missing")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user