I want to make tests to my Api so I need to test a PUT endpoint. I already tested all my Get endpoints so until now I never needed to send a body in my request. How can I do it?
TEST
public void TestUpdateSinglePlayerStats()
{
string id = "2019";
HttpWebRequest request = (HttpWebRequest)WebRequest
.Create("http://localhost:5000/GameStats/Update/" + id);
request.Headers.Add("Authorization", "Bearer " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjIwMTkiLCJyb2xlIjoiVXNlciIsIm5iZiI6MTYxMDEyMzY2OSwiZXhwIjoxNjEwMjEwMDY5LCJpYXQiOjE2MTAxMjM2Njl9.Dd2wzUJ5LnPBw0CbDXZZTIiQLX8074F_E1wW-qBPQzw");
request.ContentType = "application/json";
request.Method = "PUT";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
}
MY API ENDPOINT
Tried this
string id = "2019";
StatUpdateModel info = new StatUpdateModel();
info.Score = 4000;
info.Round = 3;
info.MonstersKilled = 102;
info.GameMode = "Singleplayer";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(info);
HttpWebRequest request = (HttpWebRequest)WebRequest
.Create("http://localhost:5000/GameStats/Update/" + id);
request.Headers.Add("Authorization", "Bearer " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjIwMTkiLCJyb2xlIjoiVXNlciIsIm5iZiI6MTYxMDEyMzY2OSwiZXhwIjoxNjEwMjEwMDY5LCJpYXQiOjE2MTAxMjM2Njl9.Dd2wzUJ5LnPBw0CbDXZZTIiQLX8074F_E1wW-qBPQzw");
request.ContentType = "application/json";
request.Method = "PUT";
request.ContentLength = byte1.Length;
Stream stream = request.GetRequestStream();
stream.Write(byte1, 0, byte1.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);