Created
December 15, 2015 01:57
-
-
Save JayBazuzi/d09d77ad1f2f3933abd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal interface IUserSettings | |
{ | |
Task<bool> GetFlag(string name); | |
Task SetFlag(string name, bool value); | |
} | |
internal abstract class UserSettingsTestsBase | |
{ | |
protected abstract IUserSettings CreateSubject(); | |
[TestMethod] | |
public async Task MyTestMethod() | |
{ | |
var subject = CreateSubject(); | |
await subject.SetFlag("foo", true); | |
var result = await subject.GetFlag("foo"); | |
Assert.IsTrue(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal partial class AzureBlobStorageUserSettings : IUserSettings | |
{ | |
private readonly HttpClient _httpClient; | |
public AzureBlobStorageUserSettings() | |
{ | |
_httpClient = new HttpClient | |
{ | |
BaseAddress = new Uri("http://myproject.azure.com/blobstorage/") | |
}; | |
} | |
public async Task<bool> GetFlag(string name) | |
{ | |
var blob = await _httpClient.GetStringAsync(string.Format("settings/{0}", name)); | |
return blob == bool.TrueString; | |
} | |
public async Task SetFlag(string name, bool value) | |
{ | |
await _httpClient.PostAsJsonAsync(string.Format("settings/{0}", name), value); | |
} | |
} | |
#if !INTEGRATION_TESTS | |
[Ignore] | |
#endif | |
[TestClass] | |
internal class AzureBlobStorageUserSettingsTests : UserSettingsTestsBase | |
{ | |
protected override IUserSettings CreateSubject() | |
{ | |
return new AzureBlobStorageUserSettings(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
internal class UserSettingsSimulatorTests1 : UserSettingsTestsBase | |
{ | |
protected override IUserSettings CreateSubject() | |
{ | |
return new UserSettingsSimulator(); | |
} | |
internal class UserSettingsSimulator : IUserSettings | |
{ | |
private readonly Dictionary<string, bool> _flagsByName = new Dictionary<string, bool>(); | |
public async Task<bool> GetFlag(string name) | |
{ | |
return _flagsByName[name]; | |
} | |
public async Task SetFlag(string name, bool value) | |
{ | |
_flagsByName[name] = value; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
internal class UserSettingsSimulatorTests2 : UserSettingsTestsBase | |
{ | |
private readonly FakeHttpClienthandler _fakeHttpClienthandler; | |
private readonly Dictionary<string, bool> _flagsByName = new Dictionary<string, bool>(); | |
public UserSettingsSimulatorTests2() | |
{ | |
_fakeHttpClienthandler = new FakeHttpClienthandler | |
{ | |
OverrideSendAsync = async (message, token) => | |
{ | |
var name = message.RequestUri.ToString().Split('/')[1]; | |
if (message.Method == HttpMethod.Get) | |
{ | |
return new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = new StringContent(_flagsByName[name].ToString()) | |
}; | |
} | |
if (message.Method == HttpMethod.Post) | |
{ | |
_flagsByName[name] = await message.Content.ReadAsStringAsync() == "true"; | |
return new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = new StringContent(_flagsByName[name].ToString()) | |
}; | |
} | |
throw new Exception(); | |
} | |
}; | |
} | |
protected override IUserSettings CreateSubject() | |
{ | |
var httpClient = new HttpClient(_fakeHttpClienthandler) | |
{ | |
BaseAddress = new Uri("http://myproject.azure.com/blobstorage/") | |
}; | |
return new AzureBlobStorageUserSettings(httpClient); | |
} | |
} | |
internal class FakeHttpClienthandler : HttpMessageHandler | |
{ | |
public Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> OverrideSendAsync; | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | |
CancellationToken cancellationToken) | |
{ | |
return OverrideSendAsync(request, cancellationToken); | |
} | |
} | |
internal partial class AzureBlobStorageUserSettings | |
{ | |
// only required for UserSettingsSimulatorTests2's FakeHttpClient | |
public AzureBlobStorageUserSettings(HttpClient httpClient) | |
{ | |
_httpClient = httpClient; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment