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

Commit 5c50924

Browse files
committed
Unit test build break fixes.
1 parent ab1e20e commit 5c50924

35 files changed

Lines changed: 321 additions & 1368 deletions

src/DotNetOpenAuth.Core/Messaging/Channel.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ internal HttpResponseMessage PrepareDirectResponseTestHook(IProtocolMessage resp
504504
/// <param name="response">The response that is anticipated to contain an protocol message.</param>
505505
/// <returns>The deserialized message parts, if found. Null otherwise.</returns>
506506
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
507-
internal Task<IDictionary<string, string>> ReadFromResponseCoreAsyncTestHook(HttpResponseMessage response) {
508-
return this.ReadFromResponseCoreAsync(response);
507+
internal Task<IDictionary<string, string>> ReadFromResponseCoreAsyncTestHook(HttpResponseMessage response, CancellationToken cancellationToken) {
508+
return this.ReadFromResponseCoreAsync(response, cancellationToken);
509509
}
510510

511511
/// <summary>
@@ -520,7 +520,7 @@ internal Task<IDictionary<string, string>> ReadFromResponseCoreAsyncTestHook(Htt
520520
/// This method should NOT be called by derived types
521521
/// except when sending ONE WAY request messages.
522522
/// </remarks>
523-
internal Task ProcessOutgoingMessageTestHookAsync(IProtocolMessage message, CancellationToken cancellationToken) {
523+
internal Task ProcessOutgoingMessageTestHookAsync(IProtocolMessage message, CancellationToken cancellationToken = default(CancellationToken)) {
524524
return this.ProcessOutgoingMessageAsync(message, cancellationToken);
525525
}
526526

@@ -685,7 +685,7 @@ protected virtual async Task<IProtocolMessage> RequestCoreAsync(IDirectedProtoco
685685
return null;
686686
}
687687

688-
var responseFields = await this.ReadFromResponseCoreAsync(response);
688+
var responseFields = await this.ReadFromResponseCoreAsync(response, cancellationToken);
689689
if (responseFields == null) {
690690
return null;
691691
}
@@ -914,9 +914,12 @@ protected virtual HttpResponseMessage CreateFormPostResponse(IDirectedProtocolMe
914914
/// Gets the protocol message that may be in the given HTTP response.
915915
/// </summary>
916916
/// <param name="response">The response that is anticipated to contain an protocol message.</param>
917-
/// <returns>The deserialized message parts, if found. Null otherwise.</returns>
917+
/// <param name="cancellationToken">The cancellation token.</param>
918+
/// <returns>
919+
/// The deserialized message parts, if found. Null otherwise.
920+
/// </returns>
918921
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
919-
protected abstract Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response);
922+
protected abstract Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken);
920923

921924
/// <summary>
922925
/// Prepares an HTTP request that carries a given message.

src/DotNetOpenAuth.Core/Messaging/MessageProtectionTasks.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ internal static class MessageProtectionTasks {
3131
/// </summary>
3232
internal static readonly Task<MessageProtections?> TamperProtection =
3333
Task.FromResult<MessageProtections?>(MessageProtections.TamperProtection);
34+
35+
/// <summary>
36+
/// A task whose result is <see cref="MessageProtections.ReplayProtection"/>
37+
/// </summary>
38+
internal static readonly Task<MessageProtections?> ReplayProtection =
39+
Task.FromResult<MessageProtections?>(MessageProtections.ReplayProtection);
3440
}
3541
}

src/DotNetOpenAuth.OAuth.ServiceProvider/OAuth/ServiceProvider.cs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,22 @@ public UnauthorizedTokenResponse PrepareUnauthorizedTokenMessage(UnauthorizedTok
296296
/// <exception cref="ProtocolException">Thrown if an unexpected OAuth message is attached to the incoming request.</exception>
297297
public Task<UserAuthorizationRequest> ReadAuthorizationRequestAsync(HttpRequestBase request = null, CancellationToken cancellationToken = default(CancellationToken)) {
298298
request = request ?? this.channel.GetRequestFromContext();
299-
return this.Channel.TryReadFromRequestAsync<UserAuthorizationRequest>(request.AsHttpRequestMessage(), cancellationToken);
299+
return this.ReadAuthorizationRequestAsync(request.AsHttpRequestMessage(), cancellationToken);
300+
}
301+
302+
/// <summary>
303+
/// Reads in a Consumer's request for the Service Provider to obtain permission from
304+
/// the user to authorize the Consumer's access of some protected resource(s).
305+
/// </summary>
306+
/// <param name="request">The HTTP request to read from.</param>
307+
/// <param name="cancellationToken">The cancellation token.</param>
308+
/// <returns>
309+
/// The incoming request, or null if no OAuth message was attached.
310+
/// </returns>
311+
/// <exception cref="ProtocolException">Thrown if an unexpected OAuth message is attached to the incoming request.</exception>
312+
public Task<UserAuthorizationRequest> ReadAuthorizationRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken)) {
313+
Requires.NotNull(request, "request");
314+
return this.Channel.TryReadFromRequestAsync<UserAuthorizationRequest>(request, cancellationToken);
300315
}
301316

302317
/// <summary>
@@ -372,7 +387,21 @@ public UserAuthorizationResponse PrepareAuthorizationResponse(UserAuthorizationR
372387
/// <exception cref="ProtocolException">Thrown if an unexpected OAuth message is attached to the incoming request.</exception>
373388
public Task<AuthorizedTokenRequest> ReadAccessTokenRequestAsync(HttpRequestBase request = null, CancellationToken cancellationToken = default(CancellationToken)) {
374389
request = request ?? this.Channel.GetRequestFromContext();
375-
return this.Channel.TryReadFromRequestAsync<AuthorizedTokenRequest>(request.AsHttpRequestMessage(), cancellationToken);
390+
return this.ReadAccessTokenRequestAsync(request.AsHttpRequestMessage(), cancellationToken);
391+
}
392+
393+
/// <summary>
394+
/// Reads in a Consumer's request to exchange an authorized request token for an access token.
395+
/// </summary>
396+
/// <param name="request">The HTTP request to read from.</param>
397+
/// <param name="cancellationToken">The cancellation token.</param>
398+
/// <returns>
399+
/// The incoming request, or null if no OAuth message was attached.
400+
/// </returns>
401+
/// <exception cref="ProtocolException">Thrown if an unexpected OAuth message is attached to the incoming request.</exception>
402+
public Task<AuthorizedTokenRequest> ReadAccessTokenRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken)) {
403+
Requires.NotNull(request, "request");
404+
return this.Channel.TryReadFromRequestAsync<AuthorizedTokenRequest>(request, cancellationToken);
376405
}
377406

