forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthenticationTests.cs
165 lines (126 loc) · 6.12 KB
/
AuthenticationTests.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Net;
using System.Text;
using NUnit.Framework;
using RestSharp.Authenticators;
using RestSharp.Extensions.MonoHttp;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class AuthenticationTests
{
[Test]
public void Can_Authenticate_With_Basic_Http_Auth()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, UsernamePasswordEchoHandler))
{
RestClient client = new RestClient(baseUrl)
{
Authenticator = new HttpBasicAuthenticator("testuser", "testpassword")
};
RestRequest request = new RestRequest("test");
IRestResponse response = client.Execute(request);
Assert.AreEqual("testuser|testpassword", response.Content);
}
}
private static void UsernamePasswordEchoHandler(HttpListenerContext context)
{
string header = context.Request.Headers["Authorization"];
string[] parts = Encoding.ASCII.GetString(Convert.FromBase64String(header.Substring("Basic ".Length)))
.Split(':');
context.Response.OutputStream.WriteStringUtf8(string.Join("|", parts));
}
//[Test]
public void Can_Authenticate_With_OAuth()
{
Uri baseUrl = new Uri("https://api.twitter.com");
RestClient client = new RestClient(baseUrl)
{
Authenticator = OAuth1Authenticator.ForRequestToken("CONSUMER_KEY", "CONSUMER_SECRET")
};
RestRequest request = new RestRequest("oauth/request_token");
IRestResponse response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
NameValueCollection qs = HttpUtility.ParseQueryString(response.Content);
string oauthToken = qs["oauth_token"];
string oauthTokenSecret = qs["oauth_token_secret"];
Assert.NotNull(oauthToken);
Assert.NotNull(oauthTokenSecret);
request = new RestRequest("oauth/authorize?oauth_token=" + oauthToken);
string url = client.BuildUri(request)
.ToString();
Process.Start(url);
const string verifier = "123456"; // <-- Breakpoint here (set verifier in debugger)
request = new RestRequest("oauth/access_token");
client.Authenticator = OAuth1Authenticator.ForAccessToken(
"P5QziWtocYmgWAhvlegxw", "jBs07SIxJ0kodeU9QtLEs1W1LRgQb9u5Lc987BA94", oauthToken,
oauthTokenSecret, verifier);
response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
qs = HttpUtility.ParseQueryString(response.Content);
oauthToken = qs["oauth_token"];
oauthTokenSecret = qs["oauth_token_secret"];
Assert.NotNull(oauthToken);
Assert.NotNull(oauthTokenSecret);
request = new RestRequest("account/verify_credentials.xml");
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
"P5QziWtocYmgWAhvlegxw", "jBs07SIxJ0kodeU9QtLEs1W1LRgQb9u5Lc987BA94", oauthToken,
oauthTokenSecret);
response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
//[Test]
//public void Can_Obtain_OAuth_Request_Token()
//{
// var baseUrl = "http://term.ie/oauth/example";
// var client = new RestClient(baseUrl);
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret");
// var request = new RestRequest("request_token.php");
// var response = client.Execute(request);
// Assert.NotNull(response);
// Assert.AreEqual("oauth_token=requestkey&oauth_token_secret=requestsecret", response.Content);
//}
//[Test]
//public void Can_Obtain_OAuth_Access_Token()
//{
// var baseUrl = "http://term.ie/oauth/example";
// var client = new RestClient(baseUrl);
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "requestkey", "requestsecret");
// var request = new RestRequest("access_token.php");
// var response = client.Execute(request);
// Assert.NotNull(response);
// Assert.AreEqual("oauth_token=accesskey&oauth_token_secret=accesssecret", response.Content);
//}
//[Test]
//public void Can_Make_Authenticated_OAuth_Call_With_Parameters()
//{
// var baseUrl = "http://term.ie/oauth/example";
// var client = new RestClient(baseUrl);
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "accesskey", "accesssecret");
// var request = new RestRequest("echo_api.php");
// request.AddParameter("foo", "bar");
// request.AddParameter("fizz", "pop");
// var response = client.Execute(request);
// Assert.NotNull(response);
// Assert.AreEqual("fizz=pop&foo=bar", response.Content);
//}
//[Test]
//public void Can_Make_Authenticated_OAuth_Call()
//{
// var baseUrl = "http://term.ie/oauth/example";
// var client = new RestClient(baseUrl);
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "accesskey", "accesssecret");
// var request = new RestRequest("echo_api.php");
// var response = client.Execute(request);
// Assert.NotNull(response);
// Assert.AreEqual(string.Empty, response.Content);
//}
}
}