public async Task DemonstratingFakingHttp() { // Faking out HTTP var expectedFoo = new FooResource { Id = 1, Value = "Hello, World!" }; string responseJson = JsonSerializer.Serialize(expectedFoo); var messageHandler = new MockHttpMessageHandler(responseJson, HttpStatusCode.OK); using var httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri("https://needs.an.address") }; // End of faking out HTTP // Arrange var fooApi = new FooApi(httpClient); // Act FooResource actual = await fooApi.GetFoo(1); // Assert Assert.Equal("Hello, World!", actual.Value); }
public HttpResponseMessage Get() { var resource = new FooResource(); resource.Links.Add(new Link("self", "/Foo")); resource.Links.Add(new Link("bars", "/BarCollection")); resource.Links.Add(new Link("failing", "/Failing")); return Request.CreateResponse(resource); }
public void items_array_contains_resources() { var collectionResource = new CollectionResource<FooResource>(); var resource = new FooResource { Bar = "Foo" }; resource.Links.Add(new Link("self", "/foo")); collectionResource.Items.Add(resource); var jToken = _mediaFormatter.Format(collectionResource); jToken .SelectToken("_items[0].bar") .Value<string>() .ShouldEqual("Foo"); }
public void ShouldSerializeResourceEmbeddedProperties() { var settingsWithResolver = CreateSettingsWithContractResolver(); // Deserialize and then serialize to get rid of all of the new lines var expectedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(FooResourceJson, settingsWithResolver)); var resource = new FooResource { NormalProperty = new { property_one = "one", property_two = new[] { 2, 3 } }, EmbeddedProperty = new { message = "Expected embedded message" }, NullEmbeddedProperty = null, SelfLink = new Link { HRef = "http://api.com/self", Title = "Self", IsTemplated = false }, LinkProperty = new Link { HRef = "http://api.com/resources/5", Title = "Some Resource", IsTemplated = true }, NullLinkProperty = null, LinkArrayProperty = new[] { new Link { HRef = "http://api.com/bars/1", Title = "Bar 1", IsTemplated = false }, new Link { HRef = "http://api.com/bars/2", Title = "Bar 2", IsTemplated = true }, }, Foo2Resource = new Foo2Resource { NormalProperty = new { property_one = "one", property_two = new[] { 2, 3 } }, EmbeddedProperty = new { message = "Expected embedded message" }, NullEmbeddedProperty = null, SelfLink = new Link { HRef = "http://api.com/self", Title = "Self", IsTemplated = false }, LinkProperty = new Link { HRef = "http://api.com/resources/5", Title = "Some Resource", IsTemplated = true }, NullLinkProperty = null, LinkArrayProperty = new[] { new Link { HRef = "http://api.com/bars/1", Title = "Bar 1", IsTemplated = false }, new Link { HRef = "http://api.com/bars/2", Title = "Bar 2", IsTemplated = true }, } } }; var actualJson = JsonConvert.SerializeObject( resource, settingsWithResolver); Assert.Equal(expectedJson, actualJson); }