Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using Rhino.Mocks;
using ServiceStack.ServiceHost;
using ServiceStack.Web;
using Xunit;

namespace ServiceStack.PartialResponse.ServiceModel.UnitTests
Expand All @@ -10,10 +10,10 @@ public class PartialResponseExtensionsTests
[Fact]
public void ToPartialResponse_NoPartialFieldsSet_ReturnsSameObject()
{
var reqContextMock = MockRepository.GenerateMock<IRequestContext>();
var request = MockRepository.GenerateMock<IRequest>();
const decimal ExpectedSalary = 1234.56m;
var fakeDto = new FakeDto {Salary = ExpectedSalary};
object partialResponse = reqContextMock.ToPartialResponse(fakeDto);
object partialResponse = request.ToPartialResponse(fakeDto);

Assert.Same(fakeDto, partialResponse);
Assert.Equal(ExpectedSalary, ((FakeDto) partialResponse).Salary);
Expand All @@ -23,9 +23,9 @@ public void ToPartialResponse_NoPartialFieldsSet_ReturnsSameObject()
public void ToPartialResponse_NullDto_ReturnsNull()
{
FakeDto fakeDto = null;
var reqContextMock = MockRepository.GenerateMock<IRequestContext>();
var request = MockRepository.GenerateMock<IRequest>();

Assert.Null(reqContextMock.ToPartialResponse(fakeDto));
Assert.Null(request.ToPartialResponse(fakeDto));
}

[Fact]
Expand All @@ -34,10 +34,10 @@ public void ToPartialResponse_UnsupportedType_ReturnsSameObject()
const decimal ExpectedSalary = 1234.56m;
var fakeDto = new FakeDto {Salary = ExpectedSalary};
var config = new DefaultPartialResponseConfig();
var reqContextMock = MockRepository.GenerateMock<IRequestContext>();
reqContextMock.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0] + "garbage");
reqContextMock.Expect(x => x.GetHeader(config.FieldsHeaderName)).Return("id");
object partialResponse = reqContextMock.ToPartialResponse(fakeDto, config);
var request = MockRepository.GenerateMock<IRequest>();
request.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0] + "garbage");
request.Expect(x => x.Headers.Get(config.FieldsHeaderName)).Return("id");
object partialResponse = request.ToPartialResponse(fakeDto, config);

Assert.Same(fakeDto, partialResponse);
Assert.Equal(ExpectedSalary, ((FakeDto) partialResponse).Salary);
Expand All @@ -49,10 +49,10 @@ public void ToPartialResponse_FieldsSetAndSupportedType_ReturnspartialResponse()
const decimal ExpectedSalary = 1234.56m;
var fakeDto = new FakeDto {Salary = ExpectedSalary, Person = new FakePerson()};
var config = new DefaultPartialResponseConfig();
var reqContextMock = MockRepository.GenerateMock<IRequestContext>();
reqContextMock.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0]);
reqContextMock.Expect(x => x.GetHeader(config.FieldsHeaderName)).Return("salary");
dynamic partialResponse = reqContextMock.ToPartialResponse(fakeDto, config);
var request = MockRepository.GenerateMock<IRequest>();
request.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0]);
request.Expect(x => x.Headers.Get(config.FieldsHeaderName)).Return("salary");
dynamic partialResponse = request.ToPartialResponse(fakeDto, config);

