diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-04-30 23:44:29 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-04-30 23:44:29 -0700 |
commit | 2677b09851e2c535ea753e6c2c49e84a52fbfdeb (patch) | |
tree | 8723079b0e2fe8427c3ad7933707a7e83611ac69 /src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs | |
parent | 0bbe2c1929028e5e13dfe47cab74778485018b11 (diff) | |
parent | 5c937f16690ad0ac6cefd0d84f64461b68b79921 (diff) | |
download | DotNetOpenAuth-2677b09851e2c535ea753e6c2c49e84a52fbfdeb.zip DotNetOpenAuth-2677b09851e2c535ea753e6c2c49e84a52fbfdeb.tar.gz DotNetOpenAuth-2677b09851e2c535ea753e6c2c49e84a52fbfdeb.tar.bz2 |
Merge branch 'v3.0' into v3.1
Diffstat (limited to 'src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs')
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs | 76 |
1 files changed, 73 insertions, 3 deletions
diff --git a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs index c7705ff..449a033 100644 --- a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs @@ -15,6 +15,7 @@ namespace DotNetOpenAuth.Test.ChannelElements { using System.Xml; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.Messaging.Reflection; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.Test.Mocks; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -69,6 +70,41 @@ namespace DotNetOpenAuth.Test.ChannelElements { this.ParameterizedReceiveTest(HttpDeliveryMethods.AuthorizationHeaderRequest); } + /// <summary> + /// Verifies that the OAuth ReadFromRequest method gathers parameters + /// from the Authorization header, the query string and the entity form data. + /// </summary> + [TestMethod] + public void ReadFromRequestAuthorizationScattered() { + // Start by creating a standard POST HTTP request. + var fields = new Dictionary<string, string> { + { "age", "15" }, + }; + HttpRequestInfo requestInfo = CreateHttpRequestInfo(HttpDeliveryMethods.PostRequest, fields); + + // Now add another field to the request URL + UriBuilder builder = new UriBuilder(requestInfo.Url); + builder.Query = "Name=Andrew"; + requestInfo.Url = builder.Uri; + requestInfo.RawUrl = builder.Path + builder.Query + builder.Fragment; + + // Finally, add an Authorization header + fields = new Dictionary<string, string> { + { "Location", "http://hostb/pathB" }, + { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) }, + }; + requestInfo.Headers.Add(HttpRequestHeader.Authorization, CreateAuthorizationHeader(fields)); + + IDirectedProtocolMessage requestMessage = this.channel.ReadFromRequest(requestInfo); + + Assert.IsNotNull(requestMessage); + Assert.IsInstanceOfType(requestMessage, typeof(TestMessage)); + TestMessage testMessage = (TestMessage)requestMessage; + Assert.AreEqual(15, testMessage.Age); + Assert.AreEqual("Andrew", testMessage.Name); + Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri); + } + [TestMethod] public void ReadFromRequestForm() { this.ParameterizedReceiveTest(HttpDeliveryMethods.PostRequest); @@ -144,6 +180,42 @@ namespace DotNetOpenAuth.Test.ChannelElements { this.ParameterizedRequestTest(HttpDeliveryMethods.AuthorizationHeaderRequest); } + /// <summary> + /// Verifies that message parts can be distributed to the query, form, and Authorization header. + /// </summary> + [TestMethod] + public void RequestUsingAuthorizationHeaderScattered() { + TestDirectedMessage request = new TestDirectedMessage(MessageTransport.Direct) { + Age = 15, + Name = "Andrew", + Location = new Uri("http://hostb/pathB"), + Recipient = new Uri("http://localtest"), + Timestamp = DateTime.UtcNow, + HttpMethods = HttpDeliveryMethods.AuthorizationHeaderRequest, + }; + + // ExtraData should appear in the form since this is a POST request, + // and only official message parts get a place in the Authorization header. + ((IProtocolMessage)request).ExtraData["appearinform"] = "formish"; + request.Recipient = new Uri("http://localhost/?appearinquery=queryish"); + request.HttpMethods = HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest; + + HttpWebRequest webRequest = this.channel.InitializeRequest(request); + Assert.IsNotNull(webRequest); + Assert.AreEqual("POST", webRequest.Method); + Assert.AreEqual(request.Recipient, webRequest.RequestUri); + + var declaredParts = new Dictionary<string, string> { + { "age", request.Age.ToString() }, + { "Name", request.Name }, + { "Location", request.Location.AbsoluteUri }, + { "Timestamp", XmlConvert.ToString(request.Timestamp, XmlDateTimeSerializationMode.Utc) }, + }; + + Assert.AreEqual(CreateAuthorizationHeader(declaredParts), webRequest.Headers[HttpRequestHeader.Authorization]); + Assert.AreEqual("appearinform=formish", this.webRequestHandler.RequestEntityAsString); + } + [TestMethod] public void RequestUsingGet() { this.ParameterizedRequestTest(HttpDeliveryMethods.GetRequest); @@ -171,9 +243,7 @@ namespace DotNetOpenAuth.Test.ChannelElements { } private static string CreateAuthorizationHeader(IDictionary<string, string> fields) { - if (fields == null) { - throw new ArgumentNullException("fields"); - } + ErrorUtilities.VerifyArgumentNotNull(fields, "fields"); StringBuilder authorization = new StringBuilder(); authorization.Append("OAuth "); |