forked from NewLifeX/NewLife.Cube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyUriTests.cs
81 lines (74 loc) · 2.33 KB
/
MyUriTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using NewLife;
using Xunit;
namespace XUnitTest;
public class MyUriTests
{
[Theory]
[InlineData("http://localhost:8080/cube/info", "http", "localhost", 8080, "/cube/info")]
[InlineData("http://localhost:8080/", "http", "localhost", 8080, "/")]
[InlineData("Http://localhost/", "Http", "localhost", 0, "/")]
[InlineData("Http://localhost", "Http", "localhost", 0, null)]
[InlineData("localhost:8080/cube/info", null, "localhost", 8080, "/cube/info")]
[InlineData("localhost:8080/", null, "localhost", 8080, "/")]
[InlineData("localhost/", null, "localhost", 0, "/")]
[InlineData("localhost", null, "localhost", 0, null)]
public void Parse(String url, String schema, String host, Int32 port, String path)
{
{
var uri = new MyUri(url);
Assert.Equal(schema, uri.Scheme);
Assert.Equal(host, uri.Host);
Assert.Equal(port, uri.Port);
Assert.Equal(path, uri.PathAndQuery);
}
{
if (!url.StartsWithIgnoreCase("http://", "https://"))
url = "http://" + url;
if (schema.IsNullOrEmpty()) schema = "http";
var uri = new Uri(url);
Assert.Equal(schema?.ToLower(), uri.Scheme);
Assert.Equal(host, uri.Host);
Assert.Equal(port > 0 ? port : 80, uri.Port);
Assert.Equal(!path.IsNullOrEmpty() ? path : "/", uri.PathAndQuery);
}
}
}
class MyUri
{
public String Scheme { get; set; }
public String Host { get; set; }
public Int32 Port { get; set; }
public String PathAndQuery { get; set; }
public MyUri(String value)
{
// 先处理头尾,再处理中间的主机和端口
var p = value.IndexOf("://");
if (p >= 0)
{
Scheme = value[..p];
p += 3;
}
else
p = 0;
var p2 = value.IndexOf('/', p);
if (p2 > 0)
{
PathAndQuery = value[p2..];
value = value[p..p2];
}
else
value = value[p..];
// 拆分主机和端口,注意IPv6地址
p2 = value.LastIndexOf(':');
if (p2 > 0)
{
Host = value[..p2];
Port = value[(p2 + 1)..].ToInt();
}
else
{
Host = value;
}
}
}