-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTest.cs
More file actions
157 lines (127 loc) · 4.66 KB
/
RequestTest.cs
File metadata and controls
157 lines (127 loc) · 4.66 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using FrApp42.Web.API;
using System.Text;
using Web.Test.Models;
namespace Web.Test
{
[TestClass]
public class RequestTest
{
private const string TestGetUrl = "https://httpbin.org/get";
private const string TestDeleteUrl = "https://httpbin.org/delete";
private const string TestPatchUrl = "https://httpbin.org/patch";
private const string TestPostUrl = "https://httpbin.org/post";
private const string TestPutUrl = "https://httpbin.org/put";
private const string TestGetJpegImageUrl = "https://httpbin.org/image/jpeg";
private const string TestGetPngImageUrl = "https://httpbin.org/image/png";
private const string TestGetSvgImageUrl = "https://httpbin.org/image/svg";
private const string TestGetWebpImageUrl = "https://httpbin.org/image/webp";
[TestMethod]
public async Task SendGetRequest()
{
Request request = new(TestGetUrl, HttpMethod.Get);
Result<HttpBinGetResponse> result = await request.Run<HttpBinGetResponse>();
AssertResponse(result, TestGetUrl);
}
[TestMethod]
public async Task SendDeleteRequest()
{
Request request = new(TestDeleteUrl, HttpMethod.Delete);
Result<HttpBinDeleteResponse> result = await request.Run<HttpBinDeleteResponse>();
AssertResponse(result, TestDeleteUrl);
}
[TestMethod]
public async Task SendPatchRequest()
{
Request request = new(TestPatchUrl, HttpMethod.Patch);
Result<HttpBinPatchResponse> result = await request.Run<HttpBinPatchResponse>();
AssertResponse(result, TestPatchUrl);
}
[TestMethod]
public async Task SendPostRequest()
{
Request request = new(TestPostUrl, HttpMethod.Post);
Result<HttpBinPostResponse> result = await request.Run<HttpBinPostResponse>();
AssertResponse(result, TestPostUrl);
}
[TestMethod]
public async Task SendPutRequest()
{
Request request = new(TestPutUrl, HttpMethod.Put);
Result<HttpBinPutResponse> result = await request.Run<HttpBinPutResponse>();
AssertResponse(result, TestPutUrl);
}
[TestMethod]
public async Task SendPostRequestWithFile()
{
byte[] fileBytes = Encoding.UTF8.GetBytes("Hello world");
string fileName = "test.txt";
Request request = new(TestPostUrl, HttpMethod.Post);
request
.SetContentType("text/plain")
.AddByteBody(fileBytes, fileName);
Result<HttpBinPostFileResponse> result = await request.RunDocument<HttpBinPostFileResponse>();
AssertResponse(result, TestPostUrl);
StringAssert.Contains(result.Value?.Data, "Hello world", "File content should contain 'Hello World'");
}
[TestMethod]
public async Task SendJpegImageRequest()
{
await SendImageRequest("image/jpeg", "jpeg");
}
[TestMethod]
public async Task SendPngImageRequest()
{
await SendImageRequest("image/png", "png");
}
[TestMethod]
public async Task SendSvgImageRequest()
{
await SendImageRequest("image/svg+xml", "svg");
}
[TestMethod]
public async Task SendWebpImageRequest()
{
await SendImageRequest("image/webp", "webp");
}
[TestMethod]
public void CheckHeaders()
{
Request request = new("");
request
.AddHeader("Authorization", "test1")
.SetAuthorization("test2")
.SetAuthorization("test3");
KeyValuePair<string, string>? authorizationHeader = request
.RequestHeaders
.Where(rh => rh.Key == "Authorization")
.FirstOrDefault();
Assert.IsNotNull(authorizationHeader, "Authorization header should be set");
Assert.AreEqual("test3", authorizationHeader.Value.Value, "Authorization header value should be: test3");
}
private async Task SendImageRequest(string acceptType, string imageType)
{
Request request = new(TestGetWebpImageUrl, HttpMethod.Get);
request
.AddHeader("Accept", acceptType);
Result<byte[]> result = await request.RunGetBytes();
AssertImageResponse(result, imageType);
}
private void AssertResponse<T>(Result<T> result, string expectedUrl) where T : class
{
Assert.AreEqual(200, result.StatusCode, "Status code should be 200");
Assert.IsNotNull(result.Value, "Response value should not be null");
dynamic response = result.Value;
Assert.AreEqual(expectedUrl, response.Url, "The response URL should match the request URL");
dynamic headers = response.Headers;
Assert.IsNotNull(headers, "Headers should not be null");
Assert.AreEqual("httpbin.org", headers.Host, "Host header should be 'httpbin.org'");
}
private void AssertImageResponse(Result<byte[]> result, string imgType)
{
Assert.AreEqual(200, result.StatusCode, "Status code should be 200");
Assert.IsNotNull(result.Value, "Response value should not be null");
Assert.IsTrue(result.Value.Length > 0, "Image data should not be empty");
File.WriteAllBytes($"test_image.{imgType}", result.Value);
}
}
}