Skip to content

Commit

Permalink
Fix: og:image meta tag not always rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
petervandenhout committed Aug 12, 2019
1 parent c13c0e7 commit 1cf6c3c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task<IActionResult> OnGetAsync(CancellationToken cancellationToken,
Url = Request.GetEncodedUrl()
};

Metadata.Image = PostList.Blog.CoverUrl;
if (PostList.Blog.CoverUrl != null && !PostList.Blog.CoverUrl.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
Metadata.Image = $"{Request.Scheme}://{Request.Host}{PostList.Blog.CoverUrl}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public async Task<IActionResult> OnGetAsync(string slug, CancellationToken cance
Url = Request.GetEncodedUrl()
};

Metadata.Image = Post.Post.CoverUrl;
if (Post.Post.CoverUrl != null && !Post.Post.CoverUrl.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
Metadata.Image = $"{Request.Scheme}://{Request.Host}{Post.Post.CoverUrl}";
if (!string.IsNullOrWhiteSpace(Post.Post.Categories))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public async Task OnGetAsync_Should_SetPostListModel()
var loggerMock = new Mock<ILogger<IndexModel>>();
var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PostListModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PostListModel>.Success(new PostListModel {
Blog = new BlogModel(new PineBlogOptions { Title = "Blog title", Description = "Blog description" }),
Pager = new Pager(0),
Posts = new List<Post>()
}));
.ReturnsAsync(Result<PostListModel>.Success(GetPostListModel()));

var httpContext = new DefaultHttpContext();
var pageContext = GetPageContext(httpContext);
Expand All @@ -46,14 +42,82 @@ public async Task OnGetAsync_Should_SetPostListModel()
pageModel.PostList.Pager.Should().NotBeNull();
pageModel.PostList.Posts.Should().NotBeNull();
pageModel.Title.Should().NotBeNull();


pageModel.PostList.Blog.Title.Should().Be("Blog title");
pageModel.PostList.Pager.Should().NotBeNull();
pageModel.PostList.Posts.Should().NotBeNull();
pageModel.Title.Should().Be("Blog title");
pageModel.Metadata.Description.Should().Be("Blog description");
pageModel.PageCover.Title.Should().Be("Blog title");
}

[Fact]
public async Task OnGetAsync_Should_SetPostListModel_WithAbsoluteCoverUrl()
{
var loggerMock = new Mock<ILogger<IndexModel>>();

var postListModel = GetPostListModel();
postListModel.Blog.CoverUrl = "http://www.example.com/images.jpg";

var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PostListModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PostListModel>.Success(postListModel));

var httpContext = new DefaultHttpContext();
var pageContext = GetPageContext(httpContext);

var pageModel = new IndexModel(mediaterMock.Object, loggerMock.Object)
{
PageContext = pageContext.Item1,
TempData = GetTempDataDictionary(httpContext),
Url = new UrlHelper(pageContext.Item2)
};

var result = await pageModel.OnGetAsync(default, 0);

result.Should().BeOfType<PageResult>();
pageModel.PostList.Blog.CoverUrl.Should().Be("http://www.example.com/images.jpg");
pageModel.Metadata.Image.Should().Be("http://www.example.com/images.jpg");
}

[Fact]
public async Task OnGetAsync_Should_SetPostListModel_WithRelativeCoverUrl()
{
var loggerMock = new Mock<ILogger<IndexModel>>();

var postListModel = GetPostListModel();
postListModel.Blog.CoverUrl = "/images.jpg";

var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PostListModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PostListModel>.Success(postListModel));

var httpContext = new DefaultHttpContext();
httpContext.Request.Scheme = "http";
httpContext.Request.Host = new HostString("localhost:5001");
var pageContext = GetPageContext(httpContext);

var pageModel = new IndexModel(mediaterMock.Object, loggerMock.Object)
{
PageContext = pageContext.Item1,
TempData = GetTempDataDictionary(httpContext),
Url = new UrlHelper(pageContext.Item2)
};

var result = await pageModel.OnGetAsync(default, 0);

