Skip to content

Commit 84e5590

Browse files
committed
feat: implemented "SetAuthorization" method in Request
1 parent 16fc840 commit 84e5590

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Web.Test/RequestTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ public async Task SendWebpImageRequest()
104104
await SendImageRequest("image/webp", "webp");
105105
}
106106

107+
[TestMethod]
108+
public void CheckHeaders()
109+
{
110+
Request request = new("");
111+
request
112+
.AddHeader("Authorization", "test1")
113+
.SetAuthorization("test2")
114+
.SetAuthorization("test3");
115+
116+
KeyValuePair<string, string>? authorizationHeader = request
117+
.RequestHeaders
118+
.Where(rh => rh.Key == "Authorization")
119+
.FirstOrDefault();
120+
121+
Assert.IsNotNull(authorizationHeader, "Authorization header should be set");
122+
Assert.AreEqual("test3", authorizationHeader.Value.Value, "Authorization header value should be: test3");
123+
}
124+
107125
private async Task SendImageRequest(string acceptType, string imageType)
108126
{
109127
Request request = new(TestGetWebpImageUrl, HttpMethod.Get);

Web/API/Request.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ public Request AddHeaders(Dictionary<string, string> headers)
124124
return this;
125125
}
126126

127+
/// <summary>
128+
/// Sets the authorization header.
129+
/// In case of an existing Authorization header, its value
130+
/// will be replaced with the last set.
131+
/// </summary>
132+
/// <param name="value">Authorization header value</param>
133+
/// <returns>Instance</returns>
134+
public Request SetAuthorization(string value)
135+
{
136+
KeyValuePair<string, string>? existingAuthorization = RequestHeaders
137+
.Where(rh => rh.Key == "Authorization")
138+
.FirstOrDefault();
139+
140+
if (existingAuthorization == null)
141+
{
142+
AddHeader("Authorization", value);
143+
}
144+
else
145+
{
146+
RequestHeaders[existingAuthorization.Value.Key] = value;
147+
}
148+
149+
return this;
150+
}
151+
127152
/// <summary>
128153
/// Adds a content header to the request
129154
/// </summary>

0 commit comments

Comments
 (0)