Last active
April 21, 2023 10:55
-
-
Save TsuyoshiUshio/d4e619f9123a9b521ac3cbe6804cc647 to your computer and use it in GitHub Desktop.
Azure Functions Unit Testing sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[FunctionName(“HttpTrigger”)] | |
public async static Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, “get”, “post”, Route = null)]HttpRequest req, TraceWriter log) | |
{ | |
log.Info(“C# HTTP trigger function processed a request.”); | |
string name = req.Query[“name”]; | |
string requestBody = new StreamReader(req.Body).ReadToEnd(); | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
name = name ?? data?.name; | |
return name != null | |
? (ActionResult)new OkObjectResult($”Hello, {name}”) | |
: new BadRequestObjectResult(“Please pass a name on the query string or in the request body”); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class HttpTriggerTest : FunctionTestHelper.FunctionTest | |
{ | |
[TestMethod] | |
public async Task Request_With_Query() | |
{ | |
var query = new Dictionary<String, StringValues>(); | |
query.TryAdd(“name”, “ushio”); | |
var body = “”; | |
var result = await HttpTrigger.RunAsync(req: HttpRequestSetup(query, body), log: log); | |
var resultObject = (OkObjectResult)result; | |
Assert.AreEqual(“Hello, ushio”, resultObject.Value); | |
: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment