简体   繁体   中英

How to write Unit test cases for Web hooks in c#?

I have a web page which receives web hook request and i want to write Unit test cases (NUnit framework) for this web hook. I want to mock the web hook request (Post request) in unit test cases and pass the request headers and body as the Json data to the web page which receives request.

Web page name

/webhook/receiveWebHook.aspx

Request Headers

Content-Type: application/json
messageType: TestInsertNotification

Request Body (Json data)

{ 
   "Alias":"Test",
   "TransactionID":"123"
}

Code in web page:

protected void Page_Load(object sender, EventArgs e)
{
      if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
      {
           object notification = null;
           string message = string.Empty;

           Int32 pID = InsertHTTPRequest(HttpContext.Current.Request); //Generate id in DB

           if (pID > 0 && this.Request.ContentType == "application/json")
           {
                StreamReader sr = new StreamReader(this.Request.InputStream, this.Request.ContentEncoding);
                message = sr.ReadToEnd();
                TextReader content = new StreamReader(new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(message)));
                JToken json = JToken.Load(new JsonTextReader(content));
                notification = new JsonSerializer().Deserialize(new JTokenReader(json), this.GetTypeOfMessage(HttpContext.Current.Request.Headers("MessageType")));   
           }
            if (notification != null)
            {
                 if (notification is InsertNotification)
                 {
                      InsertNotification insertNotification = notification as InsertNotification;
                      DbInsertMethod(pID, message, insertNotification);
                 }
            }
      }
}

Can someone please help how to achieve unit testing for webhooks in c#? Also, please suggest how to pass HTTP Post request from Unit test method with Header and body (containing Json data)?

PS: Currently i'm using Postman to test the functionality of this webhook.

Try to use RestSharp library. For your case it looks like this:

[TestFixture]
public class TestWebApp
{
    [Test]
    public void TestRequest()
    {
        IRestClient client = new RestClient($"{BaseUrl}/webhook/receiveWebHook.aspx");
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("messageType", "TestInsertNotification");
        JObject jObjectbody = new JObject();
        jObjectbody.Add("Alias", "Test");
        jObjectbody.Add("TransactionID", "123");
        request.AddParameter("application/json", jObjectbody, ParameterType.RequestBody);
        var response = client.Execute(request);
        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM