Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ef8bd0

Browse files
authored
Allow removal of prefix when proxying to another server (#630) (#924)
* #630 Allow removal of prefix when proxying to another server * #630 Rename replace to replace settings and ensure properties used in place of fields * #630 Update replace settings type name to ProxyUrlReplaceSettings * #630 Add admin model and update settings parser to parse new values * Fix formatting issues * #630 Ensure json mapping between admin model and internal model takes place * #630 Refactor parsing and structure of extracting new proxy url * Reduce function complexity * #630 Fix line length issues and remove try prefix from parser methods
1 parent 090e0eb commit 9ef8bd0

File tree

8 files changed

+350
-212
lines changed

8 files changed

+350
-212
lines changed
Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,76 @@
1-
namespace WireMock.Admin.Settings;
2-
3-
[FluentBuilder.AutoGenerateBuilder]
4-
public class ProxyAndRecordSettingsModel
5-
{
6-
/// <summary>
7-
/// The clientCertificate thumbprint or subject name fragment to use.
8-
/// Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
9-
/// </summary>
10-
public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
11-
12-
/// <summary>
13-
/// Defines the WebProxySettings.
14-
/// </summary>
15-
public WebProxySettingsModel WebProxySettings { get; set; }
16-
17-
/// <summary>
18-
/// Proxy requests should follow redirection (30x).
19-
/// </summary>
20-
public bool? AllowAutoRedirect { get; set; }
21-
22-
/// <summary>
23-
/// The URL to proxy.
24-
/// </summary>
25-
public string Url { get; set; }
26-
27-
/// <summary>
28-
/// Save the mapping for each request/response to the internal Mappings.
29-
/// </summary>
30-
public bool SaveMapping { get; set; }
31-
32-
/// <summary>
33-
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
34-
/// </summary>
35-
public bool SaveMappingToFile { get; set; }
36-
37-
/// <summary>
38-
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
39-
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
40-
/// </summary>
41-
public string SaveMappingForStatusCodePattern { get; set; } = "*";
42-
43-
/// <summary>
44-
/// Defines a list from headers which will be excluded from the saved mappings.
45-
/// </summary>
46-
public string[] ExcludedHeaders { get; set; }
47-
48-
/// <summary>
49-
/// Defines a list of cookies which will be excluded from the saved mappings.
50-
/// </summary>
51-
public string[] ExcludedCookies { get; set; }
52-
53-
/// <summary>
54-
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
55-
/// </summary>
56-
// public bool PreferProxyMapping { get; set; }
57-
58-
/// <summary>
59-
/// When SaveMapping is set to <c>true</c>, this setting can be used to control the behavior of the generated request matchers for the new mapping.
60-
/// - <c>false</c>, the default matchers will be used.
61-
/// - <c>true</c>, the defined mappings in the request wil be used for the new mapping.
62-
///
63-
/// Default value is false.
64-
/// </summary>
65-
public bool UseDefinedRequestMatchers { get; set; }
66-
67-
/// <summary>
68-
/// Append an unique GUID to the filename from the saved mapping file.
69-
/// </summary>
70-
public bool AppendGuidToSavedMappingFile { get; set; }
1+
namespace WireMock.Admin.Settings;
2+
3+
[FluentBuilder.AutoGenerateBuilder]
4+
public class ProxyAndRecordSettingsModel
5+
{
6+
/// <summary>
7+
/// The clientCertificate thumbprint or subject name fragment to use.
8+
/// Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
9+
/// </summary>
10+
public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
11+
12+
/// <summary>
13+
/// Defines the WebProxySettings.
14+
/// </summary>
15+
public WebProxySettingsModel WebProxySettings { get; set; }
16+
17+
/// <summary>
18+
/// Proxy requests should follow redirection (30x).
19+
/// </summary>
20+
public bool? AllowAutoRedirect { get; set; }
21+
22+
/// <summary>
23+
/// The URL to proxy.
24+
/// </summary>
25+
public string Url { get; set; }
26+
27+
/// <summary>
28+
/// Save the mapping for each request/response to the internal Mappings.
29+
/// </summary>
30+
public bool SaveMapping { get; set; }
31+
32+
/// <summary>
33+
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
34+
/// </summary>
35+
public bool SaveMappingToFile { get; set; }
36+
37+
/// <summary>
38+
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
39+
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
40+
/// </summary>
41+
public string SaveMappingForStatusCodePattern { get; set; } = "*";
42+
43+
/// <summary>
44+
/// Defines a list from headers which will be excluded from the saved mappings.
45+
/// </summary>
46+
public string[] ExcludedHeaders { get; set; }
47+
48+
/// <summary>
49+
/// Defines a list of cookies which will be excluded from the saved mappings.
50+
/// </summary>
51+
public string[] ExcludedCookies { get; set; }
52+
53+
/// <summary>
54+
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
55+
/// </summary>
56+
// public bool PreferProxyMapping { get; set; }
57+
58+
/// <summary>
59+
/// When SaveMapping is set to <c>true</c>, this setting can be used to control the behavior of the generated request matchers for the new mapping.
60+
/// - <c>false</c>, the default matchers will be used.
61+
/// - <c>true</c>, the defined mappings in the request wil be used for the new mapping.
62+
///
63+
/// Default value is false.
64+
/// </summary>
65+
public bool UseDefinedRequestMatchers { get; set; }
66+
67+
/// <summary>
68+
/// Append an unique GUID to the filename from the saved mapping file.
69+
/// </summary>
70+
public bool AppendGuidToSavedMappingFile { get; set; }
71+
72+
/// <summary>
73+
/// Defines the Replace Settings
74+
/// </summary>
75+
public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; }
7176
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace WireMock.Admin.Settings;
2+
3+
/// <summary>
4+
/// Defines an old path param and a new path param to be replaced when proxying.
5+
/// </summary>
6+
[FluentBuilder.AutoGenerateBuilder]
7+
public class ProxyUrlReplaceSettingsModel
8+
{
9+
/// <summary>
10+
/// The old path value to be replaced by the new path value
11+
/// </summary>
12+
public string OldValue { get; set; } = null!;
13+
14+
/// <summary>
15+
/// The new path value to replace the old value with
16+
/// </summary>
17+
public string NewValue { get; set; } = null!;
18+
}

src/WireMock.Net/Proxy/ProxyHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public ProxyHelper(WireMockServerSettings settings)
3737
var requiredUri = new Uri(url);
3838

3939
// Create HttpRequestMessage
40-
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, url);
40+
var replaceSettings = proxyAndRecordSettings.ReplaceSettings;
41+
var proxyUrl = replaceSettings is not null ? url.Replace(replaceSettings.OldValue, replaceSettings.NewValue) : url;
42+
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, proxyUrl);
4143

