public void WhenIRequestPostWithId(int id) { //act response = client.GetAsync(b => b .AppendPath("posts") .AppendPath(id.ToString())).Result; }
protected async Task <T> DeserializeResponse <T>(FluentResponse response) { string json = await response.HttpContent.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); return(_serializer.Deserialize <T>(json)); }
public static MessagesResponse ToMessagesResponse(this FluentResponse value) { if (value.Messages != null) { MessagesResponse?result = new MessagesResponse(); List <string>?infos = value.Messages.OfType <FluentResponseInfoMessage>().Select(m => m.Message) .ToList(); if (infos.Count > 0) { result.InformationMessages = infos; } List <string>?warnings = value.Messages.OfType <FluentResponseWarningMessage>().Select(m => m.Message) .ToList(); if (warnings.Count > 0) { result.WarningMessages = warnings; } List <string>?errors = value.Messages.OfType <FluentResponseErrorMessage>().Select(m => m.Message) .ToList(); if (errors.Count > 0) { result.ErrorMessages = errors; } return(result); } return(null); }
public void HandlesNetworkFailure() { var client = new FluentClient <Error>("http://local.property.erm-api.com/v1/"); // server set up to fail on this code FluentResponse <User, Error> room = client.Get <User>("forceerror").Result; Assert.IsNotNull(room); Assert.IsNull(room.Entity); Assert.AreEqual(401, (int)room.StatusCode); Assert.AreEqual("Authenticate", room.Error.Message); }
public void ThenResponseContentIsComposedByAllFieldsWithValue(int id) { //arrange var responseObject = new Post() { Id = 2, Body = "2", Title = "2", UserId = "2" }; MemoryMessageStore.Current.Register(b => b .Url($"{apiUrl}/posts/{id}") .StatusCode(HttpStatusCode.OK) .ReasonPhrase("OK") .Content(c => c .Header("Content-Type", "application/json; charset=utf-8") .Data(responseObject) // object to be JSON serialized ) ); // use memory store by default var serializer = new JsonContentSerializer(); var fakeHttp = new FakeMessageHandler(); var client = new FluentClient(serializer, fakeHttp); client.BaseUri = new Uri(apiUrl, UriKind.Absolute); //act response = client.GetAsync(b => b .AppendPath("posts") .AppendPath(id.ToString())).Result; //assert Assert.NotNull(response); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); result = response.DeserializeAsync <Post>().Result; Assert.NotNull(result); Assert.AreEqual(id, result.Id); Assert.AreEqual(id.ToString(), result.Title); Assert.AreEqual(id.ToString(), result.UserId); Assert.AreEqual(id.ToString(), result.Body); }