|
1 | 1 | // Copyright (c) Mohamed Hassan & Contributors. All rights reserved. See License.md in the project root for license information.
|
2 | 2 |
|
3 |
| -namespace OData2Poco.Fake.Common; |
4 |
| - |
| 3 | +using System.Text; |
| 4 | +using OData2Poco; |
5 | 5 | using WireMock.RequestBuilders;
|
6 | 6 | using WireMock.ResponseBuilders;
|
7 | 7 | using WireMock.Server;
|
8 | 8 |
|
9 |
| -public sealed class OdataService : IDisposable |
| 9 | +internal sealed class OdataService : IDisposable |
10 | 10 | {
|
11 |
| - private static readonly Lazy<OdataService> s_lazy = new(() => new OdataService()); |
12 |
| - private readonly WireMockServer _mockServer; |
| 11 | + private static readonly Lazy<OdataService> s_instance = new(() => |
| 12 | + new OdataService()); |
| 13 | + internal static OdataService Instance => s_instance.Value; |
| 14 | + public static string BaseAddress => Instance._mockServer.Urls[0]; |
| 15 | + internal static bool IsStarted => Instance._mockServer.IsStarted; |
| 16 | + public static string Trippin => Instance.TrippinUrl; |
| 17 | + public static string TrippinBasic => Instance.TrippinBasicUrl; |
| 18 | + public static string TrippinBearer => Instance.TrippinBearerUrl; |
| 19 | + public static string Northwind => Instance.NorthwindUrl; |
| 20 | + public static string Northwind4 => Instance.Northwind4Url; |
| 21 | + public static string Northwind3 => Instance.Northwind3Url; |
| 22 | + public static string Northwind2 => Instance.Northwind2Url; |
| 23 | + |
| 24 | + internal readonly WireMockServer _mockServer; |
| 25 | + |
| 26 | +#if TESTLIB |
| 27 | + private readonly int _port = 5678; |
| 28 | +#else |
| 29 | + private readonly int _port = 5679; |
| 30 | +#endif |
13 | 31 | private bool _disposedValue;
|
14 | 32 |
|
15 | 33 | private OdataService()
|
16 | 34 | {
|
17 |
| - _mockServer = WireMockServer.Start(); |
18 |
| - CreateGzipService("/trippin", TestSample.TripPin4Gzip); |
19 |
| - CreateService("/northwind", TestSample.NorthWindV4); |
20 |
| - CreateService("/v4/northwind", TestSample.NorthWindV4); |
21 |
| - CreateService("/v3/northwind", TestSample.NorthWindV3); |
22 |
| - CreateService("/v2/northwind", TestSample.NorthWindV2); |
| 35 | + var setting = new WireMock.Settings.WireMockServerSettings |
| 36 | + { |
| 37 | + Port = 0, |
| 38 | + StartAdminInterface = true, |
| 39 | + }; |
| 40 | + _mockServer = WireMockServer.Start(setting); |
| 41 | + InitializeWireMockServer(); |
| 42 | + } |
| 43 | + |
| 44 | + public string TrippinUrl { get; private set; } |
| 45 | + |
| 46 | + public string TrippinBasicUrl { get; private set; } |
| 47 | + public string TrippinBearerUrl { get; private set; } |
| 48 | + public string NorthwindUrl { get; private set; } |
| 49 | + public string Northwind4Url { get; private set; } |
| 50 | + public string Northwind3Url { get; private set; } |
| 51 | + public string Northwind2Url { get; private set; } |
| 52 | + |
| 53 | + public void ShowWireMockServerInfo() |
| 54 | + { |
| 55 | + var isStarted = IsStarted; |
| 56 | + if (!isStarted) |
| 57 | + { |
| 58 | + throw new OData2PocoException("Failed to start OData service"); |
| 59 | + } |
| 60 | + Console.WriteLine("OData service is started"); |
| 61 | + Console.WriteLine($"OData service is running on {_mockServer.Urls[0]}"); |
| 62 | + Console.WriteLine($"Trippin: {Trippin}"); |
| 63 | + Console.WriteLine($"TrippinBasic: {TrippinBasic}"); |
| 64 | + Console.WriteLine($"TrippinBearer: {TrippinBearer}"); |
| 65 | + Console.WriteLine($"Northwind: {Northwind}"); |
| 66 | + Console.WriteLine($"Northwind2: {Northwind2}"); |
| 67 | + Console.WriteLine($"Northwind3: {Northwind3}"); |
| 68 | + Console.WriteLine($"Northwind4: {Northwind4}"); |
| 69 | + } |
| 70 | + private void InitializeWireMockServer() |
| 71 | + { |
| 72 | + TrippinUrl = CreateGzipService("/trippin", TestSample.TripPin4Gzip); |
| 73 | + NorthwindUrl = CreateService("/northwind", TestSample.NorthWindV4); |
| 74 | + Northwind4Url = CreateService("/v4/northwind", TestSample.NorthWindV4); |
| 75 | + Northwind3Url = CreateService("/v3/northwind", TestSample.NorthWindV3); |
| 76 | + Northwind2Url = CreateService("/v2/northwind", TestSample.NorthWindV2); |
| 77 | + TrippinBasicUrl = CreateServiceWithBasicAuth("/trippin/basic", TestSample.TripPin4); |
| 78 | + TrippinBearerUrl = CreateServiceWithBearerTokenAuth("/trippin/bearer", TestSample.TripPin4); |
23 | 79 | }
|
24 | 80 |
|
25 |
| - private void CreateService(string url, string body) |
| 81 | + |
| 82 | + private string CreateService(string path, string body) |
26 | 83 | {
|
27 | 84 | _mockServer
|
28 |
| - .Given(Request.Create().WithPath($"{url}/$metadata")) |
| 85 | + .Given(Request.Create().WithPath($"{path}/$metadata").UsingGet()) |
29 | 86 | .RespondWith(Response.Create()
|
30 | 87 | .WithStatusCode(200)
|
| 88 | + .WithHeader("Content-Type", "application/xml") |
31 | 89 | .WithBodyFromFile(body));
|
| 90 | + return $"{_mockServer.Urls[0]}{path}"; |
32 | 91 | }
|
33 | 92 |
|
34 |
| - private void CreateGzipService(string url, string body) |
| 93 | + private string CreateGzipService(string path, string body) |
35 | 94 | {
|
36 | 95 | _mockServer
|
37 |
| - .Given(Request.Create().WithPath($"{url}/$metadata")) |
| 96 | + .Given(Request.Create().WithPath($"{path}/$metadata").UsingGet()) |
38 | 97 | .RespondWith(Response.Create()
|
39 | 98 | .WithStatusCode(200)
|
40 | 99 | .WithHeader("Content-Encoding", "gzip")
|
41 | 100 | .WithBodyFromFile(body));
|
| 101 | + return $"{_mockServer.Urls[0]}{path}"; |
42 | 102 | }
|
43 | 103 |
|
44 |
| - public static string Trippin => $"{Instance._mockServer.Urls[0]}/trippin"; |
45 |
| - |
46 |
| - public static string Northwind => $"{Instance._mockServer.Urls[0]}/northwind"; |
47 |
| - public static string Northwind4 => $"{Instance._mockServer.Urls[0]}/v4/northwind"; |
48 |
| - public static string Northwind3 => $"{Instance._mockServer.Urls[0]}/v3/northwind"; |
49 |
| - public static string Northwind2 => $"{Instance._mockServer.Urls[0]}/v2/northwind"; |
50 |
| - internal static OdataService Instance => s_lazy.Value; |
| 104 | + private string CreateServiceWithBasicAuth(string path, string body, string user = "user", string password = "secret") |
| 105 | + { |
| 106 | + _mockServer |
| 107 | + .Given(Request.Create().WithPath($"{path}/$metadata").UsingGet() |
| 108 | + .WithHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{password}")))) |
| 109 | + .RespondWith(Response.Create() |
| 110 | + .WithStatusCode(200) |
| 111 | + .WithHeader("Content-Type", "application/xml") |
| 112 | + .WithBodyFromFile(body)); |
| 113 | + return $"{_mockServer.Urls[0]}{path}"; |
| 114 | + } |
51 | 115 |
|
52 |
| - internal static bool IsStarted => Instance._mockServer.IsStarted; |
| 116 | + private string CreateServiceWithBearerTokenAuth(string path, |
| 117 | + string body, |
| 118 | + string token = "secret_token") |
| 119 | + { |
| 120 | + _mockServer |
| 121 | + .Given(Request.Create().WithPath($"{path}/$metadata").UsingGet() |
| 122 | + .WithHeader("Authorization", $"Bearer {token}")) |
| 123 | + .RespondWith(Response.Create() |
| 124 | + .WithStatusCode(200) |
| 125 | + .WithHeader("Content-Type", "application/xml") |
| 126 | + .WithBodyFromFile(body)); |
| 127 | + return $"{_mockServer.Urls[0]}{path}"; |
| 128 | + } |
53 | 129 |
|
54 | 130 | public void Dispose()
|
55 | 131 | {
|
56 | 132 | Dispose(true);
|
| 133 | + GC.SuppressFinalize(this); |
57 | 134 | }
|
58 | 135 |
|
59 | 136 | private void Dispose(bool disposing)
|
|
0 commit comments