Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 7be5846

Browse files
committed
#341 Add HttpReqeuest GetEncodedUrl and GetDecodedUrl extensions.
1 parent ac945a0 commit 7be5846

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/Microsoft.AspNet.Http.Extensions/UriHelper.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,27 @@ public static string Encode(Uri uri)
7171
return uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
7272
}
7373
}
74+
75+
/// <summary>
76+
/// Returns the combine components of the request URL in a fully escaped form suitable for use in HTTP headers
77+
/// and other HTTP operations.
78+
/// </summary>
79+
/// <param name="request"></param>
80+
/// <returns></returns>
81+
public static string GetEncodedUrl(this HttpRequest request)
82+
{
83+
return Encode(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
84+
}
85+
86+
/// <summary>
87+
/// Returns the combine components of the request URL in a fully un-escaped form (except for the QueryString)
88+
/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
89+
/// </summary>
90+
/// <param name="request"></param>
91+
/// <returns></returns>
92+
public static string GetDecodedUrl(this HttpRequest request)
93+
{
94+
return request.Scheme + "://" + request.Host.Value + request.PathBase.Value + request.Path.Value + request.QueryString.Value;
95+
}
7496
}
7597
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 Microsoft.AspNet.Http.Internal;
5+
using Xunit;
6+
7+
namespace Microsoft.AspNet.Http.Extensions
8+
{
9+
public class UriHelperTests
10+
{
11+
[Fact]
12+
public void EncodeEmptyPartialUrl()
13+
{
14+
var result = UriHelper.Encode();
15+
16+
Assert.Equal("/", result);
17+
}
18+
19+
[Fact]
20+
public void EncodePartialUrl()
21+
{
22+
var result = UriHelper.Encode(new PathString("/un?escaped/base"), new PathString("/un?escaped"),
23+
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
24+
25+
Assert.Equal("/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
26+
}
27+
28+
[Fact]
29+
public void EncodeEmptyFullUrl()
30+
{
31+
var result = UriHelper.Encode("http", new HostString(string.Empty));
32+
33+
Assert.Equal("http:///", result);
34+
}
35+
36+
[Fact]
37+
public void EncodeFullUrl()
38+
{
39+
var result = UriHelper.Encode("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"),
40+
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
41+
42+
Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
43+
}
44+
45+
[Fact]
46+
public void GetEncodedUrlFromRequest()
47+
{
48+
var request = new DefaultHttpContext().Request;
49+
request.Scheme = "http";
50+
request.Host = new HostString("my.HoΨst:80");
51+
request.PathBase = new PathString("/un?escaped/base");
52+
request.Path = new PathString("/un?escaped");
53+
request.QueryString = new QueryString("?name=val%23ue");
54+
55+
Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue", request.GetEncodedUrl());
56+
}
57+
58+
[Fact]
59+
public void GetDecodedUrlFromRequest()
60+
{
61+
var request = new DefaultHttpContext().Request;
62+
request.Scheme = "http";
63+
request.Host = new HostString("my.HoΨst:80");
64+
request.PathBase = new PathString("/un?escaped/base");
65+
request.Path = new PathString("/un?escaped");
66+
request.QueryString = new QueryString("?name=val%23ue");
67+
68+
Assert.Equal("http://my.hoψst:80/un?escaped/base/un?escaped?name=val%23ue", request.GetDecodedUrl());
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)