Search code examples
c#azure-functionshttprequesthttp-getazure-function-app

My Azure Function should throw an exception in case of empty string


The code of the Run method of my Azure Function is this:

public static void Run([HttpTrigger("get")] HttpRequest req, ILogger log) {
   string parameter = req.Query["parameter"];
   if (string.IsNullOrEmpty(parameter)) {
      throw new ArgumentNullException("Parameter must be set.");
   }
   log.LogInformation(parameter);
}

I have the following cases when running the Function and passing the parameter to the HTTP request:

  • HTTP GET without parameter (/api/ServiceBusOutput): exception (correct)
  • HTTP GET with not set parameter (/api/ServiceBusOutput?parameter): exception (correct)
  • HTTP GET with parameter as empty string (/api/ServiceBusOutput?parameter=""): success but it should fail (the URL becomes /api/ServiceBusOutput?parameter=%22%22)
  • HTTP GET with parameter as string (/api/ServiceBusOutput?parameter="something"): success (correct)

How can I do to make the third test to fail?


Solution

  • The value "" is a valid query string parameter. String values would not be passed in quotes via a URL.

    For example, if you want to receive the word something as the parameter, it would be passed as /api/ServiceBusOutput?parameter=something.

    If you are trying to test an empty string value any of the following would work.

    /api/ServiceBusOutput?parameter= /api/ServiceBusOutput