This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TestingExtensions.cs
70 lines (54 loc) 路 2.56 KB
/
TestingExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
锘縰sing System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using Cosmonaut.Internal;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
namespace Cosmonaut.Testing
{
public static class TestingExtensions
{
public static ResourceResponse<T> ToResourceResponse<T>(this T resource, HttpStatusCode statusCode, IDictionary<string, string> responseHeaders = null) where T : Resource, new()
{
var resourceResponse = new ResourceResponse<T>(resource);
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
var headers = new NameValueCollection { { "x-ms-request-charge", "0" } };
if (responseHeaders != null)
{
foreach (var responseHeader in responseHeaders)
{
headers[responseHeader.Key] = responseHeader.Value;
}
}
var headersDictionaryInstance = Activator.CreateInstance(InternalTypeCache.Instance.DictionaryNameValueCollectionType, headers);
var arguments = new[] { Stream.Null, headersDictionaryInstance, statusCode, null };
var documentServiceResponse = InternalTypeCache.Instance.DocumentServiceResponseCtorInfo.Invoke(arguments);
var responseField = typeof(ResourceResponse<T>).GetTypeInfo().GetField("response", flags);
responseField?.SetValue(resourceResponse, documentServiceResponse);
return resourceResponse;
}
public static FeedResponse<T> ToFeedResponse<T>(this IQueryable<T> resource, IDictionary<string, string> responseHeaders = null)
{
var headers = new NameValueCollection
{
{ "x-ms-request-charge", "0" },
{ "x-ms-activity-id", Guid.NewGuid().ToString() }
};
if (responseHeaders != null)
{
foreach (var responseHeader in responseHeaders)
{
headers[responseHeader.Key] = responseHeader.Value;
}
}
var headersDictionaryInstance = Activator.CreateInstance(InternalTypeCache.Instance.DictionaryNameValueCollectionType, headers);
var arguments = new[] { resource, resource.Count(), headersDictionaryInstance, false, null, null, null, 0 };
var feedResponse = InternalTypeCache.Instance.FeedResponseCtorInfo<T>().Invoke(arguments);
return (FeedResponse<T>)feedResponse;
}
}
}