1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System; 4 using System.Web.Helpers.Test; 5 using Xunit; 6 using Assert = Microsoft.TestCommon.AssertEx; 7 8 namespace Microsoft.Web.Helpers.Test 9 { 10 public class GravatarTest 11 { 12 [Fact] GetUrlDefaults()13 public void GetUrlDefaults() 14 { 15 string url = Gravatar.GetUrl("foo@bar.com"); 16 Assert.Equal("http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80", url); 17 } 18 19 [Fact] RenderEncodesDefaultImageUrl()20 public void RenderEncodesDefaultImageUrl() 21 { 22 string render = Gravatar.GetHtml("foo@bar.com", defaultImage: "http://example.com/images/example.jpg").ToString(); 23 Assert.Equal( 24 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=http%3a%2f%2fexample.com%2fimages%2fexample.jpg\" alt=\"gravatar\" />", 25 render); 26 } 27 28 [Fact] RenderLowerCasesEmail()29 public void RenderLowerCasesEmail() 30 { 31 string render = Gravatar.GetHtml("FOO@BAR.COM").ToString(); 32 Assert.Equal( 33 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80\" alt=\"gravatar\" />", 34 render); 35 } 36 37 [Fact] RendersValidXhtml()38 public void RendersValidXhtml() 39 { 40 XhtmlAssert.Validate1_1(Gravatar.GetHtml("foo@bar.com")); 41 } 42 43 [Fact] RenderThrowsWhenEmailIsEmpty()44 public void RenderThrowsWhenEmailIsEmpty() 45 { 46 Assert.ThrowsArgumentNullOrEmptyString(() => { Gravatar.GetHtml(String.Empty); }, "email"); 47 } 48 49 [Fact] RenderThrowsWhenEmailIsNull()50 public void RenderThrowsWhenEmailIsNull() 51 { 52 Assert.ThrowsArgumentNullOrEmptyString(() => { Gravatar.GetHtml(null); }, "email"); 53 } 54 55 [Fact] RenderThrowsWhenImageSizeIsLessThanZero()56 public void RenderThrowsWhenImageSizeIsLessThanZero() 57 { 58 Assert.ThrowsArgument(() => { Gravatar.GetHtml("foo@bar.com", imageSize: -1); }, "imageSize", "The Gravatar image size must be between 1 and 512 pixels."); 59 } 60 61 [Fact] RenderThrowsWhenImageSizeIsZero()62 public void RenderThrowsWhenImageSizeIsZero() 63 { 64 Assert.ThrowsArgument(() => { Gravatar.GetHtml("foo@bar.com", imageSize: 0); }, "imageSize", "The Gravatar image size must be between 1 and 512 pixels."); 65 } 66 67 [Fact] RenderThrowsWhenImageSizeIsGreaterThan512()68 public void RenderThrowsWhenImageSizeIsGreaterThan512() 69 { 70 Assert.ThrowsArgument(() => { Gravatar.GetHtml("foo@bar.com", imageSize: 513); }, "imageSize", "The Gravatar image size must be between 1 and 512 pixels."); 71 } 72 73 [Fact] RenderTrimsEmail()74 public void RenderTrimsEmail() 75 { 76 string render = Gravatar.GetHtml(" \t foo@bar.com\t\r\n").ToString(); 77 Assert.Equal( 78 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80\" alt=\"gravatar\" />", 79 render); 80 } 81 82 [Fact] RenderUsesDefaultImage()83 public void RenderUsesDefaultImage() 84 { 85 string render = Gravatar.GetHtml("foo@bar.com", defaultImage: "wavatar").ToString(); 86 Assert.Equal( 87 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=wavatar\" alt=\"gravatar\" />", 88 render); 89 } 90 91 [Fact] RenderUsesImageSize()92 public void RenderUsesImageSize() 93 { 94 string render = Gravatar.GetHtml("foo@bar.com", imageSize: 512).ToString(); 95 Assert.Equal( 96 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=512\" alt=\"gravatar\" />", 97 render); 98 } 99 100 [Fact] RenderUsesRating()101 public void RenderUsesRating() 102 { 103 string render = Gravatar.GetHtml("foo@bar.com", rating: GravatarRating.G).ToString(); 104 Assert.Equal( 105 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&r=g\" alt=\"gravatar\" />", 106 render); 107 } 108 109 [Fact] RenderWithAttributes()110 public void RenderWithAttributes() 111 { 112 string render = Gravatar.GetHtml("foo@bar.com", 113 attributes: new { id = "gravatar", alT = "<b>foo@bar.com</b>", srC = "ignored" }).ToString(); 114 // beware of attributes ordering in tests 115 Assert.Equal( 116 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80\" alT=\"<b>foo@bar.com</b>\" id=\"gravatar\" />", 117 render); 118 } 119 120 [Fact] RenderWithDefaults()121 public void RenderWithDefaults() 122 { 123 string render = Gravatar.GetHtml("foo@bar.com").ToString(); 124 Assert.Equal( 125 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80\" alt=\"gravatar\" />", 126 render); 127 } 128 129 [Fact] RenderWithExtension()130 public void RenderWithExtension() 131 { 132 string render = Gravatar.GetHtml("foo@bar.com", imageExtension: ".png").ToString(); 133 Assert.Equal( 134 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8.png?s=80\" alt=\"gravatar\" />", 135 render); 136 137 render = Gravatar.GetHtml("foo@bar.com", imageExtension: "xyz").ToString(); 138 Assert.Equal( 139 "<img src=\"http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8.xyz?s=80\" alt=\"gravatar\" />", 140 render); 141 } 142 } 143 } 144