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

Commit 07d382d

Browse files
committed
Added OutgoingWebResponse.AsHttpResponseMessage extension method.
1 parent 50585d8 commit 07d382d

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/DotNetOpenAuth.Core/Messaging/MessagingUtilities.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace DotNetOpenAuth.Messaging {
1515
using System.IO.Compression;
1616
using System.Linq;
1717
using System.Net;
18+
#if CLR4
19+
using System.Net.Http;
20+
#endif
1821
using System.Net.Mime;
1922
using System.Runtime.Serialization.Json;
2023
using System.Security;
@@ -161,6 +164,28 @@ public static ActionResult AsActionResult(this OutgoingWebResponse response) {
161164
return new OutgoingWebResponseActionResult(response);
162165
}
163166

167+
#if CLR4
168+
/// <summary>
169+
/// Transforms an OutgoingWebResponse to a Web API-friendly HttpResponseMessage.
170+
/// </summary>
171+
/// <param name="outgoingResponse">The response to send to the user agent.</param>
172+
/// <returns>The <see cref="HttpResponseMessage"/> instance to be returned by the Web API method.</returns>
173+
public static HttpResponseMessage AsHttpResponseMessage(this OutgoingWebResponse outgoingResponse) {
174+
HttpResponseMessage response = new HttpResponseMessage(outgoingResponse.Status) {
175+
Content = new StreamContent(outgoingResponse.ResponseStream)
176+
};
177+
178+
var responseHeaders = outgoingResponse.Headers;
179+
foreach (var header in responseHeaders.AllKeys) {
180+
if (!response.Headers.TryAddWithoutValidation(header, responseHeaders[header])) {
181+
response.Content.Headers.TryAddWithoutValidation(header, responseHeaders[header]);
182+
}
183+
}
184+
185+
return response;
186+
}
187+
#endif
188+
164189
/// <summary>
165190
/// Gets the original request URL, as seen from the browser before any URL rewrites on the server if any.
166191
/// Cookieless session directory (if applicable) is also included.

src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ namespace DotNetOpenAuth.Test.Messaging {
99
using System.Collections.Generic;
1010
using System.Collections.Specialized;
1111
using System.Diagnostics;
12+
using System.Globalization;
1213
using System.IO;
14+
using System.Linq;
1315
using System.Net;
16+
using System.Net.Http;
1417
using System.Text;
1518
using System.Text.RegularExpressions;
1619
using System.Web;
1720
using DotNetOpenAuth.Messaging;
1821
using DotNetOpenAuth.Test.Mocks;
22+
using Moq;
1923
using NUnit.Framework;
2024

2125
[TestFixture]
@@ -62,6 +66,30 @@ public void AppendQueryArgsNullDictionary() {
6266
MessagingUtilities.AppendQueryArgs(new UriBuilder(), null);
6367
}
6468

69+
[Test]
70+
public void AsHttpResponseMessage() {
71+
var responseContent = new byte[10];
72+
(new Random()).NextBytes(responseContent);
73+
var responseStream = new MemoryStream(responseContent);
74+
var outgoingResponse = new OutgoingWebResponse();
75+
outgoingResponse.Headers.Add("X-SOME-HEADER", "value");
76+
outgoingResponse.Headers.Add("Content-Length", responseContent.Length.ToString(CultureInfo.InvariantCulture));
77+
outgoingResponse.ResponseStream = responseStream;
78+
79+
var httpResponseMessage = outgoingResponse.AsHttpResponseMessage();
80+
Assert.That(httpResponseMessage, Is.Not.Null);
81+
Assert.That(httpResponseMessage.Headers.GetValues("X-SOME-HEADER").ToList(), Is.EqualTo(new[] { "value" }));
82+
Assert.That(
83+
httpResponseMessage.Content.Headers.GetValues("Content-Length").ToList(),
84+
Is.EqualTo(new[] { responseContent.Length.ToString(CultureInfo.InvariantCulture) }));
85+
var actualContent = new byte[responseContent.Length + 1]; // give the opportunity to provide a bit more data than we expect.
86+
var bytesRead = httpResponseMessage.Content.ReadAsStreamAsync().Result.Read(actualContent, 0, actualContent.Length);
87+
Assert.That(bytesRead, Is.EqualTo(responseContent.Length)); // verify that only the data we expected came back.
88+
var trimmedActualContent = new byte[bytesRead];
89+
Array.Copy(actualContent, trimmedActualContent, bytesRead);
90+
Assert.That(trimmedActualContent, Is.EqualTo(responseContent));
91+
}
92+
6593
[Test]
6694
public void ToDictionary() {
6795
NameValueCollection nvc = new NameValueCollection();
@@ -151,7 +179,7 @@ public void PostMultipart() {
151179
var httpHandler = new TestWebRequestHandler();
152180
bool callbackTriggered = false;
153181
httpHandler.Callback = req => {
154-
Match m = Regex.Match(req.ContentType, "multipart/form-data; boundary=(.+)");
182+
var m = Regex.Match(req.ContentType, "multipart/form-data; boundary=(.+)");
155183
Assert.IsTrue(m.Success, "Content-Type HTTP header not set correctly.");
156184
string boundary = m.Groups[1].Value;
157185
boundary = boundary.Substring(0, boundary.IndexOf(';')); // trim off charset

0 commit comments

Comments
 (0)