4244
// Call the URL
4345
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
Lines changed: 96 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,97 @@
1-
using JetBrains.Annotations;
2-
3-
namespace WireMock.Settings;
4-
5-
/// <summary>
6-
/// ProxyAndRecordSettings
7-
/// </summary>
8-
public class ProxyAndRecordSettings : HttpClientSettings
9-
{
10-
/// <summary>
11-
/// The URL to proxy.
12-
/// </summary>
13-
[PublicAPI]
14-
public string Url { get; set; } = null!;
15-
16-
/// <summary>
17-
/// Save the mapping for each request/response to the internal Mappings.
18-
/// </summary>
19-
[PublicAPI]
20-
public bool SaveMapping { get; set; }
21-
22-
/// <summary>
23-
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
24-
/// </summary>
25-
[PublicAPI]
26-
public bool SaveMappingToFile { get; set; }
27-
28-
/// <summary>
29-
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
30-
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
31-
///
32-
/// Deprecated : use SaveMappingSettings.
33-
/// </summary>
34-
[PublicAPI]
35-
public string SaveMappingForStatusCodePattern
36-
{
37-
set
38-
{
39-
if (SaveMappingSettings is null)
40-
{
41-
SaveMappingSettings = new ProxySaveMappingSettings();
42-
}
43-
44-
SaveMappingSettings.StatusCodePattern = value;
45-
}
46-
}
47-
48-
/// <summary>
49-
/// Additional SaveMappingSettings.
50-
/// </summary>
51-
[PublicAPI]
52-
public ProxySaveMappingSettings? SaveMappingSettings { get; set; }
53-
54-
/// <summary>
55-
/// Defines a list from headers which will be excluded from the saved mappings.
56-
/// </summary>
57-
[PublicAPI]
58-
public string[]? ExcludedHeaders { get; set; }
59-
60-
/// <summary>
61-
/// Defines a list of params which will be excluded from the saved mappings.
62-
/// </summary>
63-
[PublicAPI]
64-
public string[]? ExcludedParams { get; set; }
65-
66-
/// <summary>
67-
/// Defines a list of cookies which will be excluded from the saved mappings.
68-
/// </summary>
69-
[PublicAPI]
70-
public string[]? ExcludedCookies { get; set; }
71-
72-
/// <summary>
73-
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
74-
/// </summary>
75-
//[PublicAPI]
76-
//public bool PreferProxyMapping { get; set; }
77-
78-
/// <summary>
79-
/// When SaveMapping is set to <c>true</c>, this setting can be used to control the behavior of the generated request matchers for the new mapping.
80-
/// - <c>false</c>, the default matchers will be used.
81-
/// - <c>true</c>, the defined mappings in the request wil be used for the new mapping.
82-
///
83-
/// Default value is false.
84-
/// </summary>
85-
public bool UseDefinedRequestMatchers { get; set; }
86-
87-
/// <summary>
88-
/// Append an unique GUID to the filename from the saved mapping file.
89-
/// </summary>
90-
public bool AppendGuidToSavedMappingFile { get; set; }
1+
using JetBrains.Annotations;
2+
3+
namespace WireMock.Settings;
4+
5+
/// <summary>
6+
/// ProxyAndRecordSettings
7+
/// </summary>
8+
public class ProxyAndRecordSettings : HttpClientSettings
9+
{
10+
/// <summary>
11+
/// The URL to proxy.
12+
/// </summary>
13+
[PublicAPI]
14+
public string Url { get; set; } = null!;
15+
16+
/// <summary>
17+
/// Save the mapping for each request/response to the internal Mappings.
18+
/// </summary>
19+
[PublicAPI]
20+
public bool SaveMapping { get; set; }
21+
22+
/// <summary>
23+
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
24+
/// </summary>
25+
[PublicAPI]
26+
public bool SaveMappingToFile { get; set; }
27+
28+
/// <summary>
29+
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
30+
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
31+
///
32+
/// Deprecated : use SaveMappingSettings.
33+
/// </summary>
34+
[PublicAPI]
35+
public string SaveMappingForStatusCodePattern
36+
{
37+
set
38+
{
39+
if (SaveMappingSettings is null)
40+
{
41+
SaveMappingSettings = new ProxySaveMappingSettings();
42+
}
43+
44+
SaveMappingSettings.StatusCodePattern = value;
45+
}
46+
}
47+
48+
/// <summary>
49+
/// Additional SaveMappingSettings.
50+
/// </summary>
51+
[PublicAPI]
52+
public ProxySaveMappingSettings? SaveMappingSettings { get; set; }
53+
54+
/// <summary>
55+
/// Defines a list from headers which will be excluded from the saved mappings.
56+
/// </summary>
57+
[PublicAPI]
58+
public string[]? ExcludedHeaders { get; set; }
59+
60+
/// <summary>
61+
/// Defines a list of params which will be excluded from the saved mappings.
62+
/// </summary>
63+
[PublicAPI]
64+
public string[]? ExcludedParams { get; set; }
65+
66+
/// <summary>
67+
/// Defines a list of cookies which will be excluded from the saved mappings.
68+
/// </summary>
69+
[PublicAPI]
70+
public string[]? ExcludedCookies { get; set; }
71+
72+
/// <summary>
73+
/// Replace Settings
74+
/// </summary>
75+
[PublicAPI]
76+
public ProxyUrlReplaceSettings? ReplaceSettings { get; set; }
77+
78+
/// <summary>
79+
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
80+
/// </summary>
81+
//[PublicAPI]
82+
//public bool PreferProxyMapping { get; set; }
83+
84+
/// <summary>
85+
/// When SaveMapping is set to <c>true</c>, this setting can be used to control the behavior of the generated request matchers for the new mapping.
86+
/// - <c>false</c>, the default matchers will be used.
87+
/// - <c>true</c>, the defined mappings in the request wil be used for the new mapping.
88+
///
89+
/// Default value is false.
90+
/// </summary>
91+
public bool UseDefinedRequestMatchers { get; set; }
92+
93+
/// <summary>
94+
/// Append an unique GUID to the filename from the saved mapping file.
95+
/// </summary>
96+
public bool AppendGuidToSavedMappingFile { get; set; }
9197
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace WireMock.Settings;
2+
3+
/// <summary>
4+
/// Defines an old path param and a new path param to be replaced when proxying.
5+
/// </summary>
6+
public class ProxyUrlReplaceSettings
7+
{
8+
/// <summary>
9+
/// The old path value to be replaced by the new path value
10+
/// </summary>
11+
public string OldValue { get; set; } = null!;
12+
13+
/// <summary>
14+
/// The new path value to replace the old value with
15+
/// </summary>
16+
public string NewValue { get; set; } = null!;
17+
}

0 commit comments

Comments
 (0)