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

Add HttpReqeuest GetEncodedUrl and GetDecodedUrl extensions. #359

Merged
merged 1 commit into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Microsoft.AspNet.Http.Extensions/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,27 @@ public static string Encode(Uri uri)
return uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
}
}

/// <summary>
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
/// and other HTTP operations.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetEncodedUrl(this HttpRequest request)
{
return Encode(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
}

/// <summary>
/// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)
/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetDisplayUrl(this HttpRequest request)
{
return request.Scheme + "://" + request.Host.Value + request.PathBase.Value + request.Path.Value + request.QueryString.Value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does all this concatenation work when some sections are missing? E.g. no query string or empty path etc.?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Added tests for empty params.

}
}
}
71 changes: 71 additions & 0 deletions test/Microsoft.AspNet.Http.Extensions.Tests/UriHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Http.Internal;
using Xunit;

namespace Microsoft.AspNet.Http.Extensions
{
public class UriHelperTests
{
[Fact]
public void EncodeEmptyPartialUrl()
{
var result = UriHelper.Encode();

Assert.Equal("/", result);
}

[Fact]
public void EncodePartialUrl()
{
var result = UriHelper.Encode(new PathString("/un?escaped/base"), new PathString("/un?escaped"),
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));

Assert.Equal("/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
}

[Fact]
public void EncodeEmptyFullUrl()
{
var result = UriHelper.Encode("http", new HostString(string.Empty));

Assert.Equal("http:///", result);
}

[Fact]
public void EncodeFullUrl()
{
var result = UriHelper.Encode("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"),
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));

Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
}

[Fact]
public void GetEncodedUrlFromRequest()
{
var request = new DefaultHttpContext().Request;
request.Scheme = "http";
request.Host = new HostString("my.HoΨst:80");
request.PathBase = new PathString("/un?escaped/base");
request.Path = new PathString("/un?escaped");
request.QueryString = new QueryString("?name=val%23ue");

Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue", request.GetEncodedUrl());
}

[Fact]
public void GetDisplayUrlFromRequest()
{
var request = new DefaultHttpContext().Request;
request.Scheme = "http";
request.Host = new HostString("my.HoΨst:80");
request.PathBase = new PathString("/un?escaped/base");
request.Path = new PathString("/un?escaped");
request.QueryString = new QueryString("?name=val%23ue");

Assert.Equal("http://my.hoψst:80/un?escaped/base/un?escaped?name=val%23ue", request.GetDisplayUrl());
}
}
}