Skip to content

Commit 0def84b

Browse files
committed
Merge pull request #455 from ecraft/CustomUseDefaultCredentials
Added support for UseDefaultCredentials
2 parents 04f9404 + 6c3f314 commit 0def84b

File tree

10 files changed

+134
-7
lines changed

10 files changed

+134
-7
lines changed

RestSharp.IntegrationTests/Helpers/SimpleServer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ public class SimpleServer : IDisposable
1010
readonly Action<HttpListenerContext> _handler;
1111
Thread _processor;
1212

13-
public static SimpleServer Create(string url, Action<HttpListenerContext> handler)
13+
public static SimpleServer Create(string url, Action<HttpListenerContext> handler, AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous)
1414
{
15-
var server = new SimpleServer(new HttpListener { Prefixes = { url } }, handler);
15+
var listener = new HttpListener
16+
{
17+
Prefixes = { url },
18+
AuthenticationSchemes = authenticationSchemes
19+
};
20+
var server = new SimpleServer(listener, handler);
1621
server.Start();
1722
return server;
1823
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Collections.Specialized;
2+
using System.Linq;
3+
using System.Net;
4+
using RestSharp.IntegrationTests.Helpers;
5+
using Xunit;
6+
7+
namespace RestSharp.IntegrationTests
8+
{
9+
public class RequestHeadTests
10+
{
11+
private const string BASE_URL = "http://localhost:8080/";
12+
13+
public RequestHeadTests()
14+
{
15+
RequestHeadCapturer.Initialize();
16+
}
17+
18+
[Fact]
19+
public void Does_Not_Pass_Default_Credentials_When_Server_Does_Not_Negotiate()
20+
{
21+
const Method httpMethod = Method.GET;
22+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>()))
23+
{
24+
var client = new RestClient(BASE_URL);
25+
var request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
26+
{
27+
UseDefaultCredentials = true
28+
};
29+
30+
client.Execute(request);
31+
32+
Assert.NotNull(RequestHeadCapturer.CapturedHeaders);
33+
var keys = RequestHeadCapturer.CapturedHeaders.Keys.Cast<string>().ToArray();
34+
Assert.False(keys.Contains("Authorization"), "Authorization header was present in HTTP request from client, even though server does not use the Negotiate scheme");
35+
}
36+
}
37+
38+
[Fact]
39+
public void Passes_Default_Credentials_When_UseDefaultCredentials_Is_True()
40+
{
41+
const Method httpMethod = Method.GET;
42+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>(), AuthenticationSchemes.Negotiate))
43+
{
44+
var client = new RestClient(BASE_URL);
45+
var request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
46+
{
47+
UseDefaultCredentials = true
48+
};
49+
50+
var response = client.Execute(request);
51+
52+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
53+
Assert.NotNull(RequestHeadCapturer.CapturedHeaders);
54+
var keys = RequestHeadCapturer.CapturedHeaders.Keys.Cast<string>().ToArray();
55+
Assert.True(keys.Contains("Authorization"), "Authorization header not present in HTTP request from client, even though UseDefaultCredentials = true");
56+
}
57+
}
58+
59+
[Fact]
60+
public void Does_Not_Pass_Default_Credentials_When_UseDefaultCredentials_Is_False()
61+
{
62+
const Method httpMethod = Method.GET;
63+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>(), AuthenticationSchemes.Negotiate))
64+
{
65+
var client = new RestClient(BASE_URL);
66+
var request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
67+
{
68+
// UseDefaultCredentials is currently false by default, but to make the test more robust in case that ever
69+
// changes, it's better to explicitly set it here.
70+
UseDefaultCredentials = false
71+
};
72+
73+
var response = client.Execute(request);
74+
75+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
76+
Assert.Null(RequestHeadCapturer.CapturedHeaders);
77+
}
78+
}
79+
80+
private class RequestHeadCapturer
81+
{
82+
public const string RESOURCE = "Capture";
83+
84+
public static NameValueCollection CapturedHeaders { get; set; }
85+
86+
public static void Initialize()
87+
{
88+
CapturedHeaders = null;
89+
}
90+
91+
public static void Capture(HttpListenerContext context)
92+
{
93+
var request = context.Request;
94+
CapturedHeaders = request.Headers;
95+
}
96+
}
97+
}
98+
}

RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="Helpers\SimpleServer.cs" />
7777
<Compile Include="RequestBodyTests.cs" />
7878
<Compile Include="StatusCodeTests.cs" />
79+
<Compile Include="RequestHeadTests.cs" />
7980
</ItemGroup>
8081
<ItemGroup>
8182
<Content Include="Assets\Koala.jpg">

RestSharp/Http.Async.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,16 @@ partial void AddAsyncHeaderActions()
350350
#endif
351351
}
352352

353+
// TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code
354+
// TODO: duplication at the moment).
353355
private HttpWebRequest ConfigureAsyncWebRequest(string method, Uri url)
354356
{
355357
#if SILVERLIGHT
356358
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
357359
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
358360
#endif
359361
var webRequest = (HttpWebRequest)WebRequest.Create(url);
360-
webRequest.UseDefaultCredentials = false;
362+
webRequest.UseDefaultCredentials = UseDefaultCredentials;
361363

362364
AppendHeaders(webRequest);
363365
AppendCookies(webRequest);

RestSharp/Http.Sync.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,12 @@ private void WriteRequestBody(HttpWebRequest webRequest)
212212
}
213213
}
214214

215+
// TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code
216+
// TODO: duplication at the moment).
215217
private HttpWebRequest ConfigureWebRequest(string method, Uri url)
216218
{
217219
var webRequest = (HttpWebRequest)WebRequest.Create(url);
218-
webRequest.UseDefaultCredentials = false;
220+
webRequest.UseDefaultCredentials = UseDefaultCredentials;
219221
ServicePointManager.Expect100Continue = false;
220222

221223
AppendHeaders(webRequest);

RestSharp/Http.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ protected bool HasFiles
135135
/// </summary>
136136
public int? MaxRedirects { get; set; }
137137
#endif
138+
/// <summary>
139+
/// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running)
140+
/// will be sent along to the server.
141+
/// </summary>
142+
public bool UseDefaultCredentials { get; set; }
143+
138144
/// <summary>
139145
/// HTTP headers to be sent with request
140146
/// </summary>

RestSharp/IHttp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public interface IHttp
4242
X509CertificateCollection ClientCertificates { get; set; }
4343
int? MaxRedirects { get; set; }
4444
#endif
45+
bool UseDefaultCredentials { get; set; }
4546

4647
IList<HttpHeader> Headers { get; }
4748
IList<HttpParameter> Parameters { get; }

RestSharp/IRestRequest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public interface IRestRequest
118118
/// </remarks>
119119
int Attempts { get; }
120120

121+
/// <summary>
122+
/// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running)
123+
/// will be sent along to the server. The default is false.
124+
/// </summary>
125+
bool UseDefaultCredentials { get; set; }
126+
121127
#if FRAMEWORK
122128
/// <summary>
123129
/// Adds a file to the Files collection to be included with a POST or PUT request

RestSharp/RestClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ IDeserializer GetHandler(string contentType)
158158

159159
/// <summary>
160160
/// Proxy to use for requests made by this client instance.
161-
/// Passed on to underying WebRequest if set.
161+
/// Passed on to underlying WebRequest if set.
162162
/// </summary>
163163
public IWebProxy Proxy { get; set; }
164164
#endif
@@ -295,11 +295,11 @@ private string EncodeParameters(IRestRequest request, IEnumerable<Parameter> par
295295
private void ConfigureHttp(IRestRequest request, IHttp http)
296296
{
297297
http.AlwaysMultipartFormData = request.AlwaysMultipartFormData;
298+
http.UseDefaultCredentials = request.UseDefaultCredentials;
299+
http.ResponseWriter = request.ResponseWriter;
298300

299301
http.CookieContainer = CookieContainer;
300302

301-
http.ResponseWriter = request.ResponseWriter;
302-
303303
// move RestClient.DefaultParameters into Request.Parameters
304304
foreach (var p in DefaultParameters)
305305
{

RestSharp/RestRequest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public class RestRequest : IRestRequest
5151
/// </summary>
5252
public Action<Stream> ResponseWriter { get; set; }
5353

54+
/// <summary>
55+
/// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running)
56+
/// will be sent along to the server. The default is false.
57+
/// </summary>
58+
public bool UseDefaultCredentials { get; set; }
59+
5460
/// <summary>
5561
/// Default constructor
5662
/// </summary>

0 commit comments

Comments
 (0)