-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philippe Lécaillon
committed
Jan 15, 2025
1 parent
0b69941
commit 5caf814
Showing
13 changed files
with
338 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Version>1.1.0</Version> | ||
<AssemblyVersion>1.1.0.0</AssemblyVersion> | ||
<FileVersion>1.1.0.0</FileVersion> | ||
<Version>1.1.1</Version> | ||
<AssemblyVersion>1.1.1.0</AssemblyVersion> | ||
<FileVersion>1.1.1.0</FileVersion> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cassette | ||
namespace Cassette; | ||
|
||
[Serializable] | ||
internal class Cassette | ||
{ | ||
[Serializable] | ||
internal class Cassette | ||
public Request Request { get; set; } | ||
public Response Response { get; set; } | ||
public DateTimeOffset RecordedAt { get; set; } | ||
|
||
public byte[] ToByteArray() | ||
{ | ||
public Request Request { get; set; } | ||
public Response Response { get; set; } | ||
public DateTimeOffset RecordedAt { get; set; } | ||
return JsonSerializer.SerializeToUtf8Bytes(this); | ||
} | ||
|
||
public byte[] ToByteArray() | ||
{ | ||
var formatter = new BinaryFormatter(); | ||
using (var stream = new MemoryStream()) | ||
{ | ||
formatter.Serialize(stream, this); | ||
return stream.ToArray(); | ||
} | ||
} | ||
public static async Task<Cassette> Record(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpRequest); | ||
ArgumentNullException.ThrowIfNull(httpResponse); | ||
|
||
public static async Task<Cassette> Record(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse) | ||
return new Cassette | ||
{ | ||
if (httpRequest is null) | ||
{ | ||
throw new ArgumentNullException(nameof(httpRequest)); | ||
} | ||
|
||
if (httpResponse is null) | ||
{ | ||
throw new ArgumentNullException(nameof(httpResponse)); | ||
} | ||
Request = await httpRequest.ToRequest(), | ||
Response = await httpResponse.ToResponse(), | ||
RecordedAt = DateTimeOffset.UtcNow | ||
}; | ||
} | ||
|
||
return new Cassette | ||
{ | ||
Request = await httpRequest.ToRequest(), | ||
Response = await httpResponse.ToResponse(), | ||
RecordedAt = DateTimeOffset.UtcNow | ||
}; | ||
public HttpResponseMessage Replay() | ||
{ | ||
var httpResponse = new HttpResponseMessage | ||
{ | ||
StatusCode = Response.Status, | ||
Version = Response.Version, | ||
ReasonPhrase = Response.ReasonPhrase, | ||
RequestMessage = Request.ToHttpRequestMessage(), | ||
Content = Response.Body.ToHttpContent() | ||
}; | ||
|
||
foreach (var header in Response.Headers) | ||
{ | ||
httpResponse.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
} | ||
|
||
public HttpResponseMessage Replay() | ||
if (httpResponse.Content != null) | ||
{ | ||
var httpResponse = new HttpResponseMessage | ||
foreach (var header in Response.ContentHeaders) | ||
{ | ||
StatusCode = Response.Status, | ||
Version = Response.Version, | ||
ReasonPhrase = Response.ReasonPhrase, | ||
RequestMessage = Request.ToHttpRequestMessage(), | ||
Content = Response.Body.ToHttpContent() | ||
}; | ||
|
||
foreach (var header in Response.Headers) | ||
{ | ||
httpResponse.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
httpResponse.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
} | ||
} | ||
|
||
if (httpResponse.Content != null) | ||
{ | ||
foreach (var header in Response.ContentHeaders) | ||
{ | ||
httpResponse.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
} | ||
} | ||
return httpResponse; | ||
} | ||
|
||
return httpResponse; | ||
public static string GetKey(Request request, CassetteOptions options) | ||
{ | ||
if (request.Headers.ContainsKey(CassetteOptions.NoRecord)) | ||
{ | ||
return null; | ||
} | ||
|
||
public static string GetKey(Request request, CassetteOptions options) | ||
{ | ||
if (request.Headers.ContainsKey(CassetteOptions.NoRecord)) | ||
{ | ||
return null; | ||
} | ||
string requestMethod = request.Method; | ||
string requestUri = request.Headers.ContainsKey(CassetteOptions.ExcludeLastUriSegment) ? request.GetUriWithoutLastSegment() : request.Uri; | ||
|
||
string requestMethod = request.Method; | ||
string requestUri = request.Headers.ContainsKey(CassetteOptions.ExcludeLastUriSegment) ? request.GetUriWithoutLastSegment() : request.Uri; | ||
using (var hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA1)) | ||
{ | ||
hasher.AppendData(Encoding.UTF8.GetBytes(requestMethod + requestUri)); | ||
|
||
using (var hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA1)) | ||
if (request.Body != null && !request.Headers.ContainsKey(CassetteOptions.ExcludeRequestBody)) | ||
{ | ||
hasher.AppendData(Encoding.UTF8.GetBytes(requestMethod + requestUri)); | ||
|
||
if (request.Body != null && !request.Headers.ContainsKey(CassetteOptions.ExcludeRequestBody)) | ||
{ | ||
hasher.AppendData(request.Body); | ||
} | ||
|
||
return options.KeyPrefix is null ? "" : options.KeyPrefix + options.KeySeparator | ||
+ requestMethod + options.KeySeparator | ||
+ requestUri.Replace("http://", "http//").Replace("https://", "https//") + options.KeySeparator | ||
+ Convert.ToBase64String(hasher.GetHashAndReset()); | ||
hasher.AppendData(request.Body); | ||
} | ||
|
||
return options.KeyPrefix is null ? "" : options.KeyPrefix + options.KeySeparator | ||
+ requestMethod + options.KeySeparator | ||
+ requestUri.Replace("http://", "http//").Replace("https://", "https//") + options.KeySeparator | ||
+ Convert.ToBase64String(hasher.GetHashAndReset()); | ||
} | ||
} | ||
} | ||
|
||
[Serializable] | ||
internal class Request | ||
{ | ||
public string Method { get; set; } | ||
public string Uri { get; set; } | ||
public Dictionary<string, IEnumerable<string>> Headers { get; set; } | ||
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; } | ||
public byte[] Body { get; set; } | ||
public Version Version { get; set; } | ||
} | ||
[Serializable] | ||
internal class Request | ||
{ | ||
public string Method { get; set; } | ||
public string Uri { get; set; } | ||
public Dictionary<string, IEnumerable<string>> Headers { get; set; } | ||
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; } | ||
public byte[] Body { get; set; } | ||
public Version Version { get; set; } | ||
} | ||
|
||
[Serializable] | ||
internal class Response | ||
{ | ||
public HttpStatusCode Status { get; set; } | ||
public string ReasonPhrase { get; set; } | ||
public Dictionary<string, IEnumerable<string>> Headers { get; set; } | ||
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; } | ||
public byte[] Body { get; set; } | ||
public Version Version { get; set; } | ||
} | ||
[Serializable] | ||
internal class Response | ||
{ | ||
public HttpStatusCode Status { get; set; } | ||
public string ReasonPhrase { get; set; } | ||
public Dictionary<string, IEnumerable<string>> Headers { get; set; } | ||
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; } | ||
public byte[] Body { get; set; } | ||
public Version Version { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.