// <-- Note that this is an explicit class, not an interface // POST api/sandwich public HttpResponseMessage Post(DeliSandwich sandwich) { // Make sure we've got something. Throw a bad request if there's nothing in it. if (sandwich == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "sandwich cannot be null")); } var mySandwich = new DeliSandwich() {Description = sandwich.Description, Id = sandwich.Id}; // Do something with it....I'm not going to do anything with it. You get the picture. return new HttpResponseMessage(HttpStatusCode.Created); }
public void Create_Sandwich_Success() { var client = new HttpClient(_httpServer); var postAddress = string.Format("api/sandwich/"); client.BaseAddress = new Uri(Url); var sandwich = new DeliSandwich() { Description = "Egg Salad Sandwich", Id = 2}; // <---- Note that this is an explicit object, not an interface var postData = new StringContent(JsonConvert.SerializeObject(sandwich), Encoding.UTF8, "application/json"); using (HttpResponseMessage response = client.PostAsync(postAddress, postData, new CancellationTokenSource().Token).Result) { Assert.That(response.IsSuccessStatusCode); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created)); } }