Skip to content

Commit 9c79a2e

Browse files
committed
Accept proxy URL without trailing slash
1 parent d582c3c commit 9c79a2e

6 files changed

Lines changed: 53 additions & 15 deletions

File tree

smsapiTests/Integration/IntegrationTestBase.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@ namespace smsapiTests.Integration;
1010

1111
public abstract class IntegrationTestBase
1212
{
13+
protected virtual bool AutostartServer => true;
1314
private static string _currentHost;
14-
15+
1516
[TestInitialize]
1617
public void InitializeServer()
1718
{
18-
RunTestServer();
19+
if (!AutostartServer) return;
20+
RunTestServer(FreeHost());
1921
}
2022

21-
private static void RunTestServer()
23+
protected void InitializeServer(string host)
2224
{
23-
_currentHost = FreeHost();
24-
25+
RunTestServer(host);
26+
}
27+
28+
private static void RunTestServer(string host)
29+
{
30+
_currentHost = host;
31+
2532
new WebHostBuilder()
2633
.UseKestrel()
2734
.UseStartup(typeof(Program))
@@ -36,7 +43,7 @@ protected static ProxyHTTP GetProxy()
3643
return new ProxyHTTP(_currentHost);
3744
}
3845

39-
private static string FreeHost()
46+
protected static string FreeHost()
4047
{
4148
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
4249
l.Start();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SMSApi.Api;
3+
4+
namespace smsapiTests.Integration;
5+
6+
[TestClass]
7+
public class ProxyPathTest : IntegrationTestBase
8+
{
9+
protected override bool AutostartServer => true;
10+
11+
[TestMethod]
12+
public void proxy_adds_trailing_slash()
13+
{
14+
var client = new ClientOAuth("any");
15+
var host = FreeHost();
16+
InitializeServer(host);
17+
var smsFactory = new SMSFactory(client, GetProxy());
18+
19+
SendAnyMessage(smsFactory);
20+
21+
var expectedUri = host + "/sms.do";
22+
RequestAssert.AsserRawPath(expectedUri);
23+
}
24+
25+
private static void SendAnyMessage(SMSFactory smsFactory)
26+
{
27+
SendActionHelper.SendAnySms(smsFactory);
28+
}
29+
}

smsapiTests/Integration/RequestAssert.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Text.RegularExpressions;
31
using Microsoft.VisualStudio.TestTools.UnitTesting;
42

53
namespace smsapiTests.Integration;
@@ -24,13 +22,15 @@ public static void AssertContainsUserAgentHeader(string value)
2422
Assert.IsTrue(headerExists, $"Expected {value}, Found: {RequestStorage.UserAgentHeader}");
2523
}
2624

27-
public static void AsserPath(string path)
25+
public static void AsserRawPath(string path)
2826
{
27+
Assert.IsNotNull(RequestStorage.Path, "Missing request path");
28+
2929
var pathEquals = RequestStorage
30-
.Path
30+
.RawPath
3131
.Equals(path);
3232

33-
Assert.IsTrue(pathEquals);
33+
Assert.IsTrue(pathEquals, "Found: " + RequestStorage.RawPath);
3434
}
3535

3636
public static void AssertContainsFormParameter(string name, string value)

smsapiTests/Integration/RequestInterceptorMiddleware.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Collections.Immutable;
21
using System.Linq;
3-
using System.Text.Json;
42
using System.Threading.Tasks;
53
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Http.Extensions;
65

76
namespace smsapiTests.Integration;
87

@@ -20,8 +19,9 @@ public async Task InvokeAsync(HttpContext context)
2019
RequestStorage.Method = context.Request.Method;
2120
RequestStorage.AuthorizationHeader = context.Request.Headers.Authorization;
2221
RequestStorage.UserAgentHeader = context.Request.Headers.UserAgent;
23-
RequestStorage.FormParameters = context.Request.Form.ToDictionary(k => k.Key, v => v.Value.ToString());
2422
RequestStorage.Path = $"{context.Request.Scheme}://{context.Request.Host.Value}{context.Request.Path.Value}";
23+
RequestStorage.RawPath = context.Request.GetDisplayUrl();
24+
RequestStorage.FormParameters = context.Request.Form.ToDictionary(k => k.Key, v => v.Value.ToString());
2525

2626
await _next(context);
2727
}

smsapiTests/Integration/RequestStorage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public static class RequestStorage
77
public static string AuthorizationHeader;
88
public static string UserAgentHeader;
99
public static string Path;
10+
public static string RawPath;
1011
public static Dictionary<string, string> FormParameters;
1112
public static string Method;
1213
}

smsapiTests/Integration/SendActionHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public static void SendAnySms(SMSFactory smsFactory)
1111
{
1212
smsFactory.ActionSend("48500100100", "any").Execute();
1313
}
14-
catch (MissingMethodException)
14+
catch (MissingMethodException e)
1515
{
16+
Console.WriteLine(@"Error sending message: " + e.Message);
1617
}
1718
}
1819

0 commit comments

Comments
 (0)