-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoggingHttpHandler.cs
More file actions
110 lines (95 loc) · 3.57 KB
/
LoggingHttpHandler.cs
File metadata and controls
110 lines (95 loc) · 3.57 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Contentstack.Management.Core.Tests.Helpers
{
public class LoggingHttpHandler : DelegatingHandler
{
public LoggingHttpHandler() : base(new HttpClientHandler()) { }
public LoggingHttpHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
await CaptureRequest(request);
}
catch
{
// Never let logging break the request
}
var response = await base.SendAsync(request, cancellationToken);
try
{
await CaptureResponse(response);
}
catch
{
// Never let logging break the response
}
return response;
}
private async Task CaptureRequest(HttpRequestMessage request)
{
var headers = new Dictionary<string, string>();
foreach (var h in request.Headers)
headers[h.Key] = string.Join(", ", h.Value);
string body = null;
if (request.Content != null)
{
foreach (var h in request.Content.Headers)
headers[h.Key] = string.Join(", ", h.Value);
await request.Content.LoadIntoBufferAsync();
body = await request.Content.ReadAsStringAsync();
}
var curl = BuildCurl(request.Method.ToString(), request.RequestUri?.ToString(), headers, body);
TestOutputLogger.LogHttpRequest(
method: request.Method.ToString(),
url: request.RequestUri?.ToString() ?? "",
headers: headers,
body: body ?? "",
curlCommand: curl,
sdkMethod: ""
);
}
private async Task CaptureResponse(HttpResponseMessage response)
{
var headers = new Dictionary<string, string>();
foreach (var h in response.Headers)
headers[h.Key] = string.Join(", ", h.Value);
string body = null;
if (response.Content != null)
{
foreach (var h in response.Content.Headers)
headers[h.Key] = string.Join(", ", h.Value);
await response.Content.LoadIntoBufferAsync();
body = await response.Content.ReadAsStringAsync();
}
TestOutputLogger.LogHttpResponse(
statusCode: (int)response.StatusCode,
statusText: response.ReasonPhrase ?? response.StatusCode.ToString(),
headers: headers,
body: body ?? ""
);
}
private static string BuildCurl(string method, string url,
IDictionary<string, string> headers, string body)
{
var sb = new StringBuilder();
sb.Append($"curl -X {method} \\\n");
sb.Append($" '{url}' \\\n");
foreach (var kv in headers)
sb.Append($" -H '{kv.Key}: {kv.Value}' \\\n");
if (!string.IsNullOrEmpty(body))
{
var escaped = body.Replace("'", "'\\''");
sb.Append($" -d '{escaped}'");
}
return sb.ToString().TrimEnd('\\', '\n', ' ');
}
}
}