Assert.False(((IDictionary<string, object>) partialResponse).ContainsKey("Person"));
Assert.True(((IDictionary<string, object>) partialResponse).ContainsKey("Salary"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Interfaces, Version=3.9.48.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Common.3.9.48\lib\net35\ServiceStack.Interfaces.dll</HintPath>
<Reference Include="ServiceStack.Interfaces, Version=4.0.0.0, Culture=neutral, PublicKeyToken=e06fbc6124f57c43, processorArchitecture=MSIL">
<HintPath>..\packages\ServiceStack.Interfaces.4.0.44\lib\portable-wp80+sl5+net40+win8+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="xunit">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="RhinoMocks" version="3.6.1" targetFramework="net40" />
<package id="ServiceStack.Common" version="3.9.48" targetFramework="net40" />
<package id="ServiceStack.Text" version="3.9.48" targetFramework="net40" />
<package id="xunit" version="1.9.1" targetFramework="net40" />
<package id="RhinoMocks" version="3.6.1" targetFramework="net4" />
<package id="ServiceStack.Interfaces" version="4.0.44" targetFramework="net4" />
<package id="xunit" version="1.9.1" targetFramework="net4" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using ServiceStack.ServiceHost;
using ServiceStack.Web;

namespace ServiceStack.PartialResponse.ServiceModel
{
internal static class FieldsRetriever
{
public static string GetFields(IRequestContext requestContext, IPartialResponseConfig partialResponseConfig)
public static string GetFields(IRequest request, IPartialResponseConfig partialResponseConfig)
{
switch (partialResponseConfig.FieldResolutionMethod)
{
case FieldResolutionMethod.HeaderOnly:
return FieldsFromHeader(requestContext, partialResponseConfig.FieldsHeaderName);
return FieldsFromHeader(request, partialResponseConfig.FieldsHeaderName);
case FieldResolutionMethod.QueryStringOnly:
return FieldsFromQueryString(requestContext, partialResponseConfig.FieldsQueryStringName);
return FieldsFromQueryString(request, partialResponseConfig.FieldsQueryStringName);
case FieldResolutionMethod.HeaderThenQueryString:
{
string fields = FieldsFromHeader(requestContext, partialResponseConfig.FieldsHeaderName);
string fields = FieldsFromHeader(request, partialResponseConfig.FieldsHeaderName);
return string.IsNullOrWhiteSpace(fields)
? FieldsFromQueryString(requestContext, partialResponseConfig.FieldsQueryStringName)
? FieldsFromQueryString(request, partialResponseConfig.FieldsQueryStringName)
: fields;
}
case FieldResolutionMethod.QueryStringThenHeader:
{
string fields = FieldsFromQueryString(requestContext, partialResponseConfig.FieldsQueryStringName);
string fields = FieldsFromQueryString(request, partialResponseConfig.FieldsQueryStringName);
return string.IsNullOrWhiteSpace(fields)
? FieldsFromHeader(requestContext, partialResponseConfig.FieldsHeaderName)
? FieldsFromHeader(request, partialResponseConfig.FieldsHeaderName)
: fields;
}
case FieldResolutionMethod.QueryStringAndHeader:
{
string headerFields = FieldsFromHeader(requestContext, partialResponseConfig.FieldsHeaderName);
string headerFields = FieldsFromHeader(request, partialResponseConfig.FieldsHeaderName);
string queryFields = FieldsFromQueryString(
requestContext, partialResponseConfig.FieldsQueryStringName);
request, partialResponseConfig.FieldsQueryStringName);
return string.IsNullOrWhiteSpace(headerFields)
? queryFields
: string.Join(
Expand All @@ -41,25 +41,24 @@ public static string GetFields(IRequestContext requestContext, IPartialResponseC
}
}

public static string FieldsFromQueryString(IRequestContext requestContext, string fieldsQueryStringName)
public static string FieldsFromQueryString(IRequest request, string fieldsQueryStringName)
{
var httpRequest = requestContext.Get<IHttpRequest>();

if (httpRequest == null)
{
return string.Empty;
}
if (httpRequest.QueryString == null)
if (request == null || request.QueryString == null)
{
return string.Empty;
}

return httpRequest.QueryString.Get(fieldsQueryStringName);
return request.QueryString.Get(fieldsQueryStringName) ?? string.Empty;
}

public static string FieldsFromHeader(IRequestContext requestContext, string fieldsHeaderName)
public static string FieldsFromHeader(IRequest request, string fieldsHeaderName)
{
return requestContext.GetHeader(fieldsHeaderName) ?? string.Empty;
if (request == null || request.Headers == null)
{
return string.Empty;
}

return request.Headers.Get(fieldsHeaderName) ?? string.Empty;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using ServiceStack.ServiceHost;
using ServiceStack.Web;

namespace ServiceStack.PartialResponse.ServiceModel
{
Expand All @@ -10,47 +10,47 @@ public static class PartialResponseExtensions
{
/// <summary>
/// <para>
/// Returns a partial response of the given Dto if fileds are specified in the request context and the content
/// Returns a partial response of the given Dto if fields are specified in the request and the content
/// type is supported.
/// </para>
/// <para>
/// Uses <seealso cref="DefaultPartialResponseConfig" /> for partial response configuration.
/// </para>
/// </summary>
/// <typeparam name="T">Dto Class</typeparam>
/// <param name="requestContext">Servicestack Request Context</param>
/// <param name="request">Servicestack Request</param>
/// <param name="dto">Dto to process to partial response.</param>
/// <returns></returns>
public static object ToPartialResponse<T>(this IRequestContext requestContext, T dto)
public static object ToPartialResponse<T>(this IRequest request, T dto)
where T : class
{
return requestContext.ToPartialResponse(dto, new DefaultPartialResponseConfig());
return request.ToPartialResponse(dto, new DefaultPartialResponseConfig());
}

/// <summary>
/// <para>
/// Returns a partial response of the given Dto if fileds are specified in the request context and the content
/// Returns a partial response of the given Dto if fields are specified in the request and the content
/// type is supported.
/// </para>
/// <para>Configuration is provided by the caller.</para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestContext"></param>
/// <param name="request"></param>
/// <param name="dto"></param>
/// <param name="partialResponseConfig"></param>
/// <returns></returns>
public static object ToPartialResponse<T>(
this IRequestContext requestContext, T dto, IPartialResponseConfig partialResponseConfig)
this IRequest request, T dto, IPartialResponseConfig partialResponseConfig)
where T : class
{
if (dto == null)
{
return null;
}

string fields = FieldsRetriever.GetFields(requestContext, partialResponseConfig);
string fields = FieldsRetriever.GetFields(request, partialResponseConfig);
bool isSupportedContentType =
partialResponseConfig.IsSupportedContentType(requestContext.ResponseContentType);
partialResponseConfig.IsSupportedContentType(request.ResponseContentType);

object processedResponse = dto;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<Reference Include="ImpromptuInterface">
<HintPath>..\packages\ImpromptuInterface.6.0.6\lib\net40\ImpromptuInterface.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Interfaces, Version=3.9.48.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Common.3.9.48\lib\net35\ServiceStack.Interfaces.dll</HintPath>
<Reference Include="ServiceStack.Interfaces, Version=4.0.0.0, Culture=neutral, PublicKeyToken=e06fbc6124f57c43, processorArchitecture=MSIL">
<HintPath>..\packages\ServiceStack.Interfaces.4.0.44\lib\portable-wp80+sl5+net40+win8+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -79,4 +79,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ImpromptuInterface" version="6.0.6" targetFramework="net40-Client" />
<package id="ServiceStack.Common" version="3.9.48" targetFramework="net40-Client" />
<package id="ServiceStack.Text" version="3.9.48" targetFramework="net40-Client" />
</packages>
<package id="ImpromptuInterface" version="6.0.6" targetFramework="net4-client" />
<package id="ServiceStack.Common" version="3.9.48" targetFramework="net4-client" />
<package id="ServiceStack.Interfaces" version="4.0.44" targetFramework="net4-client" />
<package id="ServiceStack.Text" version="3.9.48" targetFramework="net4-client" />
</packages>