Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
[TestMethod]
public async Task ShouldSetGetCache()
{
var options = new DbContextOptionsBuilder<ImmutableCacheDbContext>()
.UseInMemoryDatabase(databaseName: "LotNamesCacheTests")
.Options;
using (var dbContext = new ImmutableCacheDbContext(options))
{
var lot1 = Mock.Of<ParkingLot>(x => x.LotCode == "02" && x.LotName == "Lot 02");
@changhuixu
changhuixu / prototype-pollution-1.ts
Created July 25, 2019 15:05
Object.prototype polluted by __proto__
// you can play with the following code snippet in browser console.
let person = {name: 'John Doe'}
console.log(person.name)
// John Doe
person.__proto__.toString = () => {alert('evil')}
console.log(person.name)
// an alert box with "evil" pops up
const isObject = obj => obj && obj.constructor && obj.constructor === Object;
function merge(dest, src) {
for (var attr in src) {
if (isObject(dest[attr]) && isObject(src[attr])) {
merge(dest[attr], src[attr]);
} else {
dest[attr] = src[attr];
}
}
return dest
var userInput = JSON.parse('{"__proto__": {"admin": true}}')
var a = {...userInput}
console.log(a.admin)
// true
var b = Object.assign({}, userInput)
console.log(b.admin)
// true
@changhuixu
changhuixu / Author.cs
Created August 29, 2019 18:47
A model class with a LINQ Expression
public class Author
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Membership Membership { get; set; }
public static Expression<Func<Author, bool>> GoldAndUp = x =>
x.Membership == Membership.Gold || x.Membership == Membership.Platinum;
}
@changhuixu
changhuixu / LinqExpressionConsumption.cs
Last active August 29, 2019 18:58
Static Linq Expression, usage
return await _dbContext.Authors
.Where(Author.GoldAndUp) // expression works with IQueryable
.Select(x => new AuthorViewModel(x))
.ToListAsync();
@changhuixu
changhuixu / LinqExpressionWithIEnumerable.cs
Created August 29, 2019 19:02
Linq Expression, usage
var authors = await _dbContext.Authors.ToListAsync(); // data is in memory
var goldAndUpAuthors = authors // IEnumerable
.Where(Author.GoldAndUp.Compile())
.Select(x => new AuthorViewModel(x))
.ToList();
@changhuixu
changhuixu / CalculationService.cs
Last active September 4, 2019 14:10
Unit Testing with .NET Core ILogger<T>
public class CalculationService : ICalculationService
{
private readonly ILogger<CalculationService> _logger;
public CalculationService(ILogger<CalculationService> logger)
{
_logger = logger;
}
public int AddTwoPositiveNumbers(int a, int b)
public class CalculationService : ICalculationService
{
private readonly ILogger<CalculationService> _logger;
public CalculationService(ILogger<CalculationService> logger)
{
_logger = logger;
}
public int AddTwoPositiveNumbers(int a, int b)
public static class MockLoggerExtensions
{
public static void VerifyLog<T>(this Mock<ILogger<T>> loggerMock, LogLevel level, string message, string failMessage = null)
{
loggerMock.VerifyLog(level, message, Times.Once(), failMessage);
}
public static void VerifyLog<T>(this Mock<ILogger<T>> loggerMock, LogLevel level, string message, Times times, string failMessage = null)
{
loggerMock.Verify(l => l.Log(level, It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, _) => o.ToString() == message), It.IsAny<Exception>(),