|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace KubeClient.Tests.Http.BuildMessage |
| 6 | +{ |
| 7 | + using KubeClient.Http; |
| 8 | + using KubeClient.Http.Testability.Xunit; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Message-building tests for <see cref="HttpRequest"/> (<see cref="HttpRequest.BuildRequestMessage"/>). |
| 12 | + /// </summary> |
| 13 | + public class UntypedRequest |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// A request with an absolute URI. |
| 17 | + /// </summary> |
| 18 | + static readonly HttpRequest AbsoluteRequest = HttpRequest.Create("http://localhost:1234"); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// A request with a relative URI. |
| 22 | + /// </summary> |
| 23 | + static readonly HttpRequest RelativeRequest = HttpRequest.Create("foo/bar"); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// An <see cref="HttpRequest"/> throws <see cref="InvalidOperationException"/>. |
| 27 | + /// </summary> |
| 28 | + [Fact] |
| 29 | + public void Empty_Throws() |
| 30 | + { |
| 31 | + Assert.Throws<InvalidOperationException>(() => |
| 32 | + { |
| 33 | + HttpRequest.Empty.BuildRequestMessage(HttpMethod.Get); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// An <see cref="HttpRequest"/> with a relative URI throws <see cref="InvalidOperationException"/> if no base URI is supplied. |
| 39 | + /// </summary> |
| 40 | + [Fact] |
| 41 | + public void RelativeUri_NoBaseUri_Throws() |
| 42 | + { |
| 43 | + Assert.Throws<InvalidOperationException>(() => |
| 44 | + { |
| 45 | + RelativeRequest.BuildRequestMessage(HttpMethod.Get); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// An <see cref="HttpRequest"/> with a relative URI prepends the supplied base URI to the request URI. |
| 51 | + /// </summary> |
| 52 | + [Fact] |
| 53 | + public void RelativeUri_BaseUri_PrependsBaseUri() |
| 54 | + { |
| 55 | + Uri baseUri = new Uri("http://tintoy.io:5678/"); |
| 56 | + |
| 57 | + RequestAssert.MessageHasUri(RelativeRequest, baseUri, |
| 58 | + expectedUri: new Uri(baseUri, RelativeRequest.Uri) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// An <see cref="HttpRequest"/> with an absolute URI ignores the lack of a base URI and uses the request URI. |
| 64 | + /// </summary> |
| 65 | + [Fact] |
| 66 | + public void AbsoluteUri_NoBaseUri_UsesRequestUri() |
| 67 | + { |
| 68 | + RequestAssert.MessageHasUri(AbsoluteRequest, |
| 69 | + expectedUri: AbsoluteRequest.Uri |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// An <see cref="HttpRequest"/> with an absolute URI ignores the supplied base URI and uses the request URI. |
| 75 | + /// </summary> |
| 76 | + [Fact] |
| 77 | + public void AbsoluteUri_BaseUri_UsesRequestUri() |
| 78 | + { |
| 79 | + Uri baseUri = new Uri("http://tintoy.io:5678/"); |
| 80 | + |
| 81 | + RequestAssert.MessageHasUri(AbsoluteRequest, baseUri, |
| 82 | + expectedUri: AbsoluteRequest.Uri |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + #region Template URIs |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// An <see cref="HttpRequest"/> with an absolute template URI, using statically-bound template parameters. |
| 90 | + /// </summary> |
| 91 | + [Fact] |
| 92 | + public void Absoluteuri_Template() |
| 93 | + { |
| 94 | + HttpRequest request = |
| 95 | + HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}") |
| 96 | + .WithTemplateParameter("action", "foo") |
| 97 | + .WithTemplateParameter("id", "bar"); |
| 98 | + |
| 99 | + RequestAssert.MessageHasUri(request, |
| 100 | + expectedUri: "http://localhost:1234/foo/bar" |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// An <see cref="HttpRequest"/> with an absolute template URI, using dynamically-bound template parameters. |
| 106 | + /// </summary> |
| 107 | + [Fact] |
| 108 | + public void AbsoluteUri_Template_DeferredValues() |
| 109 | + { |
| 110 | + string action = "foo"; |
| 111 | + string id = "bar"; |
| 112 | + |
| 113 | + HttpRequest request = |
| 114 | + HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}") |
| 115 | + .WithTemplateParameter("action", () => action) |
| 116 | + .WithTemplateParameter("id", () => id); |
| 117 | + |
| 118 | + RequestAssert.MessageHasUri(request, |
| 119 | + expectedUri: "http://localhost:1234/foo/bar" |
| 120 | + ); |
| 121 | + |
| 122 | + action = "diddly"; |
| 123 | + id = "dee"; |
| 124 | + |
| 125 | + RequestAssert.MessageHasUri(request, |
| 126 | + expectedUri: "http://localhost:1234/diddly/dee" |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// An <see cref="HttpRequest"/> with an absolute template URI that includes a query component, using statically-bound template parameters. |
| 132 | + /// </summary> |
| 133 | + [Fact] |
| 134 | + public void AbsoluteUri_Template_Query() |
| 135 | + { |
| 136 | + HttpRequest request = |
| 137 | + HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}?flag={flag}") |
| 138 | + .WithTemplateParameter("action", "foo") |
| 139 | + .WithTemplateParameter("id", "bar") |
| 140 | + .WithTemplateParameter("flag", true); |
| 141 | + |
| 142 | + RequestAssert.MessageHasUri(request, |
| 143 | + expectedUri: "http://localhost:1234/foo/bar?flag=True" |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// An <see cref="HttpRequest"/> with an absolute template URI that includes a query component, using dynamically-bound template parameters. |
| 149 | + /// </summary> |
| 150 | + [Fact] |
| 151 | + public void AbsoluteUri_Template_Query_DeferredValues() |
| 152 | + { |
| 153 | + string action = "foo"; |
| 154 | + string id = "bar"; |
| 155 | + bool? flag = true; |
| 156 | + |
| 157 | + HttpRequest request = |
| 158 | + HttpRequest.Factory.Create("http://localhost:1234/") |
| 159 | + .WithRelativeUri("{action}/{id}?flag={flag?}") |
| 160 | + .WithTemplateParameter("action", () => action) |
| 161 | + .WithTemplateParameter("id", () => id) |
| 162 | + .WithTemplateParameter("flag", () => flag); |
| 163 | + |
| 164 | + RequestAssert.MessageHasUri(request, |
| 165 | + expectedUri: "http://localhost:1234/foo/bar?flag=True" |
| 166 | + ); |
| 167 | + |
| 168 | + action = "diddly"; |
| 169 | + id = "dee"; |
| 170 | + flag = null; |
| 171 | + |
| 172 | + RequestAssert.MessageHasUri(request, |
| 173 | + expectedUri: "http://localhost:1234/diddly/dee" |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + #endregion // Template URIs |
| 178 | + |
| 179 | + #region Query parameters |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, using statically-bound template parameters. |
| 183 | + /// </summary> |
| 184 | + [Fact] |
| 185 | + public void AbsoluteUri_Query() |
| 186 | + { |
| 187 | + HttpRequest request = |
| 188 | + HttpRequest.Factory.Create("http://localhost:1234/foo/bar") |
| 189 | + .WithQueryParameter("flag", true); |
| 190 | + |
| 191 | + RequestAssert.MessageHasUri(request, |
| 192 | + expectedUri: "http://localhost:1234/foo/bar?flag=True" |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + /// <summary> |
| 197 | + /// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, using dynamically-bound template parameters. |
| 198 | + /// </summary> |
| 199 | + [Fact] |
| 200 | + public void AbsoluteUri_AddQuery_DeferredValues() |
| 201 | + { |
| 202 | + bool? flag = true; |
| 203 | + |
| 204 | + HttpRequest request = |
| 205 | + HttpRequest.Factory.Create("http://localhost:1234/foo/bar") |
| 206 | + .WithQueryParameter("flag", () => flag); |
| 207 | + |
| 208 | + RequestAssert.MessageHasUri(request, |
| 209 | + expectedUri: "http://localhost:1234/foo/bar?flag=True" |
| 210 | + ); |
| 211 | + |
| 212 | + flag = null; |
| 213 | + |
| 214 | + RequestAssert.MessageHasUri(request, |
| 215 | + expectedUri: "http://localhost:1234/foo/bar" |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + /// <summary> |
| 220 | + /// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, with no additional path component). |
| 221 | + /// </summary> |
| 222 | + [Fact] |
| 223 | + public void AbsoluteUri_AddQuery_EmptyPath() |
| 224 | + { |
| 225 | + HttpRequest request = |
| 226 | + AbsoluteRequest.WithRelativeUri("foo/bar") |
| 227 | + .WithRelativeUri("?baz=bonk"); |
| 228 | + |
| 229 | + RequestAssert.MessageHasUri(request, |
| 230 | + expectedUri: "http://localhost:1234/foo/bar?baz=bonk" |
| 231 | + ); |
| 232 | + } |
| 233 | + |
| 234 | + /// <summary> |
| 235 | + /// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, with no additional path component). |
| 236 | + /// </summary> |
| 237 | + [Fact] |
| 238 | + public void AbsoluteUri_WithQuery_AddQuery_EmptyPath() |
| 239 | + { |
| 240 | + HttpRequest request = |
| 241 | + AbsoluteRequest.WithRelativeUri("foo/bar?baz=bonk") |
| 242 | + .WithRelativeUri("?bo=diddly"); |
| 243 | + |
| 244 | + RequestAssert.MessageHasUri(request, |
| 245 | + expectedUri: "http://localhost:1234/foo/bar?baz=bonk&bo=diddly" |
| 246 | + ); |
| 247 | + } |
| 248 | + |
| 249 | + #endregion // Query parameters |
| 250 | + } |
| 251 | +} |
0 commit comments