Skip to content

Commit 8ef03dc

Browse files
authored
Update HttpWorkerOptions (#11175)
1 parent 501f5b8 commit 8ef03dc

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- Fix startup deadlock on transient exceptions (#11142)
99
- Add warning log for end of support bundle version, any bundle version < 4 (#11075), (#11160)
1010
- Handles loading extensions.json with empty extensions(#11174)
11+
- Update HttpWorkerOptions to implement IOptionsFormatter (#11175)

src/WebJobs.Script/Workers/Http/Configuration/HttpWorkerOptions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
using Microsoft.Azure.WebJobs.Hosting;
58

69
namespace Microsoft.Azure.WebJobs.Script.Workers.Http
710
{
8-
public class HttpWorkerOptions
11+
public class HttpWorkerOptions : IOptionsFormatter
912
{
1013
public CustomHandlerType Type { get; set; }
1114

@@ -29,5 +32,14 @@ public class HttpWorkerOptions
2932
public bool EnableProxyingHttpRequest { get; set; }
3033

3134
public TimeSpan InitializationTimeout { get; set; } = TimeSpan.FromSeconds(30);
35+
36+
public string Format()
37+
{
38+
return JsonSerializer.Serialize(this, typeof(HttpWorkerOptions), HttpWorkerOptionsJsonSerializerContext.Default);
39+
}
3240
}
33-
}
41+
42+
[JsonSourceGenerationOptions(WriteIndented = true)]
43+
[JsonSerializable(typeof(HttpWorkerOptions))]
44+
internal partial class HttpWorkerOptionsJsonSerializerContext : JsonSerializerContext;
45+
}

src/WebJobs.Script/Workers/Http/Configuration/HttpWorkerOptionsSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Configure(HttpWorkerOptions options)
4646
ConfigureWorkerDescription(options, customHandlerSection);
4747
if (options.Type == CustomHandlerType.None)
4848
{
49-
// CustomHandlerType.None is only for maintaining backward compatability with httpWorker section.
49+
// CustomHandlerType.None is only for maintaining backward compatibility with httpWorker section.
5050
_logger.LogWarning($"CustomHandlerType {CustomHandlerType.None} is not supported. Defaulting to {CustomHandlerType.Http}.");
5151
options.Type = CustomHandlerType.Http;
5252
}

test/WebJobs.Script.Tests/Configuration/HttpWorkerOptionsSetupTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Net;
99
using System.Net.Sockets;
10+
using System.Text.Json;
1011
using Microsoft.Azure.WebJobs.Script.Configuration;
1112
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1213
using Microsoft.Azure.WebJobs.Script.Workers;
@@ -374,6 +375,87 @@ public void GetUnusedTcpPort_Succeeds()
374375
}
375376
}
376377

378+
[Fact]
379+
public void Format_SerializesOptionsToJson()
380+
{
381+
var options = new HttpWorkerOptions
382+
{
383+
Type = CustomHandlerType.Http,
384+
Port = 8080,
385+
EnableForwardingHttpRequest = true,
386+
EnableProxyingHttpRequest = false,
387+
InitializationTimeout = TimeSpan.FromSeconds(45),
388+
Description = new HttpWorkerDescription
389+
{
390+
DefaultExecutablePath = "node",
391+
DefaultWorkerPath = "server.js",
392+
WorkingDirectory = "/app"
393+
},
394+
Arguments = new WorkerProcessArguments
395+
{
396+
ExecutablePath = "node",
397+
WorkerPath = "server.js"
398+
}
399+
};
400+
401+
string json = options.Format();
402+
403+
Assert.NotNull(json);
404+
Assert.NotEmpty(json);
405+
406+
var jsonDocument = JsonDocument.Parse(json);
407+
Assert.NotNull(jsonDocument);
408+
409+
var root = jsonDocument.RootElement;
410+
Assert.True(root.TryGetProperty("Type", out var typeProperty));
411+
Assert.Equal(0, typeProperty.GetInt32()); // CustomHandlerType.Http = 0
412+
413+
Assert.True(root.TryGetProperty("Port", out var portProperty));
414+
Assert.Equal(8080, portProperty.GetInt32());
415+
416+
Assert.True(root.TryGetProperty("EnableForwardingHttpRequest", out var forwardingProperty));
417+
Assert.True(forwardingProperty.GetBoolean());
418+
419+
Assert.True(root.TryGetProperty("EnableProxyingHttpRequest", out var proxyingProperty));
420+
Assert.False(proxyingProperty.GetBoolean());
421+
422+
Assert.True(root.TryGetProperty("InitializationTimeout", out var timeoutProperty));
423+
Assert.Equal("00:00:45", timeoutProperty.GetString());
424+
425+
Assert.True(root.TryGetProperty("Description", out var descriptionProperty));
426+
Assert.Equal(JsonValueKind.Object, descriptionProperty.ValueKind);
427+
428+
Assert.True(root.TryGetProperty("Arguments", out var argumentsProperty));
429+
Assert.Equal(JsonValueKind.Object, argumentsProperty.ValueKind);
430+
}
431+
432+
[Fact]
433+
public void Format_WithNullProperties_SerializesSuccessfully()
434+
{
435+
var options = new HttpWorkerOptions
436+
{
437+
Type = CustomHandlerType.None,
438+
Port = 0,
439+
Description = null,
440+
Arguments = null
441+
};
442+
443+
string json = options.Format();
444+
445+
Assert.NotNull(json);
446+
Assert.NotEmpty(json);
447+
448+
var jsonDocument = JsonDocument.Parse(json);
449+
Assert.NotNull(jsonDocument);
450+
451+
var root = jsonDocument.RootElement;
452+
Assert.True(root.TryGetProperty("Type", out var typeProperty));
453+
Assert.Equal(1, typeProperty.GetInt32()); // CustomHandlerType.None = 1
454+
455+
Assert.True(root.TryGetProperty("Port", out var portProperty));
456+
Assert.Equal(0, portProperty.GetInt32());
457+
}
458+
377459
private IConfiguration BuildHostJsonConfiguration(IEnvironment environment = null)
378460
{
379461
environment = environment ?? new TestEnvironment();

0 commit comments

Comments
 (0)