378407
/// <summary>
@@ -425,9 +454,26 @@ public AuthorizedTokenResponse PrepareAccessTokenMessage(AuthorizedTokenRequest
425454
/// to access the resources being requested.
426455
/// </remarks>
427456
/// <exception cref="ProtocolException">Thrown if an unexpected message is attached to the request.</exception>
428-
public async Task<AccessProtectedResourceRequest> ReadProtectedResourceAuthorizationAsync(HttpRequestBase request = null, CancellationToken cancellationToken = default(CancellationToken)) {
429-
request = request ?? this.Channel.GetRequestFromContext();
430-
var accessMessage = await this.Channel.TryReadFromRequestAsync<AccessProtectedResourceRequest>(request.AsHttpRequestMessage(), cancellationToken);
457+
public Task<AccessProtectedResourceRequest> ReadProtectedResourceAuthorizationAsync(HttpRequestBase request = null, CancellationToken cancellationToken = default(CancellationToken)) {
458+
request = request ?? this.channel.GetRequestFromContext();
459+
return this.ReadProtectedResourceAuthorizationAsync(request.AsHttpRequestMessage(), cancellationToken);
460+
}
461+
462+
/// <summary>
463+
/// Gets the authorization (access token) for accessing some protected resource.
464+
/// </summary>
465+
/// <param name="request">The incoming HTTP request.</param>
466+
/// <param name="cancellationToken">The cancellation token.</param>
467+
/// <returns>The authorization message sent by the Consumer, or null if no authorization message is attached.</returns>
468+
/// <remarks>
469+
/// This method verifies that the access token and token secret are valid.
470+
/// It falls on the caller to verify that the access token is actually authorized
471+
/// to access the resources being requested.
472+
/// </remarks>
473+
/// <exception cref="ProtocolException">Thrown if an unexpected message is attached to the request.</exception>
474+
public async Task<AccessProtectedResourceRequest> ReadProtectedResourceAuthorizationAsync(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken)) {
475+
Requires.NotNull(request, "request");
476+
var accessMessage = await this.Channel.TryReadFromRequestAsync<AccessProtectedResourceRequest>(request, cancellationToken);
431477
if (accessMessage != null) {
432478
if (this.TokenManager.GetTokenType(accessMessage.AccessToken) != TokenType.AccessToken) {
433479
throw new ProtocolException(

src/DotNetOpenAuth.OAuth/OAuth/ChannelElements/OAuthChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected override async Task<IDirectedProtocolMessage> ReadFromRequestCoreAsync
171171
/// <returns>
172172
/// The deserialized message parts, if found. Null otherwise.
173173
/// </returns>
174-
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response) {
174+
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken) {
175175
string body = await response.Content.ReadAsStringAsync();
176176
return HttpUtility.ParseQueryString(body).ToDictionary();
177177
}

src/DotNetOpenAuth.OAuth2.AuthorizationServer/OAuth2/ChannelElements/OAuth2AuthorizationServerChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected internal OAuth2AuthorizationServerChannel(IAuthorizationServerHost aut
6666
/// The deserialized message parts, if found. Null otherwise.
6767
/// </returns>
6868
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
69-
protected override Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response) {
69+
protected override Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken) {
7070
throw new NotImplementedException();
7171
}
7272

src/DotNetOpenAuth.OAuth2.Client/OAuth2/ChannelElements/OAuth2ClientChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override HttpRequestMessage CreateHttpRequest(IDirectedProtocolMessage
9292
/// The deserialized message parts, if found. Null otherwise.
9393
/// </returns>
9494
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
95-
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response) {
95+
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken) {
9696
// The spec says direct responses should be JSON objects, but Facebook uses HttpFormUrlEncoded instead, calling it text/plain
9797
// Others return text/javascript. Again bad.
9898
string body = await response.Content.ReadAsStringAsync();

src/DotNetOpenAuth.OAuth2.ResourceServer/OAuth2/ChannelElements/OAuth2ResourceServerChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected override async Task<IDirectedProtocolMessage> ReadFromRequestCoreAsync
9090
/// The deserialized message parts, if found. Null otherwise.
9191
/// </returns>
9292
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
93-
protected override Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response) {
93+
protected override Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken) {
9494
// We never expect resource servers to send out direct requests,
9595
// and therefore won't have direct responses.
9696
throw new NotImplementedException();

src/DotNetOpenAuth.OpenId/OpenId/ChannelElements/KeyValueFormEncoding.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace DotNetOpenAuth.OpenId.ChannelElements {
1111
using System.Globalization;
1212
using System.IO;
1313
using System.Text;
14+
using System.Threading;
1415
using System.Threading.Tasks;
1516
using DotNetOpenAuth.Messaging;
1617
using Validation;
@@ -132,12 +133,13 @@ public static byte[] GetBytes(IEnumerable<KeyValuePair<string, string>> keysAndV
132133
/// <param name="data">The stream of Key-Value Form encoded bytes.</param>
133134
/// <returns>The deserialized dictionary.</returns>
134135
/// <exception cref="FormatException">Thrown when the data is not in the expected format.</exception>
135-
public async Task<IDictionary<string, string>> GetDictionaryAsync(Stream data) {
136+
public async Task<IDictionary<string, string>> GetDictionaryAsync(Stream data, CancellationToken cancellationToken) {
136137
using (StreamReader reader = new StreamReader(data, textEncoding)) {
137138
var dict = new Dictionary<string, string>();
138139
int line_num = 0;
139140
string line;
140141
while ((line = await reader.ReadLineAsync()) != null) {
142+
cancellationToken.ThrowIfCancellationRequested();
141143
line_num++;
142144
if (this.ConformanceLevel == KeyValueFormConformanceLevel.Loose) {
143145
line = line.Trim();

src/DotNetOpenAuth.OpenId/OpenId/ChannelElements/OpenIdChannel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ protected override HttpRequestMessage CreateHttpRequest(IDirectedProtocolMessage
133133
/// Gets the protocol message that may be in the given HTTP response.
134134
/// </summary>
135135
/// <param name="response">The response that is anticipated to contain an protocol message.</param>
136+
/// <param name="cancellationToken">The cancellation token.</param>
136137
/// <returns>
137138
/// The deserialized message parts, if found. Null otherwise.
138139
/// </returns>
139140
/// <exception cref="ProtocolException">Thrown when the response is not valid.</exception>
140-
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response) {
141+
protected override async Task<IDictionary<string, string>> ReadFromResponseCoreAsync(HttpResponseMessage response, CancellationToken cancellationToken) {
141142
try {
142143
using (var responseStream = await response.Content.ReadAsStreamAsync()) {
143-
return await this.keyValueForm.GetDictionaryAsync(responseStream);
144+
return await this.keyValueForm.GetDictionaryAsync(responseStream, cancellationToken);
144145
}
145146
} catch (FormatException ex) {
146147
throw ErrorUtilities.Wrap(ex, ex.Message);

src/DotNetOpenAuth.Test/CoordinatorBase.cs

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace DotNetOpenAuth.Test {
88
using System;
99
using System.Collections.Generic;
10+
using System.Net;
11+
using System.Net.Http;
1012
using System.Threading;
1113
using System.Threading.Tasks;
1214
using DotNetOpenAuth.Messaging;
@@ -15,40 +17,80 @@ namespace DotNetOpenAuth.Test {
1517
using NUnit.Framework;
1618
using Validation;
1719

18-
internal abstract class CoordinatorBase<T1, T2> {
19-
private Func<T1, CancellationToken, Task> party1Action;
20-
private Func<T2, CancellationToken, Task> party2Action;
20+
using System.Linq;
2121

22-
protected CoordinatorBase(Func<T1, CancellationToken, Task> party1Action, Func<T2, CancellationToken, Task> party2Action) {
23-
Requires.NotNull(party1Action, "party1Action");
24-
Requires.NotNull(party2Action, "party2Action");
22+
internal class CoordinatorBase {
23+
private Func<IHostFactories, CancellationToken, Task> driver;
2524

26-
this.party1Action = party1Action;
27-
this.party2Action = party2Action;
25+
private Handler[] handlers;
26+
27+
internal CoordinatorBase(Func<IHostFactories, CancellationToken, Task> driver, params Handler[] handlers) {
28+
Requires.NotNull(driver, "driver");
29+
Requires.NotNull(handlers, "handlers");
30+
31+
this.driver = driver;
32+
this.handlers = handlers;
2833
}
2934

30-
protected internal Action<IProtocolMessage> IncomingMessageFilter { get; set; }
35+
protected internal virtual async Task RunAsync(CancellationToken cancellationToken = default(CancellationToken)) {
36+
IHostFactories hostFactories = new MyHostFactories(this.handlers);
37+
38+
await this.driver(hostFactories, cancellationToken);
39+
}
3140

32-
protected internal Action<IProtocolMessage> OutgoingMessageFilter { get; set; }
41+
internal static Handler Handle(Uri uri) {
42+
return new Handler(uri);
43+
}
3344

34-
internal abstract Task RunAsync();
45+
internal struct Handler {
46+
internal Handler(Uri uri)
47+
: this() {
48+
this.Uri = uri;
49+
}
50+
51+
public Uri Uri { get; private set; }
52+
53+
public Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> MessageHandler { get; private set; }
54+
55+
internal Handler By(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler) {
56+
return new Handler(this.Uri) { MessageHandler = handler };
57+
}
58+
}
3559

36-
protected async Task RunCoreAsync(T1 party1Object, T2 party2Object) {
37-
var cts = new CancellationTokenSource();
60+
private class MyHostFactories : IHostFactories {
61+
private readonly Handler[] handlers;
62+
63+
public MyHostFactories(Handler[] handlers) {
64+
this.handlers = handlers;
65+
}
66+
67+
public HttpMessageHandler CreateHttpMessageHandler() {
68+
return new ForwardingMessageHandler(this.handlers);
69+
}
70+
71+
public HttpClient CreateHttpClient(HttpMessageHandler handler = null) {
72+
return new HttpClient(handler ?? this.CreateHttpMessageHandler());
73+
}
74+
}
75+
76+
private class ForwardingMessageHandler : HttpMessageHandler {
77+
private readonly Handler[] handlers;
78+
79+
public ForwardingMessageHandler(Handler[] handlers) {
80+
this.handlers = handlers;
81+
}
3882

39-
try {
40-
var parties = new List<Task> {
41-
Task.Run(() => this.party1Action(party1Object, cts.Token)),
42-
Task.Run(() => this.party2Action(party2Object, cts.Token)),
43-
};
44-
var completingTask = await Task.WhenAny(parties);
45-
await completingTask; // rethrow any exception from the first completing task.
83+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
84+
foreach (var handler in this.handlers) {
85+
if (handler.Uri.AbsolutePath == request.RequestUri.AbsolutePath) {
86+
var response = await handler.MessageHandler(request, cancellationToken);
87+
if (response != null) {
88+
return response;
89+
}
90+
}
91+
}
4692

47-
// if no exception, then block for the second task now.
48-
await Task.WhenAll(parties);
49-
} catch {
50-
cts.Cancel(); // cause the second party to terminate, if necessary.
51-
throw;
93+
return new HttpResponseMessage(HttpStatusCode.NotFound);
5294
}
5395
}
5496
}

0 commit comments

Comments
 (0)