Skip to content

Commit 369c14d

Browse files
committed
Added overloaded constructor to support backwards compatibility with string parameters
1 parent 411acc3 commit 369c14d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

RestSharp.Tests/UrlBuilderTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ public void GET_with_Uri_containing_tokens()
147147
Assert.Equal(expected, output);
148148
}
149149

150+
[Fact]
151+
public void GET_with_Url_string_containing_tokens()
152+
{
153+
var request = new RestRequest();
154+
request.AddUrlSegment("foo", "bar");
155+
var client = new RestClient("http://example.com/{foo}");
156+
157+
var expected = new Uri("http://example.com/bar");
158+
var output = client.BuildUri(request);
159+
160+
Assert.Equal(expected, output);
161+
}
162+
150163
[Fact]
151164
public void GET_with_Uri_and_resource_containing_tokens()
152165
{
@@ -160,5 +173,28 @@ public void GET_with_Uri_and_resource_containing_tokens()
160173

161174
Assert.Equal(expected, output);
162175
}
176+
177+
[Fact]
178+
public void GET_with_Url_string_and_resource_containing_tokens()
179+
{
180+
var request = new RestRequest("resource/{baz}");
181+
request.AddUrlSegment("foo", "bar");
182+
request.AddUrlSegment("baz", "bat");
183+
var client = new RestClient("http://example.com/{foo}");
184+
185+
var expected = new Uri("http://example.com/bar/resource/bat");
186+
var output = client.BuildUri(request);
187+
188+
Assert.Equal(expected, output);
189+
}
190+
191+
[Fact]
192+
public void GET_with_Invalid_Url_string_throws_exception()
193+
{
194+
Assert.Throws<UriFormatException>(delegate
195+
{
196+
var client = new RestClient("invalid url");
197+
});
198+
}
163199
}
164200
}

RestSharp/RestClient.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,21 @@ public RestClient()
6464
/// Sets the BaseUrl property for requests made by this client instance
6565
/// </summary>
6666
/// <param name="baseUrl"></param>
67-
public RestClient(Uri baseUrl)
68-
: this()
67+
public RestClient(Uri baseUrl) : this()
6968
{
7069
BaseUrl = baseUrl;
7170
}
7271

72+
/// <summary>
73+
/// Sets the BaseUrl property for requests made by this client instance
74+
/// </summary>
75+
/// <param name="baseUrl"></param>
76+
public RestClient(string baseUrl) : this()
77+
{
78+
var uri = new Uri(baseUrl);
79+
BaseUrl = uri;
80+
}
81+
7382
private IDictionary<string, IDeserializer> ContentHandlers { get; set; }
7483
private IList<string> AcceptTypes { get; set; }
7584

0 commit comments

Comments
 (0)