|
4 | 4 | using System.Net; |
5 | 5 | using System.Threading; |
6 | 6 | using System.Threading.Tasks; |
| 7 | +using System.Web; |
7 | 8 | using InfluxDB3.Client.Config; |
| 9 | +using InfluxDB3.Client.Test.Utils; |
8 | 10 | using InfluxDB3.Client.Write; |
9 | 11 | using WireMock.Matchers; |
10 | 12 | using WireMock.RequestBuilders; |
@@ -559,6 +561,113 @@ public void TimeoutExceededByToken() |
559 | 561 | TestWritePointsAsync(_client, cancellationToken); |
560 | 562 | } |
561 | 563 |
|
| 564 | + [Test] |
| 565 | + public async Task WriteFromUrl() |
| 566 | + { |
| 567 | + MockServer |
| 568 | + .Given(Request.Create().WithPath("/api/v3/write_lp").UsingPost()) |
| 569 | + .RespondWith(Response.Create().WithStatusCode(204)); |
| 570 | + |
| 571 | + const string token = "my-token"; |
| 572 | + const string org = "my-org"; |
| 573 | + const string db = "my-database"; |
| 574 | + const string precision = "nanosecond"; |
| 575 | + const string authSchema = "Token"; |
| 576 | + const bool writeNoSync = true; |
| 577 | + const int gzipThreshold = 2024; |
| 578 | + |
| 579 | + var parameters = HttpUtility.ParseQueryString(string.Empty); |
| 580 | + parameters.Add("database", db); |
| 581 | + parameters.Add("token", token); |
| 582 | + parameters.Add("authScheme", authSchema); |
| 583 | + parameters.Add("org", org); |
| 584 | + parameters.Add("precision", precision); |
| 585 | + parameters.Add("writeNoSync", writeNoSync.ToString().ToLower()); |
| 586 | + parameters.Add("gzipThreshold", gzipThreshold.ToString()); ; |
| 587 | + var uriBuilder = new UriBuilder(MockServerUrl); |
| 588 | + uriBuilder.Query = parameters.ToString()!; |
| 589 | + |
| 590 | + var clientConfig = new ClientConfig(uriBuilder.Uri.ToString()); |
| 591 | + Assert.That(clientConfig.WriteOptions!.GzipThreshold, Is.EqualTo(gzipThreshold)); ; |
| 592 | + |
| 593 | + _client = new InfluxDBClient(clientConfig); |
| 594 | + await _client.WritePointAsync(PointData |
| 595 | + .Measurement("cpu") |
| 596 | + .SetTag("tag", "c") |
| 597 | + .SetField("field", 1) |
| 598 | + ); |
| 599 | + |
| 600 | + var requests = MockServer.LogEntries.ToList(); |
| 601 | + using (Assert.EnterMultipleScope()) |
| 602 | + { |
| 603 | + Assert.That(requests[0].RequestMessage.Query?["db"].First(), Is.EqualTo(db)); |
| 604 | + Assert.That(requests[0].RequestMessage.Query?["org"].First(), Is.EqualTo(org)); |
| 605 | + Assert.That(requests[0].RequestMessage.Query?["precision"].First(), Is.EqualTo(precision)); |
| 606 | + Assert.That(requests[0].RequestMessage.Query?["no_sync"].First(), Is.EqualTo(writeNoSync.ToString().ToLower())); |
| 607 | + } |
| 608 | + var authHeader = requests[0].RequestMessage.Headers?["Authorization"].First().Split(" "); |
| 609 | + using (Assert.EnterMultipleScope()) |
| 610 | + { |
| 611 | + Assert.That(authHeader?[0], Is.EqualTo(authSchema)); |
| 612 | + Assert.That(authHeader?[1], Is.EqualTo(token)); |
| 613 | + } |
| 614 | + } |
| 615 | + |
| 616 | + [Test] |
| 617 | + public async Task WriteFromEnvVars() |
| 618 | + { |
| 619 | + MockServer |
| 620 | + .Given(Request.Create().WithPath("/api/v3/write_lp").UsingPost()) |
| 621 | + .RespondWith(Response.Create().WithStatusCode(204)); |
| 622 | + |
| 623 | + const string token = "my-token"; |
| 624 | + const string org = "my-org"; |
| 625 | + const string db = "my-database"; |
| 626 | + const string precision = "nanosecond"; |
| 627 | + const string authSchema = "Token"; |
| 628 | + const bool writeNoSync = true; |
| 629 | + const int gzipThreshold = 2024; |
| 630 | + |
| 631 | + var dict = new Dictionary<string, string>() |
| 632 | + { |
| 633 | + { ClientConfig.EnvInfluxHost, MockServerUrl }, |
| 634 | + { ClientConfig.EnvInfluxToken, token }, |
| 635 | + { ClientConfig.EnvInfluxOrg, org }, |
| 636 | + { ClientConfig.EnvInfluxDatabase, db }, |
| 637 | + { ClientConfig.EnvInfluxPrecision, precision }, |
| 638 | + { ClientConfig.EnvInfluxAuthScheme, authSchema }, |
| 639 | + { ClientConfig.EnvInfluxWriteNoSync, writeNoSync.ToString().ToLower() }, |
| 640 | + { ClientConfig.EnvInfluxGzipThreshold, gzipThreshold.ToString() } |
| 641 | + }; |
| 642 | + |
| 643 | + TestUtils.SetEnv(dict); |
| 644 | + |
| 645 | + var clientConfig = new ClientConfig(dict); |
| 646 | + Assert.That(clientConfig.WriteOptions!.GzipThreshold, Is.EqualTo(gzipThreshold)); |
| 647 | + |
| 648 | + _client = new InfluxDBClient(clientConfig); |
| 649 | + await _client.WritePointAsync(PointData |
| 650 | + .Measurement("cpu") |
| 651 | + .SetTag("tag", "c") |
| 652 | + .SetField("field", 1) |
| 653 | + ); |
| 654 | + |
| 655 | + var requests = MockServer.LogEntries.ToList(); |
| 656 | + using (Assert.EnterMultipleScope()) |
| 657 | + { |
| 658 | + Assert.That(requests[0].RequestMessage.Query?["db"].First(), Is.EqualTo(db)); |
| 659 | + Assert.That(requests[0].RequestMessage.Query?["org"].First(), Is.EqualTo(org)); |
| 660 | + Assert.That(requests[0].RequestMessage.Query?["precision"].First(), Is.EqualTo(precision)); |
| 661 | + Assert.That(requests[0].RequestMessage.Query?["no_sync"].First(), Is.EqualTo(writeNoSync.ToString().ToLower())); |
| 662 | + } |
| 663 | + var authHeader = requests[0].RequestMessage.Headers?["Authorization"].First().Split(" "); |
| 664 | + using (Assert.EnterMultipleScope()) |
| 665 | + { |
| 666 | + Assert.That(authHeader?[0], Is.EqualTo(authSchema)); |
| 667 | + Assert.That(authHeader?[1], Is.EqualTo(token)); |
| 668 | + } |
| 669 | + } |
| 670 | + |
562 | 671 | private static void TestWriteRecordAsync(InfluxDBClient client, CancellationToken? cancellationToken = null) |
563 | 672 | { |
564 | 673 | Assert.ThrowsAsync<TaskCanceledException>(async () => |
|
0 commit comments