55using System . Collections . Generic ;
66using System . Linq ;
77using System . Net . Http ;
8+ using System . Text ;
89using System . Text . RegularExpressions ;
910using System . Threading . Tasks ;
1011using NUnit . Framework ;
1112using Unity . Netcode . Runtime ;
1213using UnityEngine ;
1314using UnityEngine . TestTools ;
15+ using UnityEngine . Windows ;
1416
1517namespace Unity . Netcode . RuntimeTests
1618{
@@ -21,6 +23,7 @@ internal class HelpUrlTests
2123
2224 private bool m_VerboseLogging = false ;
2325
26+ // TODO: Since help URIs are only used in the editor, we should migrate this into the editor tests.
2427 // IOS platform can't run this test for some reason.
2528 [ UnityTest ]
2629 [ UnityPlatform ( exclude = new [ ] { RuntimePlatform . IPhonePlayer } ) ]
@@ -62,18 +65,30 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
6265 {
6366 try
6467 {
65- var split = url . Split ( '#' ) ;
66- url = split [ 0 ] ;
68+ var split = ( string [ ] ) null ;
69+ var hasAnchor = url . Contains ( "#" ) ;
70+ if ( hasAnchor )
71+ {
72+ split = url . Split ( '#' ) ;
73+ url = split [ 0 ] ;
74+ }
75+
76+ var documentText = ContentExistsInDocumentation ( url ) ;
77+ var docExists = ! string . IsNullOrEmpty ( documentText ) ;
6778
68- var stream = await GetContentFromRemoteFile ( url ) ;
79+ var stream = await GetContentFromRemoteFile ( url , docExists ) ;
6980
7081 var redirectUrl = CalculateRedirectURl ( url , stream ) ;
7182 VerboseLog ( $ "Calculated Redirect URL: { redirectUrl } ") ;
7283
73- var content = await GetContentFromRemoteFile ( redirectUrl ) ;
84+ var content = await GetContentFromRemoteFile ( redirectUrl , docExists ) ;
7485
86+ if ( string . IsNullOrEmpty ( content ) )
87+ {
88+ content = documentText ;
89+ }
7590 // If original url had an anchor part (e.g. some/url.html#anchor)
76- if ( split . Length > 1 )
91+ if ( hasAnchor )
7792 {
7893 var anchorString = split [ 1 ] ;
7994
@@ -83,7 +98,6 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
8398 return false ;
8499 }
85100 }
86-
87101 return true ;
88102 }
89103 catch ( Exception e )
@@ -93,12 +107,36 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
93107 }
94108 }
95109
110+ /// <summary>
111+ /// Checks if the help URI is yet to be published but exists as a document.
112+ /// </summary>
113+ /// <param name="url">the help uri</param>
114+ /// <returns>the contents of the file if it exist otherwise it returns an emtpy string</returns>
115+ private string ContentExistsInDocumentation ( string url )
116+ {
117+ var splitFilter = url . Contains ( HelpUrls . BaseManualUrl ) ? HelpUrls . BaseManualUrl : HelpUrls . BaseApiUrl ;
118+ var split = url . Split ( splitFilter ) ;
119+ var current = System . IO . Directory . GetCurrentDirectory ( ) . Replace ( "testproject" , string . Empty ) ;
120+ var filePath = $ "{ current } com.unity.netcode.gameobjects/Documentation~/{ split [ 1 ] . Replace ( ".html" , ".md" ) } ";
121+ try
122+ {
123+ if ( File . Exists ( filePath ) )
124+ {
125+ var bytes = File . ReadAllBytes ( filePath ) ;
126+ return Encoding . UTF8 . GetString ( bytes ) ;
127+ }
128+ }
129+ catch
130+ { }
131+ return string . Empty ;
132+ }
133+
96134 /// <summary>
97135 /// Checks if a remote file at the <paramref name="url"/> exists, and if access is not restricted.
98136 /// </summary>
99137 /// <param name="url">URL to a remote file.</param>
100138 /// <returns>True if the file at the <paramref name="url"/> is able to be downloaded, false if the file does not exist, or if the file is restricted.</returns>
101- private async Task < string > GetContentFromRemoteFile ( string url )
139+ private async Task < string > GetContentFromRemoteFile ( string url , bool documentExists )
102140 {
103141 //Checking if URI is well formed is optional
104142 var uri = new Uri ( url ) ;
@@ -113,7 +151,11 @@ private async Task<string> GetContentFromRemoteFile(string url)
113151 using var response = await k_HttpClient . SendAsync ( request ) ;
114152 if ( ! response . IsSuccessStatusCode || response . Content . Headers . ContentLength <= 0 )
115153 {
116- throw new Exception ( $ "Failed to get remote file from URL { url } ") ;
154+ if ( ! documentExists )
155+ {
156+ throw new Exception ( $ "Failed to get remote file from URL { url } ") ;
157+ }
158+ return string . Empty ;
117159 }
118160
119161 return await response . Content . ReadAsStringAsync ( ) ;
0 commit comments