public HttpResponseMessage PutCategory(int id, Category category) { var cat = Data.FirstOrDefault(c => c.CategoryID == id); if (cat == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } cat.CategoryName = category.CategoryName; cat.Description = category.Description + " (update at " + DateTime.Now + ")"; return new HttpResponseMessage(HttpStatusCode.OK); }
public HttpResponseMessage PostCategory(Category category) { var categoryId = Data.Max(c => c.CategoryID) + 1; category.CategoryID = categoryId; Data.Add(category); var message = new HttpResponseMessage(HttpStatusCode.Created) { Content = new ObjectContent<Category>(category, new JsonMediaTypeFormatter()) }; var url = Url.Route(null, new { id = categoryId }); message.Headers.Location = new Uri(url, UriKind.Relative); return message; }
public void TestPut() { var cat = new Category { CategoryID = 4, CategoryName = "My category", Description = "My category description" }; var request = new HttpRequestMessage(HttpMethod.Put, string.Format("api/categorytest/{0}", cat.CategoryID)) { Content = new ObjectContent<Category>(cat, new JsonMediaTypeFormatter()) }; var requestTask = this._httpClient.SendAsync(request); requestTask.Wait(); var response = requestTask.Result; response.EnsureSuccessStatusCode(); }
public void TestPost() { var cat = new Category { CategoryName = "My category", Description = "My category description" }; var request = new HttpRequestMessage(HttpMethod.Post, "api/categorytest") { Content = new ObjectContent<Category>(cat, new JsonMediaTypeFormatter()) }; var requestTask = this._httpClient.SendAsync(request); requestTask.Wait(); var response = requestTask.Result; response.EnsureSuccessStatusCode(); var task = response.Content.ReadAsAsync<Category>(); task.Wait(); Assert.Greater(task.Result.CategoryID, 1); }