-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathTestApiRequest.cs
More file actions
94 lines (77 loc) · 3.38 KB
/
TestApiRequest.cs
File metadata and controls
94 lines (77 loc) · 3.38 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
// <copyright file="TestApiRequest.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Datadog.Trace.Agent;
using Datadog.Trace.Agent.Transports;
namespace Datadog.Trace.TestHelpers.TransportHelpers;
internal class TestApiRequest : IApiRequest
{
private readonly int _statusCode;
private readonly string _responseContent;
private readonly string _responseContentType;
private readonly Dictionary<string, string> _responseHeaders;
public TestApiRequest(
Uri endpoint,
int statusCode = 200,
string responseContent = "{}",
string responseContentType = "application/json",
Dictionary<string, string> responseHeaders = null)
{
_statusCode = statusCode;
_responseContent = responseContent;
_responseContentType = responseContentType;
_responseHeaders = responseHeaders;
Endpoint = endpoint;
}
public Uri Endpoint { get; }
public string ContentType { get; private set; }
public Dictionary<string, string> ExtraHeaders { get; } = new();
public List<TestApiResponse> Responses { get; } = new();
public void AddHeader(string name, string value)
{
ExtraHeaders.Add(name, value);
}
public virtual Task<IApiResponse> GetAsync()
{
var response = new TestApiResponse(_statusCode, _responseContent, _responseContentType, _responseHeaders);
Responses.Add(response);
return Task.FromResult((IApiResponse)response);
}
public Task<IApiResponse> PostAsync(ArraySegment<byte> traces, string contentType)
=> PostAsync(traces, contentType, contentEncoding: null);
public virtual Task<IApiResponse> PostAsync(ArraySegment<byte> bytes, string contentType, string contentEncoding)
{
var response = new TestApiResponse(_statusCode, _responseContent, _responseContentType, _responseHeaders);
Responses.Add(response);
ContentType = contentType;
return Task.FromResult((IApiResponse)response);
}
public async Task<IApiResponse> PostAsync(Func<Stream, Task> writeToRequestStream, string contentType, string contentEncoding, string multipartBoundary)
{
using (var ms = new MemoryStream())
{
await writeToRequestStream(ms);
return await PostAsync(new ArraySegment<byte>(ms.ToArray()), ContentTypeHelper.GetContentType(contentType, multipartBoundary), contentEncoding);
}
}
public async Task<IApiResponse> PostAsync(MultipartFormItem[] items, MultipartCompression multipartCompression = MultipartCompression.None)
{
var boundary = "----not implemented" + Guid.NewGuid().ToString("N");
var contentType = ContentTypeHelper.GetContentType("multipart/form-data", boundary);
return await PostAsync(
async stream =>
{
using var writer = new StreamWriter(stream, Encoding.UTF8);
await writer.WriteAsync("not implemented \r\n");
},
contentType,
"utf-8",
boundary);
}
}