root / trunk / hammock / src / net35 / Hammock.Tests / RestClientTests.cs @ 0eea575a
History | View | Annotate | Download (8.5 kB)
1 |
using System; |
---|---|
2 |
using System.Configuration; |
3 |
using System.Diagnostics; |
4 |
using System.Net; |
5 |
using System.Text; |
6 |
using Hammock.Authentication.OAuth; |
7 |
using Hammock.Web; |
8 |
using NUnit.Framework; |
9 |
|
10 |
namespace Hammock.Tests |
11 |
{ |
12 |
[TestFixture] |
13 |
public class RestClientTests |
14 |
{ |
15 |
private string _consumerKey; |
16 |
private string _consumerSecret; |
17 |
private string _accessToken; |
18 |
private string _accessTokenSecret; |
19 |
private string _twitPicKey; |
20 |
|
21 |
[SetUp] |
22 |
public void SetUp() |
23 |
{ |
24 |
_consumerKey = ConfigurationManager.AppSettings["OAuthConsumerKey"]; |
25 |
_consumerSecret = ConfigurationManager.AppSettings["OAuthConsumerSecret"]; |
26 |
_accessToken = ConfigurationManager.AppSettings["OAuthAccessToken"]; |
27 |
_accessTokenSecret = ConfigurationManager.AppSettings["OAuthAccessTokenSecret"]; |
28 |
_twitPicKey = ConfigurationManager.AppSettings["TwitPicKey"]; |
29 |
|
30 |
ServicePointManager.Expect100Continue = false; |
31 |
} |
32 |
|
33 |
[Test] |
34 |
public void Can_request_get() |
35 |
{ |
36 |
var client = new RestClient |
37 |
{ |
38 |
Authority = "https://api.twitter.com", |
39 |
UserAgent = "Hammock" |
40 |
}; |
41 |
|
42 |
var request = new RestRequest |
43 |
{ |
44 |
Path = "statuses/public_timeline.json", |
45 |
DecompressionMethods = DecompressionMethods.GZip | |
46 |
DecompressionMethods.Deflate |
47 |
}; |
48 |
|
49 |
var response = client.Request(request); |
50 |
|
51 |
Assert.IsNotNull(response); |
52 |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
53 |
} |
54 |
|
55 |
[Test] |
56 |
public void Can_request_get_with_header_on_client() |
57 |
{ |
58 |
var client = new RestClient |
59 |
{ |
60 |
Authority = "https://api.twitter.com", |
61 |
UserAgent = "Hammock", |
62 |
Path = "statuses/public_timeline.json", |
63 |
DecompressionMethods = DecompressionMethods.GZip | DecompressionMethods.Deflate |
64 |
}; |
65 |
|
66 |
client.AddHeader("Accept", "application/json"); |
67 |
|
68 |
var response = client.Request(); |
69 |
|
70 |
Assert.IsNotNull(response); |
71 |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
72 |
} |
73 |
|
74 |
[Test] |
75 |
public void Can_get_oauth_request_token() |
76 |
{ |
77 |
var client = new RestClient |
78 |
{ |
79 |
Authority = "https://api.twitter.com", |
80 |
UserAgent = "Hammock" |
81 |
}; |
82 |
|
83 |
var request = new RestRequest |
84 |
{ |
85 |
Path = "oauth/request_token", |
86 |
Credentials = OAuthCredentials.ForRequestToken(_consumerKey, _consumerSecret) |
87 |
}; |
88 |
|
89 |
var response = client.Request(request); |
90 |
Assert.IsNotNull(response); |
91 |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
92 |
|
93 |
Trace.WriteLine(response.Content); |
94 |
} |
95 |
|
96 |
[Test] |
97 |
public void Can_post_to_protected_resource() |
98 |
{ |
99 |
var client = new RestClient |
100 |
{ |
101 |
Authority = "https://api.twitter.com", |
102 |
VersionPath = "1", |
103 |
UserAgent = "Hammock" |
104 |
}; |
105 |
|
106 |
var request = new RestRequest |
107 |
{ |
108 |
Method = WebMethod.Post, |
109 |
Path = "statuses/update.json?status=" + DateTime.Now.Ticks, |
110 |
Credentials = OAuthCredentials.ForProtectedResource( |
111 |
_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret |
112 |
) |
113 |
}; |
114 |
|
115 |
var response = client.Request(request); |
116 |
Assert.IsNotNull(response); |
117 |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
118 |
} |
119 |
|
120 |
[Test] |
121 |
public void Can_post_raw_content() |
122 |
{ |
123 |
var client = new RestClient {Authority = "http://www.apitize.com"}; |
124 |
var request = new RestRequest(); |
125 |
request.AddPostContent(Encoding.UTF8.GetBytes("Babbabooey!")); |
126 |
|
127 |
client.Request(request); |
128 |
} |
129 |
|
130 |
[Test] |
131 |
public void Can_stream_photo_over_delegated_credentials() |
132 |
{ |
133 |
var client = new RestClient |
134 |
{ |
135 |
Authority = "http://api.twitpic.com", |
136 |
VersionPath = "2", |
137 |
UserAgent = "Hammock" |
138 |
}; |
139 |
|
140 |
var request = PrepareEchoRequest(); |
141 |
request.Method = WebMethod.Get; |
142 |
request.Path = "upload.xml"; |
143 |
request.AddField("key", _twitPicKey); |
144 |
request.AddFile("media", "failwhale", "_failwhale.jpg", "image/jpeg"); |
145 |
|
146 |
var response = client.Request(request); |
147 |
Assert.IsNotNull(response); |
148 |
Console.WriteLine(response.Content); |
149 |
} |
150 |
|
151 |
[Test] |
152 |
[Ignore("Makes a live status update")] |
153 |
public void Can_prepare_oauth_with_url_parameters() |
154 |
{ |
155 |
var client = new RestClient |
156 |
{ |
157 |
Authority = "http://api.twitter.com", |
158 |
UserAgent = "Hammock" |
159 |
}; |
160 |
|
161 |
var credentials = OAuthCredentials.ForProtectedResource(_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret); |
162 |
credentials.ParameterHandling = OAuthParameterHandling.UrlOrPostParameters; |
163 |
|
164 |
var request = new RestRequest |
165 |
{ |
166 |
Path = "statuses/update.json", |
167 |
Method = WebMethod.Post, |
168 |
Credentials = credentials |
169 |
}; |
170 |
|
171 |
request.AddParameter("status", DateTime.Now.Ticks.ToString()); |
172 |
request.AddParameter("test", "value"); |
173 |
|
174 |
var response = client.Request(request); |
175 |
Assert.IsNotNull(response); |
176 |
|
177 |
Console.WriteLine(response.Content); |
178 |
|
179 |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
180 |
} |
181 |
|
182 |
#if NET40 |
183 |
[Test] |
184 |
public void Can_make_dynamic_request_for_collection() |
185 |
{ |
186 |
var client = new RestClient |
187 |
{ |
188 |
Authority = "https://api.twitter.com", |
189 |
UserAgent = "Hammock" |
190 |
}; |
191 |
|
192 |
var request = new RestRequest |
193 |
{ |
194 |
Path = "statuses/public_timeline.json", |
195 |
Method = WebMethod.Get |
196 |
}; |
197 |
|
198 |
var response = client.RequestDynamic(request); |
199 |
Assert.IsNotNull(response); |
200 |
foreach (var tweet in response.ContentEntity) |
201 |
{ |
202 |
Assert.IsNotNull(tweet); |
203 |
Assert.IsNotNullOrEmpty(tweet.Text); |
204 |
|
205 |
Can_handle_nested_dynamic_json(tweet); |
206 |
|
207 |
Console.WriteLine(tweet.Text); |
208 |
} |
209 |
} |
210 |
|
211 |
private static void Can_handle_nested_dynamic_json(dynamic tweet) |
212 |
{ |
213 |
var firstLevel = tweet.User; |
214 |
Assert.IsNotNull(firstLevel); |
215 |
|
216 |
var secondLevel = firstLevel.ScreenName; |
217 |
Assert.IsNotNull(secondLevel); |
218 |
} |
219 |
|
220 |
[Test] |
221 |
public void Can_make_dynamic_request_for_single() |
222 |
{ |
223 |
var client = new RestClient |
224 |
{ |
225 |
Authority = "https://api.twitter.com", |
226 |
UserAgent = "Hammock" |
227 |
}; |
228 |
|
229 |
var request = new RestRequest |
230 |
{ |
231 |
Path = "users/show.json?screen_name=hammockrest", |
232 |
Method = WebMethod.Get |
233 |
}; |
234 |
|
235 |
var response = client.RequestDynamic(request).ContentEntity; |
236 |
Assert.IsNotNull(response); |
237 |
Assert.IsNotNull(response.ScreenName); |
238 |
} |
239 |
#endif |
240 |
|
241 |
public RestRequest PrepareEchoRequest() |
242 |
{ |
243 |
var client = new RestClient |
244 |
{ |
245 |
Authority = "https://api.twitter.com", |
246 |
VersionPath = "1", |
247 |
UserAgent = "TweetSharp" |
248 |
}; |
249 |
|
250 |
var request = new RestRequest |
251 |
{ |
252 |
Method = WebMethod.Get, |
253 |
Path = "account/verify_credentials.json", |
254 |
Credentials = OAuthCredentials.ForProtectedResource( |
255 |
_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret |
256 |
) |
257 |
}; |
258 |
|
259 |
return OAuthCredentials.DelegateWith(client, request); |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
|