1 namespace ShortUrlTest
2 {
3 using Xunit;
4 using Nancy.Testing;
5 using Moq;
6 using ShortUrl;
7
8 public class UrlStorageTest
9 {
10 private Browser app;
11 private Mock<UrlStore> fakeStorage;
12
13 public UrlStorageTest()
14 {
15 ShortUrlModule artificiaReference;
16
17 //Given
18 fakeStorage = new Mock<UrlStore>();
19 app = new Browser(
20 new ConfigurableBootstrapper(
21 with => with.Dependency(fakeStorage.Object)));
22
23 }
24
25 [Fact]
26 public void app_should_store_url_when_one_is_posted()
27 {
28 //When
29 app.Post("/",
30 with =>
31 {
32 with.FormValue("url", "http://www.longurlplease.com/");
33 with.HttpRequest();
34 });
35
36 //Then
37 fakeStorage
38 .Verify(store =>
39 store.SaveUrl("http://www.longurlplease.com/",
40 It.IsAny<string>()));
41 }
42
43 [Fact]
44 public void app_should_try_to_retrieve_url_when_getting_a_short_url()
45 {
46 //When
47 app.Get("/shorturl",
48 with => with.HttpRequest());
49
50 //Then
51 fakeStorage
52 .Verify(store =>
53 store.GetUrlFor("shorturl"));
54 }
55 }
56 }
57
1 namespace ShortUrl
2 {
3 public interface UrlStore
4 {
5 void SaveUrl(string url, string shortenedUrl);
6 string GetUrlFor(string shortenedUrl);
7 }
8 }
1 namespace ShortUrl
2 {
3 using Nancy;
4
5 public class ShortUrlModule : NancyModule
6 {
7 public ShortUrlModule(UrlStore urlStore)
8 {
9 Get["/"] = _ => View["index.html"];
10 Post["/"] = _ => ShortenUrl(urlStore);
11 Get["/{shorturl}"] = param =>
12 {
13 string shortUrl = param.shorturl;
14 return Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString()));
15 };
16 }
17
18 private Response ShortenUrl(UrlStore urlStore)
19 {
20 string longUrl = Request.Form.url;
21 var shortUrl = ShortenUrl(longUrl);
22 urlStore.SaveUrl(longUrl, shortUrl);
23
24 return View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }];
25 }
26
27 private string ShortenUrl(string longUrl)
28 {
29 return "a" + longUrl.GetHashCode();
30 }
31 }
32 }
33
1 namespace ShortUrlTest
2 {
3 using System.Linq;
4 using MongoDB.Bson;
5 using MongoDB.Driver;
6 using MongoDB.Driver.Builders;
7 using ShortUrl.DataAccess;
8 using Xunit;
9
10 public class MongoUrlStoreTest
11 {
12 private string connectionString = "mongodb://localhost:27010/short_url_test";
13 private MongoDatabase database;
14 private MongoCollection<BsonDocument> urlCollection;
15
16 public MongoUrlStoreTest()
17 {
18 //given
19 database = MongoDatabase.Create(connectionString);
20 urlCollection = database.GetCollection("urls");
21 urlCollection.RemoveAll();
22 }
23
24 [Fact]
25 public void should_store_urls_in_mongo()
26 {
27 //when
28 var store = new MongoUrlStore(connectionString);
29 store.SaveUrl("http://somelongurl.com/", "http://shorturl/abc");
30
31 //then
32 var urlFromDB = urlCollection
33 .Find(Query.EQ("url", "http://somelongurl.com/"))
34 .FirstOrDefault();
35
36 Assert.NotNull(urlFromDB);
37 Assert.Equal(urlFromDB["shortenedUrl"], "http://shorturl/abc");
38 }
39
40 [Fact]
41 public void should_be_able_to_find_shortened_urls()
42 {
43 //given
44 var store = new MongoUrlStore(connectionString);
45 store.SaveUrl("http://somelongurl.com/", "http://shorturl/abc");
46
47 //when
48 var longUrl = store.GetUrlFor("http://shorturl/abc");
49
50 //then
51 Assert.Equal("http://somelongurl.com/", longUrl);
52 }
53 }
54 }
55
1 namespace ShortUrl.DataAccess
2 {
3 using System.Linq;
4 using MongoDB.Bson;
5 using MongoDB.Driver;
6 using MongoDB.Driver.Builders;
7
8 public class MongoUrlStore : UrlStore
9 {
10 private MongoDatabase database;
11 private MongoCollection<BsonDocument> urls;
12
13 public MongoUrlStore(string connectionString)
14 {
15 database = MongoDatabase.Create(connectionString);
16 urls = database.GetCollection("urls");
17 }
18
19 public void SaveUrl(string url, string shortenedUrl)
20 {
21 urls.Insert(new {url, shortenedUrl});
22 }
23
24 public string GetUrlFor(string shortenedUrl)
25 {
26 var urlDocument =
27 urls
28 .Find(Query.EQ("shortenedUrl", shortenedUrl))
29 .FirstOrDefault();
30
31 return
32 urlDocument == null ?
33 null : urlDocument["url"].AsString;
34 }
35 }
36 }
14 public BaseUrlSpec()
15 {
16 ShortUrlModule artificiaReference;
17 app = new Browser(new Bootstrapper());
18 }
1 namespace ShortUrl
2 {
3 using Nancy;
4 using DataAccess;
5
6 public class Bootstrapper : DefaultNancyBootstrapper
7 {
8 protected override void ConfigureApplicationContainer(TinyIoC.TinyIoCContainer container)
9 {
10 base.ConfigureApplicationContainer(container);
11
12 var mongoUrlStore = new MongoUrlStore("mongodb://localhost:27010/short_url");
13 container.Register<UrlStore>(mongoUrlStore);
14 }
15 }
16 }
"This post was aggregated from https://www.horsdal-consult.dk/2011/10/frictionless-net-web-app-development_24.html and all comments should be submitted on the original post"