result.Should().BeOfType<PageResult>();
pageModel.PostList.Blog.CoverUrl.Should().Be("/images.jpg");
pageModel.Metadata.Image.Should().Be("http://localhost:5001/images.jpg");
}

private PostListModel GetPostListModel()
{
return new PostListModel
{
Blog = new BlogModel(new PineBlogOptions { Title = "Blog title", Description = "Blog description" }),
Pager = new Pager(0),
Posts = new List<Post>()
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ public async Task OnGetAsync_Should_SetPostModel()
var loggerMock = new Mock<ILogger<PostModel>>();
var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PineBlog.Models.PostModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PineBlog.Models.PostModel>.Success(new PineBlog.Models.PostModel
{
Blog = new BlogModel(new PineBlogOptions { Title = "Blog title" }),
Post = new Post() {
Title = "Post title",
Description = "Post description",
Author = new Author { DisplayName = "Post author" }
}
}));
.ReturnsAsync(Result<PineBlog.Models.PostModel>.Success(GetPostModel()));

var httpContext = new DefaultHttpContext();
var pageContext = GetPageContext(httpContext);
Expand All @@ -57,6 +49,66 @@ public async Task OnGetAsync_Should_SetPostModel()
pageModel.BlogTitle.Should().Be("Blog title");
}

[Fact]
public async Task OnGetAsync_Should_SetPostModel_WithAbsoluteCoverUrl()
{
var loggerMock = new Mock<ILogger<PostModel>>();

var postModel = GetPostModel();
postModel.Post.CoverUrl = "http://www.example.com/images.jpg";

var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PineBlog.Models.PostModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PineBlog.Models.PostModel>.Success(postModel));

var httpContext = new DefaultHttpContext();
var pageContext = GetPageContext(httpContext);

var pageModel = new PostModel(mediaterMock.Object, loggerMock.Object)
{
PageContext = pageContext.Item1,
TempData = GetTempDataDictionary(httpContext),
Url = new UrlHelper(pageContext.Item2)
};

var result = await pageModel.OnGetAsync("slug", default);

result.Should().BeOfType<PageResult>();
pageModel.Post.Post.CoverUrl.Should().Be("http://www.example.com/images.jpg");
pageModel.Metadata.Image.Should().Be("http://www.example.com/images.jpg");
}

[Fact]
public async Task OnGetAsync_Should_SetPostModel_WithRelativeCoverUrl()
{
var loggerMock = new Mock<ILogger<PostModel>>();

var postModel = GetPostModel();
postModel.Post.CoverUrl = "/images.jpg";

var mediaterMock = new Mock<IMediator>();
mediaterMock.Setup(m => m.Send(It.IsAny<IRequest<Result<PineBlog.Models.PostModel>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result<PineBlog.Models.PostModel>.Success(postModel));

var httpContext = new DefaultHttpContext();
httpContext.Request.Scheme = "http";
httpContext.Request.Host = new HostString("localhost:5001");
var pageContext = GetPageContext(httpContext);

var pageModel = new PostModel(mediaterMock.Object, loggerMock.Object)
{
PageContext = pageContext.Item1,
TempData = GetTempDataDictionary(httpContext),
Url = new UrlHelper(pageContext.Item2)
};

var result = await pageModel.OnGetAsync("slug", default);

result.Should().BeOfType<PageResult>();
pageModel.Post.Post.CoverUrl.Should().Be("/images.jpg");
pageModel.Metadata.Image.Should().Be("http://localhost:5001/images.jpg");
}

[Fact]
public async Task OnGetAsync_Should_ThrowNotFoundException()
{
Expand All @@ -78,5 +130,19 @@ public async Task OnGetAsync_Should_ThrowNotFoundException()

action.Should().Throw<NotFoundException>();
}

private PineBlog.Models.PostModel GetPostModel()
{
return new PineBlog.Models.PostModel
{
Blog = new BlogModel(new PineBlogOptions { Title = "Blog title" }),
Post = new Post()
{
Title = "Post title",
Description = "Post description",
Author = new Author { DisplayName = "Post author" }
}
};
}
}
}

0 comments on commit 1cf6c3c

Please sign in to comment.