forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRequestHeadTests.cs
111 lines (90 loc) · 4.25 KB
/
RequestHeadTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using NUnit.Framework;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class RequestHeadTests
{
private const string BASE_URL = "http://localhost:8888/";
[SetUp]
public void SetupRequestHeadTests()
{
RequestHeadCapturer.Initialize();
}
[Test]
public void Does_Not_Pass_Default_Credentials_When_Server_Does_Not_Negotiate()
{
const Method httpMethod = Method.GET;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>()))
{
RestClient client = new RestClient(BASE_URL);
RestRequest request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
{
UseDefaultCredentials = true
};
client.Execute(request);
Assert.NotNull(RequestHeadCapturer.CapturedHeaders);
string[] keys = RequestHeadCapturer.CapturedHeaders.Keys.Cast<string>()
.ToArray();
Assert.False(keys.Contains("Authorization"),
"Authorization header was present in HTTP request from client, even though server does not use the Negotiate scheme");
}
}
[Test]
public void Passes_Default_Credentials_When_UseDefaultCredentials_Is_True()
{
const Method httpMethod = Method.GET;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>(), AuthenticationSchemes.Negotiate))
{
RestClient client = new RestClient(BASE_URL);
RestRequest request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
{
UseDefaultCredentials = true
};
IRestResponse response = client.Execute(request);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(RequestHeadCapturer.CapturedHeaders);
string[] keys = RequestHeadCapturer.CapturedHeaders.Keys.Cast<string>()
.ToArray();
Assert.True(keys.Contains("Authorization"),
"Authorization header not present in HTTP request from client, even though UseDefaultCredentials = true");
}
}
[Test]
public void Does_Not_Pass_Default_Credentials_When_UseDefaultCredentials_Is_False()
{
const Method httpMethod = Method.GET;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestHeadCapturer>(), AuthenticationSchemes.Negotiate))
{
RestClient client = new RestClient(BASE_URL);
RestRequest request = new RestRequest(RequestHeadCapturer.RESOURCE, httpMethod)
{
// UseDefaultCredentials is currently false by default,
// but to make the test more robust in case that ever
// changes, it's better to explicitly set it here.
UseDefaultCredentials = false
};
IRestResponse response = client.Execute(request);
Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Null(RequestHeadCapturer.CapturedHeaders);
}
}
private class RequestHeadCapturer
{
public const string RESOURCE = "Capture";
public static NameValueCollection CapturedHeaders { get; set; }
public static void Initialize()
{
CapturedHeaders = null;
}
public static void Capture(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
CapturedHeaders = request.Headers;
}
}
}
}