Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2889 from youngbob/fix-media-range-null-case
Browse files Browse the repository at this point in the history
Cast a null MediaRange reference to a null string
  • Loading branch information
thecodejunkie committed May 6, 2018
2 parents b7b20aa + 77caaf8 commit 93c1594
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Nancy/Responses/Negotiation/MediaRange.cs
Expand Up @@ -122,6 +122,11 @@ public bool MatchesWithParameters(MediaRange other)
/// </returns>
public static implicit operator string(MediaRange mediaRange)
{
if (null == mediaRange)
{
return null;
}

if (mediaRange.Parameters.Any())
{
return string.Concat(mediaRange.Type, "/", mediaRange.Subtype, ";", mediaRange.Parameters);
Expand Down
15 changes: 14 additions & 1 deletion test/Nancy.Tests/Unit/Responses/Negotiation/MediaRangeFixture.cs
Expand Up @@ -83,5 +83,18 @@ public void Should_strip_whitespace_when_calling_tostring()
// Then
range.ToString().ShouldEqual("application/vnd.nancy;a=1;b=2");
}

[Fact]
public void Should_cast_to_null_string()
{
// Given
MediaRange range = null;

// When
string cast = (string)range;

// Then
Assert.Null(cast);
}
}
}
}

0 comments on commit 93c1594

Please sign in to comment.