@@ -322,6 +322,35 @@ impl HALClient {
322322 self . fetch_url ( & link_data, template_values) . await
323323 }
324324
325+ /// Normalize a URL or path from a broker link, handling context paths and absolute URLs
326+ fn normalize_path_from_url ( & self , url_string : & str ) -> Result < String , PactBrokerError > {
327+ trace ! ( "normalize_path_from_url(url='{}')" , url_string) ;
328+
329+ // Extract path from URL if it's absolute, otherwise use as-is
330+ let mut path = if let Ok ( parsed_url) = url_string. parse :: < Url > ( ) {
331+ // URL is absolute, extract path to use broker's original host and context path
332+ parsed_url. path ( ) . to_string ( )
333+ } else {
334+ // URL is already a path (relative)
335+ url_string. to_string ( )
336+ } ;
337+
338+ // If we have a context path and the path doesn't already include it, prepend it
339+ let broker_url = self . url . parse :: < Url > ( ) ?;
340+ let context_path = broker_url. path ( ) ;
341+ if !context_path. is_empty ( ) && context_path != "/" && path. starts_with ( "/" ) {
342+ let context_with_slash = format ! ( "{}/" , context_path) ;
343+ let path_matches_context = path == context_path || path. starts_with ( & context_with_slash) ;
344+ if !path_matches_context {
345+ // Path doesn't include context path, prepend it
346+ debug ! ( "Prepending context path '{}' to path '{}'" , context_path, path) ;
347+ path = format ! ( "{}{}" , context_path, path) ;
348+ }
349+ }
350+
351+ Ok ( path)
352+ }
353+
325354 /// Fetch the resource at the Link from the Pact broker
326355 pub async fn fetch_url (
327356 & self ,
@@ -340,9 +369,8 @@ impl HALClient {
340369 ) )
341370 } ?;
342371
343- let base_url = self . url . parse :: < Url > ( ) ?;
344- let joined_url = base_url. join ( & link_url) ?;
345- self . fetch ( joined_url. path ( ) . into ( ) ) . await
372+ let path = self . normalize_path_from_url ( & link_url) ?;
373+ self . fetch ( & path) . await
346374 }
347375
348376 async fn fetch ( & self , path : & str ) -> Result < Value , PactBrokerError > {
@@ -528,29 +556,8 @@ impl HALClient {
528556 async fn send_document ( & self , url : & str , body : & str , method : Method ) -> Result < Value , PactBrokerError > {
529557 debug ! ( "Sending JSON to {} using {}: {}" , url, method, body) ;
530558
531- // Extract path from URL if it's absolute (like fetch_url does), then resolve with context path
532- let mut path = if let Ok ( link_as_url) = url. parse :: < Url > ( ) {
533- // URL is absolute, extract path to use broker's original host and context path
534- link_as_url. path ( ) . to_string ( )
535- } else {
536- // URL is already a path (relative)
537- url. to_string ( )
538- } ;
539-
540- // If we have a context path and the path doesn't already include it, prepend it
541- let broker_url = self . url . parse :: < Url > ( ) ?;
542- let context_path = broker_url. path ( ) ;
543- if !context_path. is_empty ( ) && context_path != "/" && path. starts_with ( "/" ) {
544- let context_with_slash = format ! ( "{}/" , context_path) ;
545- let path_matches_context = path == context_path || path. starts_with ( & context_with_slash) ;
546- if !path_matches_context {
547- // Path doesn't include context path, prepend it
548- let full_path = format ! ( "{}{}" , context_path, path) ;
549- path = full_path;
550- }
551- }
552-
553- let url = self . resolve_path ( path. as_str ( ) ) ?;
559+ let path = self . normalize_path_from_url ( url) ?;
560+ let url = self . resolve_path ( & path) ?;
554561
555562 let request_builder = match self . auth {
556563 Some ( ref auth) => match auth {
0 commit comments