|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text; |
| 7 | +using Xunit; |
| 8 | +namespace Microsoft.AspNetCore.Http.Internal.Tests |
| 9 | +{ |
| 10 | + public class BindingAddressTests |
| 11 | + { |
| 12 | + [Theory] |
| 13 | + [InlineData("")] |
| 14 | + [InlineData("5000")] |
| 15 | + [InlineData("//noscheme")] |
| 16 | + public void FromUriThrowsForUrlsWithoutSchemeDelimiter(string url) |
| 17 | + { |
| 18 | + Assert.Throws<FormatException>(() => BindingAddress.Parse(url)); |
| 19 | + } |
| 20 | + |
| 21 | + [Theory] |
| 22 | + [InlineData("://")] |
| 23 | + [InlineData("://:5000")] |
| 24 | + [InlineData("http://")] |
| 25 | + [InlineData("http://:5000")] |
| 26 | + [InlineData("http:///")] |
| 27 | + [InlineData("http:///:5000")] |
| 28 | + [InlineData("http:////")] |
| 29 | + [InlineData("http:////:5000")] |
| 30 | + public void FromUriThrowsForUrlsWithoutHost(string url) |
| 31 | + { |
| 32 | + Assert.Throws<FormatException>(() => BindingAddress.Parse(url)); |
| 33 | + } |
| 34 | + |
| 35 | + [Theory] |
| 36 | + [InlineData("://emptyscheme", "", "emptyscheme", 0, "", "://emptyscheme:0")] |
| 37 | + [InlineData("http://+", "http", "+", 80, "", "http://+:80")] |
| 38 | + [InlineData("http://*", "http", "*", 80, "", "http://*:80")] |
| 39 | + [InlineData("http://localhost", "http", "localhost", 80, "", "http://localhost:80")] |
| 40 | + [InlineData("http://www.example.com", "http", "www.example.com", 80, "", "http://www.example.com:80")] |
| 41 | + [InlineData("https://www.example.com", "https", "www.example.com", 443, "", "https://www.example.com:443")] |
| 42 | + [InlineData("http://www.example.com/", "http", "www.example.com", 80, "", "http://www.example.com:80")] |
| 43 | + [InlineData("http://www.example.com/foo?bar=baz", "http", "www.example.com", 80, "/foo?bar=baz", "http://www.example.com:80/foo?bar=baz")] |
| 44 | + [InlineData("http://www.example.com:5000", "http", "www.example.com", 5000, "", null)] |
| 45 | + [InlineData("https://www.example.com:5000", "https", "www.example.com", 5000, "", null)] |
| 46 | + [InlineData("http://www.example.com:5000/", "http", "www.example.com", 5000, "", "http://www.example.com:5000")] |
| 47 | + [InlineData("http://www.example.com:NOTAPORT", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")] |
| 48 | + [InlineData("https://www.example.com:NOTAPORT", "https", "www.example.com:NOTAPORT", 443, "", "https://www.example.com:notaport:443")] |
| 49 | + [InlineData("http://www.example.com:NOTAPORT/", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")] |
| 50 | + [InlineData("http://foo:/tmp/kestrel-test.sock:5000/doesn't/matter", "http", "foo:", 80, "/tmp/kestrel-test.sock:5000/doesn't/matter", "http://foo::80/tmp/kestrel-test.sock:5000/doesn't/matter")] |
| 51 | + [InlineData("http://unix:foo/tmp/kestrel-test.sock", "http", "unix:foo", 80, "/tmp/kestrel-test.sock", "http://unix:foo:80/tmp/kestrel-test.sock")] |
| 52 | + [InlineData("http://unix:5000/tmp/kestrel-test.sock", "http", "unix", 5000, "/tmp/kestrel-test.sock", "http://unix:5000/tmp/kestrel-test.sock")] |
| 53 | + [InlineData("http://unix:/tmp/kestrel-test.sock", "http", "unix:/tmp/kestrel-test.sock", 0, "", null)] |
| 54 | + [InlineData("https://unix:/tmp/kestrel-test.sock", "https", "unix:/tmp/kestrel-test.sock", 0, "", null)] |
| 55 | + [InlineData("http://unix:/tmp/kestrel-test.sock:", "http", "unix:/tmp/kestrel-test.sock", 0, "", "http://unix:/tmp/kestrel-test.sock")] |
| 56 | + [InlineData("http://unix:/tmp/kestrel-test.sock:/", "http", "unix:/tmp/kestrel-test.sock", 0, "", "http://unix:/tmp/kestrel-test.sock")] |
| 57 | + [InlineData("http://unix:/tmp/kestrel-test.sock:5000/doesn't/matter", "http", "unix:/tmp/kestrel-test.sock", 0, "5000/doesn't/matter", "http://unix:/tmp/kestrel-test.sock")] |
| 58 | + public void UrlsAreParsedCorrectly(string url, string scheme, string host, int port, string pathBase, string toString) |
| 59 | + { |
| 60 | + var serverAddress = BindingAddress.Parse(url); |
| 61 | + |
| 62 | + Assert.Equal(scheme, serverAddress.Scheme); |
| 63 | + Assert.Equal(host, serverAddress.Host); |
| 64 | + Assert.Equal(port, serverAddress.Port); |
| 65 | + Assert.Equal(pathBase, serverAddress.PathBase); |
| 66 | + |
| 67 | + Assert.Equal(toString ?? url, serverAddress.ToString()); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments