vendredi 20 novembre 2015

HttpClient Post Unit Testing

I have to unit test my POST methods of my REST services. [TestMethod] public void RemoveScheduleEntryTest() { var client = new HttpClient(); client.BaseAddress = new System.Uri("my ACTUAL http SERVICE url"); client.DefaultRequestHeaders.Add("MyID", "SomeVALUE");
HttpRequestMessage requestMessage = new HttpRequestMessage(); string str = "SOME JSON CONTENT "; requestMessage.Content = new StringContent(str, Encoding.UTF8, "application/json"); StringContent stringContent = new StringContent(JsonConvert.SerializeObject(str),Encoding.UTF8,"application/json"); HttpResponseMessage responseMessage = client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(str), Encoding.UTF8, "application/json")) .Result; Assert.IsTrue(responseMessage.IsSuccessStatusCode); }

So I have this so far written. But I am thinking this is NOT how I am supposed to do it. Can someone point me in the right direction at-least? I get false for my assert statement. And also on the responseMessage.Content.ReadAsStringAsync() I get "Object reference not set to an instance of an object". I see the request message with the correct URI and headers are correct. I tested this on firefox tool HttpRequester and I get the correct (intended) response back. So what is going wrong. Please ask questions if I missed something or correct me if I did something not right.

Aucun commentaire:

Enregistrer un commentaire