|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Net;
|
3 | 4 | using System.Net.Http;
|
| 5 | +using System.Net.Http.Headers; |
4 | 6 | using System.Threading.Tasks;
|
5 | 7 | using Xunit;
|
6 | 8 |
|
@@ -122,5 +124,88 @@ public async Task Timeouts()
|
122 | 124 | Assert.Equal("https://httpbin.org/delay/10", err.Uri.ToString());
|
123 | 125 | Assert.Null(err.StatusCode);
|
124 | 126 | }
|
| 127 | + |
| 128 | + [Fact] |
| 129 | + public async Task AddHeaderWithoutValidation() |
| 130 | + { |
| 131 | + var result = await Http.Request("https://httpbin.org/bearer") |
| 132 | + .AddHeaderWithoutValidation("Authorization","abcd") |
| 133 | + .ExpectJson<HttpBinResponse>() |
| 134 | + .GetAsync(); |
| 135 | + Assert.True(result.RawRequest.Headers.Contains("Authorization")); |
| 136 | + Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public async Task AddHeaders() |
| 141 | + { |
| 142 | + var result = await Http.Request("https://httpbin.org/headers") |
| 143 | + .AddHeaders(new Dictionary<string, string>() |
| 144 | + { |
| 145 | + {"Content-Type", "application/json"}, |
| 146 | + {"Custom", "Test"} |
| 147 | + }) |
| 148 | + .SendJson("{}") |
| 149 | + .ExpectJson<HttpBinResponse>() |
| 150 | + .GetAsync(); |
| 151 | + |
| 152 | + Assert.Equal(HttpStatusCode.OK,result.StatusCode); |
| 153 | + Assert.Equal("Test", result.Data.Headers["Custom"]); |
| 154 | + // Content-Type should be present because we're sending a body up |
| 155 | + Assert.StartsWith("application/json", result.Data.Headers["Content-Type"]); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public async Task TestAddHeadersWhereClientAddsHeaderBeforeContent() |
| 160 | + { |
| 161 | + var result = await Http.Request("https://httpbin.org/headers") |
| 162 | + .AddHeaders(new Dictionary<string, string>() |
| 163 | + { |
| 164 | + {"Content-Type", "application/json"}, |
| 165 | + {"Custom", "Test"} |
| 166 | + }) |
| 167 | + .SendJson("") |
| 168 | + .ExpectJson<HttpBinResponse>() |
| 169 | + .GetAsync(); |
| 170 | + |
| 171 | + Assert.Equal(HttpStatusCode.OK,result.StatusCode); |
| 172 | + Assert.Equal("Test", result.Data.Headers["Custom"]); |
| 173 | + Assert.Equal("application/json; charset=utf-8", result.Data.Headers["Content-Type"]); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public async Task TestAddHeadersWhereClientAddsHeaderAndNoContent() |
| 178 | + { |
| 179 | + var result = await Http.Request("https://httpbin.org/headers") |
| 180 | + .AddHeaders(new Dictionary<string, string>() |
| 181 | + { |
| 182 | + {"Content-Type", "application/json"}, |
| 183 | + {"Custom", "Test"} |
| 184 | + }) |
| 185 | + .ExpectJson<HttpBinResponse>() |
| 186 | + .GetAsync(); |
| 187 | + |
| 188 | + Assert.Equal(HttpStatusCode.OK,result.StatusCode); |
| 189 | + Assert.Equal("Test", result.Data.Headers["Custom"]); |
| 190 | + // Content-Type is NOT sent when there's no body - this is correct behavior |
| 191 | + Assert.DoesNotContain("Content-Type", result.Data.Headers.Keys); |
| 192 | + } |
| 193 | + |
| 194 | + [Fact] |
| 195 | + public async Task PatchRequest() |
| 196 | + { |
| 197 | + var guid = Guid.NewGuid().ToString(); |
| 198 | + var result = await Http.Request("https://httpbin.org/patch") |
| 199 | + .SendPlaintext(guid) |
| 200 | + .ExpectJson<HttpBinResponse>() |
| 201 | + .PatchAsync(); |
| 202 | + |
| 203 | + Assert.True(result.Success); |
| 204 | + Assert.Equal(HttpStatusCode.OK, result.StatusCode); |
| 205 | + Assert.NotNull(result); |
| 206 | + Assert.True(result.Data.Form.ContainsKey(guid)); |
| 207 | + Assert.Equal("https://httpbin.org/patch", result.Data.Url); |
| 208 | + Assert.Equal(Http.DefaultSettings.UserAgent, result.Data.Headers["User-Agent"]); |
| 209 | + } |
125 | 210 | }
|
126 | 211 | }
|
0 commit comments