Last active
June 23, 2017 16:16
-
-
Save jrgcubano/c672929bdd5f9b5b71843c1c242c33cd to your computer and use it in GitHub Desktop.
TDD resources for scmallorca Marvel KATA with HttpClient, Moq, Md5
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using FluentAssertions; | |
using RestSharp; | |
using Xunit; | |
namespace Todo.Web.AcceptanceTests | |
{ | |
public static class Extensions | |
{ | |
public static T Get<T>(this IRestClient client, string url) where T : new() => | |
client.Get<T>(new RestRequest(url + "/") ).Data; | |
} | |
public class TaskServiceTestBase | |
{ | |
readonly string _baseurl = Environment.GetEnvironmentVariable("todo-web-url") ?? "http://dev-todo-web-staging.azurewebsites.net/Tasks/"; | |
protected readonly IRestClient Client; | |
protected readonly string User = Guid.NewGuid().ToString("N") + "@test.com"; | |
protected const string OtherUser = "[email protected]"; | |
protected TaskServiceTestBase() | |
{ | |
Client = new RestClient(_baseurl); | |
} | |
} | |
public class CompletingATask : TaskServiceTestBase | |
{ | |
readonly Task _task; | |
public CompletingATask() | |
{ | |
_task = Client.Post<Task>(User, new Task { Name = "test", DueDate = DateTime.Now.AddDays(1) }); | |
_task.Done = true; | |
Client.Put<Task>(User, _task); | |
} | |
[Fact] | |
public void ReturnsTheTaskIdInTheEntireList() => Client.Get<List<Task>>(User).Should().Contain(t => t.Id == _task.Id); | |
} | |
public class Task | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public DateTime DueDate { get; set; } | |
public bool Done { get; set; } | |
} | |
} | |
public class ComicApi { | |
const string BaseUrl = "https://api.twilio.com/2008-08-01"; | |
readonly string _accountSid; | |
readonly string _secretKey; | |
public TwilioApi(string accountSid, string secretKey) { | |
_accountSid = accountSid; | |
_secretKey = secretKey; | |
} | |
public T Execute<T>(RestRequest request) where T : new() | |
{ | |
var client = new RestClient(); | |
client.BaseUrl = new System.Uri(BaseUrl); | |
client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey); | |
request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment); // used on every request | |
var response = client.Execute<T>(request); | |
if (response.ErrorException != null) | |
{ | |
const string message = "Error retrieving response. Check inner details for more info."; | |
var twilioException = new ApplicationException(message, response.ErrorException); | |
throw twilioException; | |
} | |
return response.Data; | |
} | |
} |
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
Mapper.Initialize(cfg => { | |
cfg.CreateMap<ComicDto, Comic>(); | |
}); | |
var comic = Mapper.Map<ComicDto, Comic>(dto); |
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
https://wildermuth.com/2016/04/14/Using-Cache-in-ASP-NET-Core-1-0-RC1 |
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
https://gist.github.com/jrgcubano/de375db2154f094d04339c09641013eb |
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
public class ComicDto | |
{ | |
.... | |
} | |
public class Comic | |
{ | |
string _title; | |
string _thumbnail; | |
DateTime _releaseDate; | |
decimal _price; | |
Comic() | |
{} | |
Comic(string title, string thumbnail, DateTime releaseDate, decimal price) | |
{ | |
} | |
public override string ToString() => .... | |
public override bool Equals(Comic ... | |
public override int GetHashCode(... | |
} |
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
// automapper | |
// then | |
orderDto.ShouldBeEquivalentTo(order); |
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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
using System.Linq; | |
namespace WebRequest | |
{ | |
class Post | |
{ | |
public string UserId { get; set; } | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public string Body { get; set; } | |
} | |
// WITH Json file | |
http://stackoverflow.com/questions/26134425/get-json-string-with-httpclient | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
SendRequest().Wait(); | |
} | |
private static async Task SendRequest() | |
{ | |
using (var client = new HttpClient()) | |
{ | |
try | |
{ | |
client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com"); | |
var response = await client.GetAsync("/posts"); | |
response.EnsureSuccessStatusCode(); // Throw in not success | |
var stringResponse = await response.Content.ReadAsStringAsync(); | |
var posts = JsonConvert.DeserializeObject<IEnumerable<Post>>(stringResponse); | |
Console.WriteLine($"Got {posts.Count()} posts"); | |
Console.WriteLine($"First post is {JsonConvert.SerializeObject(posts.First())}"); | |
} | |
catch (HttpRequestException e) | |
{ | |
Console.WriteLine($"Request exception: {e.Message}"); | |
} | |
} | |
} | |
} | |
} |
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
public class MD5HashException : Exception {} | |
using System.Text; | |
using System.Security.Cryptography; | |
public class MD5HashGenerator | |
{ | |
public static string GenerateHash(string word) | |
{ | |
MD5 md5 = MD5.Create(); | |
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(word); | |
byte[] hash = md5.ComputeHash(inputBytes); | |
var sb = new StringBuilder(); | |
for (int i = 0; i < hash.Length; i++) | |
sb.Append(hash[i].ToString("x2")); | |
return sb.ToString(); | |
} | |
} | |
public class MD5HashModel | |
{ | |
readonly int _timeStamp; | |
readonly string _privateKey; | |
readonly string _publicKey; | |
readonly MD5HashGenerator _md5HashGenerator; | |
string _hash; | |
public MD5HashModel(int timeStamp, string privateKey, string publicKey) | |
{ | |
_timeStamp = timeStamp; | |
_privateKey = privateKey; | |
_publicKey = publicKey; | |
_md5Generator = new MD5HashGenerator(); | |
} | |
public int GetTimeStamp() => _timeStamp; | |
public string GetPublicKey() => _publicKey; | |
public string GenerateHash() | |
{ | |
if(_hash.IsNull()) | |
{ | |
try { | |
_hash = _md5HashGenerator.GenerateHash(${_timeStamp, _privateKey, _publicKey)); | |
} catch(Exception ex) { | |
throws new MD5HashException()... | |
} | |
} | |
return _hash; | |
} | |
} |
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
var stuardService = new Mock<IStuardService>(); | |
+ stuardService | |
+ .Setup(x => x.GetComics()) | |
+ .ReturnsAsync(new List<Comic> | |
+ { | |
+ new Comic | |
+ { | |
Marco = 5 | |
+ } | |
+ }); | |
_comicService = new ComicGateway(stuardService.Object); |
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
https://github.com/aspnet/Docs/blob/master/aspnetcore/fundamentals/configuration/sample/src/ConfigJson/Program.cs |
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
https://ireznykov.com/2016/07/31/xunit-and-async-task/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment