|
| 1 | +using System.Net.Mime; |
| 2 | +using System.Text; |
| 3 | +using FluentAssertions; |
| 4 | +using HttpClientToCurl; |
| 5 | +using HttpClientToCurl.Utility; |
| 6 | +using Microsoft.AspNetCore.WebUtilities; |
| 7 | + |
| 8 | +namespace HttpRequestMessageToCurlGeneratorTest.FunctionalTest; |
| 9 | + |
| 10 | +public class SuccessScenariosTests |
| 11 | +{ |
| 12 | + #region :: GenerateCurlInString For Post Method :: |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void Success_GenerateCurlInString_For_PostMethod() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; |
| 19 | + |
| 20 | + var requestUri = "api/test"; |
| 21 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 22 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 23 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 24 | + // Act |
| 25 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 26 | + |
| 27 | + // Assert |
| 28 | + curlResult.Should().NotBeNullOrEmpty(); |
| 29 | + curlResult.Should().StartWith("curl -X POST"); |
| 30 | + curlResult.Trim().Should() |
| 31 | + .BeEquivalentTo( |
| 32 | + @"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void Success_GenerateCurlInString_With_RequestUri_TypeOf_Uri_For_PostMethod() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; |
| 40 | + |
| 41 | + var requestUri = Helpers.CreateUri("api/test"); |
| 42 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 43 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 44 | + |
| 45 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 46 | + |
| 47 | + // Act |
| 48 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 49 | + |
| 50 | + // Assert |
| 51 | + curlResult.Should().NotBeNullOrEmpty(); |
| 52 | + curlResult.Should().StartWith("curl -X POST"); |
| 53 | + curlResult.Trim().Should() |
| 54 | + .BeEquivalentTo( |
| 55 | + @"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void GenerateCurl_When_Set_RequestUri_Inside_HttpRequestMessage_For_PostMethod() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; |
| 63 | + |
| 64 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test") { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 65 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 66 | + |
| 67 | + using var httpClient = new HttpClient(); |
| 68 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 69 | + |
| 70 | + // Act |
| 71 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 72 | + |
| 73 | + // Assert |
| 74 | + curlResult.Should().NotBeNullOrEmpty(); |
| 75 | + curlResult.Should().StartWith("curl -X POST"); |
| 76 | + curlResult.Trim().Should() |
| 77 | + .BeEquivalentTo( |
| 78 | + @"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_PostMethod() |
| 83 | + { |
| 84 | + // Arrange |
| 85 | + string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; |
| 86 | + |
| 87 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, string.Empty) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 88 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 89 | + |
| 90 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 91 | + |
| 92 | + // Act |
| 93 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 94 | + |
| 95 | + // Assert |
| 96 | + curlResult.Should().NotBeNullOrEmpty(); |
| 97 | + curlResult.Should().StartWith("curl -X POST"); |
| 98 | + curlResult.Trim().Should() |
| 99 | + .BeEquivalentTo( |
| 100 | + @"curl -X POST 'http://localhost:1213/v1/' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void Success_GenerateCurlInString_When_RequestBody_Is_Null_For_PostMethod() |
| 105 | + { |
| 106 | + // Arrange |
| 107 | + var requestUri = "api/test"; |
| 108 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 109 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 110 | + |
| 111 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 112 | + |
| 113 | + // Act |
| 114 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 115 | + |
| 116 | + // Assert |
| 117 | + curlResult.Should().NotBeNullOrEmpty(); |
| 118 | + curlResult.Should().StartWith("curl -X POST"); |
| 119 | + curlResult.Trim().Should() |
| 120 | + .BeEquivalentTo( |
| 121 | + @"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d ''"); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void Success_GenerateCurlInString_When_HttpContent_Is_Null_For_PostMethod() |
| 126 | + { |
| 127 | + // Arrange |
| 128 | + var requestUri = "api/test"; |
| 129 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri); |
| 130 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 131 | + |
| 132 | + using var httpClient = new HttpClient(); |
| 133 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 134 | + |
| 135 | + // Act |
| 136 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 137 | + |
| 138 | + // Assert |
| 139 | + curlResult.Should().NotBeNullOrEmpty(); |
| 140 | + curlResult.Should().StartWith("curl -X POST"); |
| 141 | + curlResult.Trim().Should().BeEquivalentTo(@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -d ''"); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public void Success_GenerateCurlInString_With_Multiple_Value_For_A_Header_PostMethod() |
| 146 | + { |
| 147 | + // Arrange |
| 148 | + string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; |
| 149 | + |
| 150 | + var requestUri = "api/test"; |
| 151 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 152 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61"); |
| 153 | + |
| 154 | + List<string?> headerValues = ["_ga=GA1.1.41226618.1701506283", "mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05", "sp_ses.13cb=*"]; |
| 155 | + httpRequestMessage.Headers.Add("cookie", headerValues); |
| 156 | + |
| 157 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 158 | + |
| 159 | + // Act |
| 160 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 161 | + |
| 162 | + // Assert |
| 163 | + curlResult.Should().NotBeNullOrEmpty(); |
| 164 | + curlResult.Should().StartWith("curl -X POST"); |
| 165 | + curlResult.Trim().Should() |
| 166 | + .BeEquivalentTo( |
| 167 | + @"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Cookie: _ga=GA1.1.41226618.1701506283; mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05; sp_ses.13cb=*' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"); |
| 168 | + } |
| 169 | + |
| 170 | + #endregion |
| 171 | + |
| 172 | + #region :: GenerateCurlInString For Get Method :: |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void Success_GenerateCurlInString_For_GetMethod() |
| 176 | + { |
| 177 | + // Arrange |
| 178 | + var requestUri = "api/test"; |
| 179 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 180 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9"); |
| 181 | + |
| 182 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 183 | + |
| 184 | + // Act |
| 185 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 186 | + |
| 187 | + // Assert |
| 188 | + curlResult.Should().NotBeNullOrEmpty(); |
| 189 | + curlResult.Should().StartWith("curl"); |
| 190 | + curlResult.Trim().Should() |
| 191 | + .BeEquivalentTo( |
| 192 | + @"curl 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"); |
| 193 | + } |
| 194 | + |
| 195 | + [Fact] |
| 196 | + public void Success_GenerateCurlInString_With_QueryString_For_GetMethod() |
| 197 | + { |
| 198 | + // Arrange |
| 199 | + var queryString = new Dictionary<string, string> { { "id", "12" } }; |
| 200 | + var requestUri = QueryHelpers.AddQueryString("api/test", queryString!); |
| 201 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 202 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9"); |
| 203 | + |
| 204 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 205 | + |
| 206 | + // Act |
| 207 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 208 | + |
| 209 | + // Assert |
| 210 | + curlResult.Should().NotBeNullOrEmpty(); |
| 211 | + curlResult.Should().StartWith("curl"); |
| 212 | + curlResult.Trim().Should() |
| 213 | + .BeEquivalentTo( |
| 214 | + @"curl 'http://localhost:1213/v1/api/test?id=12' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"); |
| 215 | + } |
| 216 | + |
| 217 | + [Fact] |
| 218 | + public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_GetMethod() |
| 219 | + { |
| 220 | + // Arrange |
| 221 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, string.Empty) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 222 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9"); |
| 223 | + |
| 224 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 225 | + |
| 226 | + // Act |
| 227 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 228 | + |
| 229 | + // Assert |
| 230 | + curlResult.Should().NotBeNullOrEmpty(); |
| 231 | + curlResult.Should().StartWith("curl"); |
| 232 | + curlResult.Trim().Should() |
| 233 | + .BeEquivalentTo(@"curl 'http://localhost:1213/v1/' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"); |
| 234 | + } |
| 235 | + |
| 236 | + [Fact] |
| 237 | + public void Success_GenerateCurlInString_With_Multiple_Value_For_A_Header_GetMethod() |
| 238 | + { |
| 239 | + // Arrange |
| 240 | + var requestUri = "api/test"; |
| 241 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) }; |
| 242 | + httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9"); |
| 243 | + |
| 244 | + List<string?> headerValues = ["_ga=GA1.1.41226618.1701506283", "mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05", "sp_ses.13cb=*"]; |
| 245 | + httpRequestMessage.Headers.Add("cookie", headerValues); |
| 246 | + |
| 247 | + var baseAddress = new Uri("http://localhost:1213/v1/"); |
| 248 | + |
| 249 | + // Act |
| 250 | + string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress); |
| 251 | + |
| 252 | + // Assert |
| 253 | + curlResult.Should().NotBeNullOrEmpty(); |
| 254 | + curlResult.Should().StartWith("curl"); |
| 255 | + curlResult.Trim().Should() |
| 256 | + .BeEquivalentTo( |
| 257 | + @"curl 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Cookie: _ga=GA1.1.41226618.1701506283; mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05; sp_ses.13cb=*' -H 'Content-Type: application/json; charset=utf-8'"); |
| 258 | + } |
| 259 | + |
| 260 | + #endregion |
| 261 | +} |
0 commit comments