Fix NuGet/NuGetGallery#1931 and add some unit tests

Fixes NuGet/NuGetGallery#1931 because I don't know how to programming.

Added some unit tests as well.