0

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?

Pine Code
  • 2,466
  • 3
  • 18
  • 43
  • 3
    The reason it fails is because the value of "" is a valid value for a parameter from a purely URI perspective. The behavior of the Azure function is also correct, you need to take care of the logic inside the function – Mandar Dharmadhikari May 07 '20 at 10:45
  • Have you tried [`string.IsNullOrWhiteSpace(string)`](https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netcore-3.1) ? BTW `%22` is a urlencoded `"` – Fildor May 07 '20 at 11:23

1 Answers1

0

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

Matt Hensley
  • 908
  • 8
  • 18