Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static class DefaultHttpTrigger
{
[FunctionName("DefaultHttpTrigger")]
[OpenApiOperation(operationId: "greeting", tags: new[] { "greeting" }, Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter("name", Type = typeof(string), In = ParameterLocation.Query, Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Greeting), Summary = "The response", Description = "This returns the response")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "greetings")] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
var message = $"Hello, {name}!";
var instance = new Greeting() { Message = message };
var result = new OkObjectResult(instance);
return await Task.FromResult(result).ConfigureAwait(false);
}
}
public class Greeting
{
public string Message { get; set; }
}
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI Document on Azure Functions",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:7071/api"
}
],
"paths": {
"/greetings": {
"get": {
"tags": [
"greeting"
],
"summary": "Greetings",
"description": "This shows a welcome message.",
"operationId": "greeting",
"parameters": [
{
"name": "name",
"in": "query",
"schema": {
"type": "string"
},
"x-ms-visibility": "important"
}
],
"responses": {
"200": {
"description": "This returns the response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/greeting"
}
}
},
"x-ms-summary": "The response"
}
},
"security": [
{
"function_key": [ ]
}
],
"x-ms-visibility": "important"
}
}
},
"components": {
"schemas": {
"greeting": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
},
"securitySchemes": {
"function_key": {
"type": "apiKey",
"name": "code",
"in": "query"
}
}
}
}
# Bash
func start &
bg
# PowerShell
Start-Process -NoNewWindow func start
[TestClass]
public class DefaultHttpTriggerTests
{
private HttpClient _http;
[TestInitialize]
public void Initialize()
{
this._http = new HttpClient();
}
[TestCleanup]
public void Cleanup()
{
this._http.Dispose();
}
[TestMethod]
public async Task Given_OpenApiUrl_When_Endpoint_Invoked_Then_It_Should_Return_Title()
{
// Arrange
var requestUri = "http://localhost:7071/api/openapi/v3.json";
// Act
var response = await this._http.GetStringAsync(requestUri).ConfigureAwait(false);
var doc = JsonConvert.DeserializeObject<OpenApiDocument>(response);
// Assert
doc.Should().NotBeNull();
doc.Info.Title.Should().Be("OpenAPI Document on Azure Functions");
doc.Components.Schemas.Should().ContainKey("greeting");
var schema = doc.Components.Schemas["greeting"];
schema.Type.Should().Be("object");
schema.Properties.Should().ContainKey("message");
var property = schema.Properties["message"];
property.Type.Should().Be("string");
}
}
jobs:
build_and_test:
name: Build and test
strategy:
matrix:
os: [ 'windows-latest', 'macos-latest', 'ubuntu-latest' ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Setup Azure Functions Core Tools
shell: pwsh
run: |
npm install -g azure-functions-core-tools@3 --unsafe-perm true
- name: Setup .NET SDK 3.1 LTS
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.x'
- name: Test function app (Non-Windows)
if: matrix.os != 'windows-latest'
shell: pwsh
run: |
dir
$rootDir = $pwd.Path
cd ./src/FunctionApp
Start-Process -NoNewWindow func @("start","--verbose","false")
Start-Sleep -s 60
cd $rootDir/test/FunctionApp.Tests
dotnet test . -c Debug
cd $rootDir
- name: Test function app (Windows)
if: matrix.os == 'windows-latest'
shell: pwsh
run: |
dir
$rootDir = $pwd.Path
$func = $(Get-Command func).Source.Replace(".ps1", ".cmd")
cd ./src/FunctionApp
Start-Process -NoNewWindow "$func" @("start","--verbose","false")
Start-Sleep -s 60
cd $rootDir/test/FunctionApp.Tests
dotnet test . -c Debug
cd $rootDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment