@@ -315,3 +315,43 @@ func TestListProjects(t *testing.T) {
315315 require .Equal (t , "p1" , projects [0 ].Name )
316316 require .Equal (t , "p2" , projects [1 ].Name )
317317}
318+
319+ func TestIsLocalHarbor (t * testing.T ) {
320+ cases := []struct {
321+ name string
322+ coreURL string
323+ url string
324+ want bool
325+ }{
326+ {"identical" , "http://harbor-core" , "http://harbor-core" , true },
327+ // The 2.15.8 field failure: an extraEnv pinned the default port on
328+ // core's CORE_URL only, and private replication answered 401.
329+ {"core pins the port through extraEnv, jobservice does not" , "http://harbor-core" , "http://harbor-core:80" , true },
330+ {"jobservice pins the port through extraEnv, core does not" , "http://harbor-core:80" , "http://harbor-core" , true },
331+ {"explicit https default port on the registry URL" , "https://harbor-core" , "https://harbor-core:443" , true },
332+ {"explicit https default port on CORE_URL" , "https://harbor-core:443" , "https://harbor-core" , true },
333+ {"trailing slash" , "http://harbor-core" , "http://harbor-core/" , true },
334+ {"uppercase host" , "http://harbor-core" , "http://HARBOR-CORE" , true },
335+ {"non-default port on both" , "http://harbor-core:8080" , "http://harbor-core:8080" , true },
336+ {"different host" , "http://harbor-core" , "http://other-harbor" , false },
337+ {"different non-default port" , "http://harbor-core:8080" , "http://harbor-core:8081" , false },
338+ {"http default port against non-default" , "http://harbor-core" , "http://harbor-core:8080" , false },
339+ {"different scheme" , "http://harbor-core" , "https://harbor-core" , false },
340+ {"https default port is not http default port" , "https://harbor-core" , "http://harbor-core:443" , false },
341+ {"different path" , "http://harbor-core" , "http://harbor-core/registry" , false },
342+ {"unparseable registry URL" , "http://harbor-core" , "http://[::1" , false },
343+ {"schemeless registry URL" , "http://harbor-core" , "harbor-core" , false },
344+ {"empty CORE_URL against schemeless URL" , "" , "harbor-core" , false },
345+ {"empty CORE_URL against absolute URL" , "" , "http://harbor-core" , false },
346+ {"userinfo only matches exactly" , "http://harbor-core" , "http://user:pw@harbor-core" , false },
347+ {"query only matches exactly" , "http://harbor-core" , "http://harbor-core?x=1" , false },
348+ {"identical userinfo matches through the exact compare" , "http://user:pw@harbor-core" , "http://user:pw@harbor-core" , true },
349+ }
350+
351+ for _ , tc := range cases {
352+ t .Run (tc .name , func (t * testing.T ) {
353+ t .Setenv ("CORE_URL" , tc .coreURL )
354+ assert .Equal (t , tc .want , isLocalHarbor (tc .url ))
355+ })
356+ }
357+ }
0 commit comments