Skip to content
This repository was archived by the owner on Mar 20, 2019. It is now read-only.

Commit 8e8ba13

Browse files
committed
Fixes unit tests. 49 failures left.
1 parent 5bdaf44 commit 8e8ba13

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/DotNetOpenAuth.Test/MockingHostFactories.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class MockingHostFactories : IHostFactories {
1919
public MockingHostFactories(List<TestBase.Handler> handlers = null) {
2020
this.handlers = handlers ?? new List<TestBase.Handler>();
2121
this.CookieContainer = new CookieContainer();
22+
this.AllowAutoRedirects = true;
2223
}
2324

2425
public List<TestBase.Handler> Handlers {
@@ -27,8 +28,16 @@ public List<TestBase.Handler> Handlers {
2728

2829
public CookieContainer CookieContainer { get; set; }
2930

31+
public bool AllowAutoRedirects { get; set; }
32+
3033
public HttpMessageHandler CreateHttpMessageHandler() {
31-
return new AutoRedirectHandler(new CookieDelegatingHandler(new ForwardingMessageHandler(this.handlers, this), this.CookieContainer));
34+
var forwardingMessageHandler = new ForwardingMessageHandler(this.handlers, this);
35+
var cookieDelegatingHandler = new CookieDelegatingHandler(forwardingMessageHandler, this.CookieContainer);
36+
if (this.AllowAutoRedirects) {
37+
return new AutoRedirectHandler(cookieDelegatingHandler);
38+
} else {
39+
return cookieDelegatingHandler;
40+
}
3241
}
3342

3443
public HttpClient CreateHttpClient(HttpMessageHandler handler = null) {

src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace DotNetOpenAuth.Test.OAuth2 {
88
using System;
99
using System.Collections.Generic;
1010
using System.Linq;
11+
using System.Net;
1112
using System.Net.Http;
1213
using System.Text;
1314
using System.Threading;
@@ -65,6 +66,7 @@ public async Task DecodeRefreshToken() {
6566
try {
6667
var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, };
6768
var authRedirectResponse = await client.PrepareRequestUserAuthorizationAsync(authState);
69+
this.HostFactories.CookieContainer.SetCookies(authRedirectResponse, ClientCallback);
6870
Uri authCompleteUri;
6971
using (var httpClient = this.HostFactories.CreateHttpClient()) {
7072
using (var response = await httpClient.GetAsync(authRedirectResponse.Headers.Location)) {
@@ -74,7 +76,7 @@ public async Task DecodeRefreshToken() {
7476
}
7577

7678
var authCompleteRequest = new HttpRequestMessage(HttpMethod.Get, authCompleteUri);
77-
authCompleteRequest.Headers.Add("Cookie", string.Join("; ", authRedirectResponse.Headers.GetValues("Set-Cookie")));
79+
this.HostFactories.CookieContainer.ApplyCookies(authCompleteRequest);
7880
var result = await client.ProcessUserAuthorizationAsync(authCompleteRequest);
7981
Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty);
8082
Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty);
@@ -147,7 +149,7 @@ public async Task CreateAccessTokenSeesAuthorizingUserClientCredentialGrant() {
147149
return await server.HandleTokenRequestAsync(req, ct);
148150
});
149151

150-
var client = new WebServerClient(AuthorizationServerDescription, hostFactories: this.HostFactories);
152+
var client = new WebServerClient(AuthorizationServerDescription, ClientId, ClientSecret, this.HostFactories);
151153
var result = await client.GetClientAccessTokenAsync(TestScopes);
152154
Assert.That(result.AccessToken, Is.Not.Null);
153155
}
@@ -162,7 +164,7 @@ public async Task CreateAccessTokenSeesAuthorizingUserAuthorizationCodeGrant() {
162164
return true;
163165
});
164166

165-
Handle(AuthorizationServerDescription.TokenEndpoint).By(
167+
Handle(AuthorizationServerDescription.AuthorizationEndpoint).By(
166168
async (req, ct) => {
167169
var server = new AuthorizationServer(authServerMock.Object);
168170
var request = await server.ReadAuthorizationRequestAsync(req, ct);
@@ -181,15 +183,18 @@ public async Task CreateAccessTokenSeesAuthorizingUserAuthorizationCodeGrant() {
181183
Callback = ClientCallback,
182184
};
183185
var authRedirectResponse = await client.PrepareRequestUserAuthorizationAsync(authState);
186+
this.HostFactories.CookieContainer.SetCookies(authRedirectResponse, ClientCallback);
184187
Uri authCompleteUri;
188+
this.HostFactories.AllowAutoRedirects = false;
185189
using (var httpClient = this.HostFactories.CreateHttpClient()) {
186190
using (var response = await httpClient.GetAsync(authRedirectResponse.Headers.Location)) {
187-
response.EnsureSuccessStatusCode();
191+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Redirect));
188192
authCompleteUri = response.Headers.Location;
189193
}
190194
}
191195

192196
var authCompleteRequest = new HttpRequestMessage(HttpMethod.Get, authCompleteUri);
197+
this.HostFactories.CookieContainer.ApplyCookies(authCompleteRequest);
193198
var result = await client.ProcessUserAuthorizationAsync(authCompleteRequest);
194199
Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty);
195200
Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty);

src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public async Task AuthorizationCodeGrant() {
3838
return await server.HandleTokenRequestAsync(req, ct);
3939
});
4040
{
41-
var client = new UserAgentClient(AuthorizationServerDescription);
41+
var client = new UserAgentClient(AuthorizationServerDescription, ClientId, ClientSecret, this.HostFactories);
4242
var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, };
4343
var request = client.PrepareRequestUserAuthorization(authState);
4444
Assert.AreEqual(EndUserAuthorizationResponseType.AuthorizationCode, request.ResponseType);
4545
var authRequestRedirect = await client.Channel.PrepareResponseAsync(request);
4646
Uri authRequestResponse;
47+
this.HostFactories.AllowAutoRedirects = false;
4748
using (var httpClient = this.HostFactories.CreateHttpClient()) {
4849
using (var httpResponse = await httpClient.GetAsync(authRequestRedirect.Headers.Location)) {
4950
authRequestResponse = httpResponse.Headers.Location;
@@ -74,12 +75,13 @@ public async Task ImplicitGrant() {
7475
return await server.Channel.PrepareResponseAsync(response, ct);
7576
});
7677
{
77-
var client = new UserAgentClient(AuthorizationServerDescription);
78+
var client = new UserAgentClient(AuthorizationServerDescription, ClientId, ClientSecret, this.HostFactories);
7879
var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, };
7980
var request = client.PrepareRequestUserAuthorization(authState, implicitResponseType: true);
8081
Assert.That(request.ResponseType, Is.EqualTo(EndUserAuthorizationResponseType.AccessToken));
8182
var authRequestRedirect = await client.Channel.PrepareResponseAsync(request);
8283
Uri authRequestResponse;
84+
this.HostFactories.AllowAutoRedirects = false;
8385
using (var httpClient = this.HostFactories.CreateHttpClient()) {
8486
using (var httpResponse = await httpClient.GetAsync(authRequestRedirect.Headers.Location)) {
8587
authRequestResponse = httpResponse.Headers.Location;

src/DotNetOpenAuth.Test/OpenId/AuthenticationTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace DotNetOpenAuth.Test.OpenId {
88
using System;
9+
using System.Net;
910
using System.Net.Http;
1011
using System.Threading;
1112
using System.Threading.Tasks;
@@ -258,8 +259,10 @@ private async Task ParameterizedAuthenticationTestAsync(Protocol protocol, bool
258259
request.Realm = RPUri;
259260
var redirectRequest = await rp.Channel.PrepareResponseAsync(request);
260261
Uri redirectResponse;
262+
this.HostFactories.AllowAutoRedirects = false;
261263
using (var httpClient = rp.Channel.HostFactories.CreateHttpClient()) {
262264
using (var response = await httpClient.GetAsync(redirectRequest.Headers.Location)) {
265+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Redirect));
263266
redirectResponse = response.Headers.Location;
264267
}
265268
}

0 commit comments

Comments
 (0)