|
4 | 4 | using System.Net;
|
5 | 5 | using System.Net.Security;
|
6 | 6 | using LibGit2Sharp.Tests.TestHelpers;
|
| 7 | +using LibGit2Sharp.Core; |
7 | 8 | using Xunit;
|
8 | 9 | using Xunit.Extensions;
|
9 | 10 |
|
@@ -76,6 +77,58 @@ public void CustomSmartSubtransportTest(string scheme, string url)
|
76 | 77 | }
|
77 | 78 | }
|
78 | 79 |
|
| 80 | + [Theory] |
| 81 | + [InlineData("https", "https://bitbucket.org/libgit2/testgitrepository.git", "libgit3", "libgit3")] |
| 82 | + public void CanUseCredentials(string scheme, string url, string user, string pass) |
| 83 | + { |
| 84 | + string remoteName = "testRemote"; |
| 85 | + |
| 86 | + var scd = BuildSelfCleaningDirectory(); |
| 87 | + var repoPath = Repository.Init(scd.RootedDirectoryPath); |
| 88 | + |
| 89 | + SmartSubtransportRegistration<MockSmartSubtransport> registration = null; |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + // Disable server certificate validation for testing. |
| 94 | + // Do *NOT* enable this in production. |
| 95 | + ServicePointManager.ServerCertificateValidationCallback = certificateValidationCallback; |
| 96 | + |
| 97 | + registration = GlobalSettings.RegisterSmartSubtransport<MockSmartSubtransport>(scheme); |
| 98 | + Assert.NotNull(registration); |
| 99 | + |
| 100 | + using (var repo = new Repository(scd.DirectoryPath)) |
| 101 | + { |
| 102 | + Remote remote = repo.Network.Remotes.Add(remoteName, url); |
| 103 | + |
| 104 | + // Set up structures for the expected results |
| 105 | + // and verifying the RemoteUpdateTips callback. |
| 106 | + TestRemoteInfo expectedResults = TestRemoteInfo.TestRemoteInstance; |
| 107 | + ExpectedFetchState expectedFetchState = new ExpectedFetchState(remoteName); |
| 108 | + |
| 109 | + // Add expected branch objects |
| 110 | + foreach (KeyValuePair<string, ObjectId> kvp in expectedResults.BranchTips) |
| 111 | + { |
| 112 | + expectedFetchState.AddExpectedBranch(kvp.Key, ObjectId.Zero, kvp.Value); |
| 113 | + } |
| 114 | + |
| 115 | + // Perform the actual fetch |
| 116 | + repo.Network.Fetch(remote, new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler, TagFetchMode = TagFetchMode.Auto, |
| 117 | + CredentialsProvider = (_user, _valid, _hostname) => new UsernamePasswordCredentials() { Username = "libgit3", Password = "libgit3" }, |
| 118 | + }); |
| 119 | + |
| 120 | + // Verify the expected |
| 121 | + expectedFetchState.CheckUpdatedReferences(repo); |
| 122 | + } |
| 123 | + } |
| 124 | + finally |
| 125 | + { |
| 126 | + GlobalSettings.UnregisterSmartSubtransport(registration); |
| 127 | + |
| 128 | + ServicePointManager.ServerCertificateValidationCallback -= certificateValidationCallback; |
| 129 | + } |
| 130 | + } |
| 131 | + |
79 | 132 | [Fact]
|
80 | 133 | public void CannotReregisterScheme()
|
81 | 134 | {
|
@@ -234,14 +287,39 @@ private HttpWebResponse GetResponseWithRedirects()
|
234 | 287 | }
|
235 | 288 | }
|
236 | 289 |
|
237 |
| - response = (HttpWebResponse)request.GetResponse(); |
| 290 | + try |
| 291 | + { |
| 292 | + response = (HttpWebResponse)request.GetResponse(); |
| 293 | + } |
| 294 | + catch (WebException ex) |
| 295 | + { |
| 296 | + response = ex.Response as HttpWebResponse; |
| 297 | + if (response.StatusCode == HttpStatusCode.Unauthorized) |
| 298 | + { |
| 299 | + Credentials cred; |
| 300 | + int ret = SmartTransport.AcquireCredentials(out cred, null, typeof(UsernamePasswordCredentials)); |
| 301 | + if (ret != 0) |
| 302 | + { |
| 303 | + throw new InvalidOperationException("dunno"); |
| 304 | + } |
| 305 | + |
| 306 | + request = CreateWebRequest(EndpointUrl, IsPost, ContentType); |
| 307 | + UsernamePasswordCredentials userpass = (UsernamePasswordCredentials)cred; |
| 308 | + request.Credentials = new NetworkCredential(userpass.Username, userpass.Password); |
| 309 | + continue; |
| 310 | + } |
| 311 | + |
| 312 | + // rethrow if it's not 401 |
| 313 | + throw ex; |
| 314 | + } |
238 | 315 |
|
239 | 316 | if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
|
240 | 317 | {
|
241 | 318 | request = CreateWebRequest(response.Headers["Location"], IsPost, ContentType);
|
242 | 319 | continue;
|
243 | 320 | }
|
244 | 321 |
|
| 322 | + |
245 | 323 | break;
|
246 | 324 | }
|
247 | 325 |
|
|
0 commit comments