Work on framework

This commit is contained in:
Michael Bisbjerg 2019-01-26 18:46:44 +01:00
parent 0b70d62f6a
commit c8f6660191
5 changed files with 27 additions and 15 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Threading;
@ -28,13 +29,13 @@ namespace TMDbLibTests.TestFramework.HttpMocking
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Load request content
JObject reqObj = null;
JToken reqObj = null;
if (request.Content != null)
{
await request.Content.LoadIntoBufferAsync();
string reqJson = await request.Content.ReadAsStringAsync();
reqObj = JsonConvert.DeserializeObject<JObject>(reqJson);
reqObj = JsonConvert.DeserializeObject<JToken>(reqJson);
}
// Do the live request
@ -47,10 +48,11 @@ namespace TMDbLibTests.TestFramework.HttpMocking
headers.Add("Content-Type", result.Content.Headers.ContentType.ToString());
string respJson = await result.Content.ReadAsStringAsync();
JObject respObj = JsonConvert.DeserializeObject<JObject>(respJson);
JToken respObj = JsonConvert.DeserializeObject<JToken>(respJson);
_responses.AddResponse(new ResponseObject
{
RecordedAt = DateTime.UtcNow,
ReducedUri = GetReducedUri(request.RequestUri),
ReqMethod = request.Method.Method,
ReqData = reqObj,

View File

@ -20,13 +20,13 @@ namespace TMDbLibTests.TestFramework.HttpMocking
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Load request content
JObject reqObj = null;
JToken reqObj = null;
if (request.Content != null)
{
await request.Content.LoadIntoBufferAsync();
string reqJson = await request.Content.ReadAsStringAsync();
reqObj = JsonConvert.DeserializeObject<JObject>(reqJson);
reqObj = JsonConvert.DeserializeObject<JToken>(reqJson);
}
string reducedUri = GetReducedUri(request.RequestUri);

View File

@ -27,6 +27,9 @@ namespace TMDbLibTests.TestFramework.HttpMocking
{
lock (_responses)
{
if (!_responses.Any())
return;
IOrderedEnumerable<ResponseObject> data = _responses
.OrderBy(s => s.ReducedUri)
.ThenBy(s => s.ReqMethod)
@ -68,14 +71,13 @@ namespace TMDbLibTests.TestFramework.HttpMocking
}
}
public ResponseObject FindResponse(string reducedUri, string method, JObject requestObject)
public ResponseObject FindResponse(string reducedUri, string method, JToken requestObject)
{
lock (_responses)
{
IEnumerable<ResponseObject> applicables = _responses.Where(s =>
s.ReducedUri.Equals(reducedUri, StringComparison.OrdinalIgnoreCase) &&
s.ReqMethod.Equals(method, StringComparison.OrdinalIgnoreCase));
if (requestObject != null)
applicables = applicables.Where(s => JToken.DeepEquals(s.ReqData, requestObject));

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json.Linq;
@ -6,9 +7,10 @@ namespace TMDbLibTests.TestFramework.HttpMocking
{
internal class ResponseObject
{
public DateTime? RecordedAt { get; set; }
public string ReducedUri { get; set; }
public JObject RespData { get; set; }
public JObject ReqData { get; set; }
public JToken RespData { get; set; }
public JToken ReqData { get; set; }
public HttpStatusCode RespStatusCode { get; set; }
public string ReqMethod { get; set; }
public Dictionary<string, string> RespHeaders { get; set; }

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using TMDbLib.Client;
@ -48,11 +49,16 @@ namespace TMDbLibTests.TestFramework
return;
}
_initializedAs = name;
string file = $"data-{name}.json";
string dataDir = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))), "Data");
//RecordingHandler handler = new RecordingHandler(file, new SocketsHttpHandler());
ReplayingHandler handler = new ReplayingHandler(file);
if (!Directory.Exists(dataDir))
Directory.CreateDirectory(dataDir);
_initializedAs = name;
string file = Path.Combine(dataDir, $"data-{name}.json");
RecordingHandler handler = new RecordingHandler(file, new SocketsHttpHandler());
//ReplayingHandler handler = new ReplayingHandler(file);
HttpClient httpClient = new HttpClient(handler);