|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Net;
|
9 | 9 | using System.Net.Sockets;
|
| 10 | +using System.Text.Json; |
10 | 11 | using Microsoft.Azure.WebJobs.Script.Configuration;
|
11 | 12 | using Microsoft.Azure.WebJobs.Script.Diagnostics;
|
12 | 13 | using Microsoft.Azure.WebJobs.Script.Workers;
|
@@ -374,6 +375,87 @@ public void GetUnusedTcpPort_Succeeds()
|
374 | 375 | }
|
375 | 376 | }
|
376 | 377 |
|
| 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 | + |
377 | 459 | private IConfiguration BuildHostJsonConfiguration(IEnvironment environment = null)
|
378 | 460 | {
|
379 | 461 | environment = environment ?? new TestEnvironment();
|
|
0 commit comments