I needed the other day to reduce the length of a URL - so I decided to use a URL reducing service like tinyurl.com And I thought I would share the C# .Net code - nothing new I'm sure and there are plenty of examples out there.
public sealed class TinyUrlReducer : IReduceUrls
{
private readonly string _tinyUrl;
private readonly string _proxyUrl;
public TinyUrlReducer(string tinyUrl) : this(tinyUrl, null)
{
}
public TinyUrlReducer(string tinyUrl, string proxyUrl)
{
_tinyUrl = tinyUrl;
_proxyUrl = proxyUrl;
}
public string Reduce(string url)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentException("Can reduce a null or empty url!", "url");
var reducedUrl = "";
try
{
Trace.WriteLine(string.Format("Reduce: Url - '{0}'.", url));
var requestUrl = string.Format("{0}?url={1}", _tinyUrl, url);
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.KeepAlive = false;
request.UseDefaultCredentials = true;
request.AllowAutoRedirect = true;
request.UseDefaultCredentials = true;
if (!string.IsNullOrEmpty(_proxyUrl))
request.Proxy = new WebProxy(_proxyUrl, true, null, CredentialCache.DefaultCredentials);
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
throw new WebException(string.Format("Failed to call url - '{0}'!", requestUrl));
using (var stream = new StreamReader(response.GetResponseStream()))
{
reducedUrl = stream.ReadLine();
}
if (string.IsNullOrEmpty(reducedUrl))
throw new Exception("Reduced url is null or empty!");
}
catch (Exception exn)
{
Trace.WriteLine(string.Format("Reduce: Failed to reduce url, message - '{0}'.", exn.Message));
reducedUrl = url;
}
finally
{
Trace.WriteLine(string.Format("Reduce: Reduced Url - '{0}'.", reducedUrl));
}
return reducedUrl;
}
}
And a couple of tests...
[TestFixture]
public class TinyUrlReducerTests
{
[Test]
public void ShouldReduceUrl()
{
// Given we have url reducer...
var reducer = new TinyUrlReducer("http://tinyurl.com/api-create.php", "http://PROXY.MY.COM:8080");
// When we reduce a url..
var reducedUrl = reducer.Reduce("http://somereallylongurl/whichhasarelativelylongname/somepage.aspx");
// Then we expect the reduced url to start with...
Assert.IsTrue(reducedUrl.ToLower().StartsWith("http://tinyurl.com/"));
}
[Test, ExpectedException(typeof(ArgumentException))]
public void ShouldThrowExceptionIfUrlNull()
{
// Given we have url reducer...
var reducer = new TinyUrlReducer("http://tinyurl.com/api-create.php", "http://PROXY.MY.COM:8080");
// When we reduce a url..
var reducedUrl = reducer.Reduce(null);
// Then we expect the reduced url to start with...
Assert.IsTrue(reducedUrl.ToLower().StartsWith("http://tinyurl.com/"));
}
[Test, ExpectedException(typeof(ArgumentException))]
public void ShouldThrowExceptionIfUrlEmpty()
{
// Given we have url reducer...
var reducer = new TinyUrlReducer("http://tinyurl.com/api-create.php", "http://PROXY.MY.COM:8080");
// When we reduce a url..
var reducedUrl = reducer.Reduce(string.Empty);
// Then we expect the reduced url to start with...
Assert.IsTrue(reducedUrl.ToLower().StartsWith("http://tinyurl.com/"));
}
[Test]
public void ShouldReturnOrginalUrlWhenReducerFails()
{
// Given we have url reducer...
var reducer = new TinyUrlReducer("http://tinyurl.com/api-create.php");
// When we reduce a url and we know it will fail..
var url = "http://somereallylongurl/whichhasarelativelylongname/somepage.aspx";
var reducedUrl = reducer.Reduce(url);
// Then we expect the reduced url to the same as the original url...
Assert.AreEqual(url, reducedUrl);
}
}
Comments
Post a Comment