Skip to content

Commit

Permalink
Change to work with non-text HTTP requests & responses (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored and lecaillon committed Jun 11, 2019
1 parent 1f2b463 commit 5c485c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
18 changes: 11 additions & 7 deletions src/Cassette/Cassette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ public static string GetKey(Request request, CassetteOptions options)

string requestMethod = request.Method;
string requestUri = request.Headers.ContainsKey(CassetteOptions.ExcludeLastUriSegment) ? request.GetUriWithoutLastSegment() : request.Uri;
string requestBody = request.Headers.ContainsKey(CassetteOptions.ExcludeRequestBody) ? string.Empty : request.Body;
var bytes = Encoding.UTF8.GetBytes(requestMethod + requestUri + requestBody);

using (var sha1 = new SHA1Managed())
using (var hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
{
var hash = sha1.ComputeHash(bytes);
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(hash);
+ Convert.ToBase64String(hasher.GetHashAndReset());
}
}
}
Expand All @@ -104,7 +108,7 @@ internal class Request
public string Uri { get; set; }
public Dictionary<string, IEnumerable<string>> Headers { get; set; }
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; }
public string Body { get; set; }
public byte[] Body { get; set; }
public Version Version { get; set; }
}

Expand All @@ -115,7 +119,7 @@ internal class Response
public string ReasonPhrase { get; set; }
public Dictionary<string, IEnumerable<string>> Headers { get; set; }
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; set; }
public string Body { get; set; }
public byte[] Body { get; set; }
public Version Version { get; set; }
}
}
28 changes: 17 additions & 11 deletions src/Cassette/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,42 @@ public static async Task<string> GetCassetteKey(this HttpRequestMessage httpRequ

public static async Task<Request> ToRequest(this HttpRequestMessage httpRequest)
{
string requestBody = await httpRequest.Content.ToStringAsync();
var request = new Request
{
Method = httpRequest.Method.ToString(),
Uri = httpRequest.RequestUri.ToString(),
Version = httpRequest.Version,
Headers = httpRequest.Headers.ToDictionary(x => x.Key, v => v.Value),
Body = requestBody
};

if (httpRequest.Content != null)
{
request.ContentHeaders = httpRequest.Content.Headers.ToDictionary(x => x.Key, v => v.Value);
await httpRequest.Content.LoadIntoBufferAsync();
request.Body = await httpRequest.Content.ReadAsByteArrayAsync();
}

return request;
}

public static async Task<Response> ToResponse(this HttpResponseMessage httpResponse)
{
string responseBody = await httpResponse.Content.ToStringAsync();

return new Response
var response = new Response
{
Status = httpResponse.StatusCode,
ReasonPhrase = httpResponse.ReasonPhrase,
Version = httpResponse.Version,
Headers = httpResponse.Headers.ToDictionary(x => x.Key, v => v.Value),
ContentHeaders = httpResponse.Content.Headers.ToDictionary(x => x.Key, v => v.Value),
Body = responseBody
};

if (httpResponse.Content != null)
{
response.ContentHeaders = httpResponse.Content.Headers.ToDictionary(x => x.Key, v => v.Value);
await httpResponse.Content.LoadIntoBufferAsync();
response.Body = await httpResponse.Content.ReadAsByteArrayAsync();
}

return response;
}

public static HttpResponseMessage Replay(this byte[] bytes) => bytes.ToCassette().Replay();
Expand Down Expand Up @@ -78,14 +84,14 @@ public static HttpRequestMessage ToHttpRequestMessage(this Request request)
return httpRequest;
}

public static HttpContent ToHttpContent(this string body)
public static HttpContent ToHttpContent(this byte[] body)
{
if (!string.IsNullOrEmpty(body))
if (body is null)
{
return new ByteArrayContent(Encoding.UTF8.GetBytes(body));
return null;
}

return null;
return new ByteArrayContent(body);
}

public static string GetUriWithoutLastSegment(this Request request)
Expand Down

0 comments on commit 5c485c1

Please sign in to comment.