1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Net.Http;
27 using System.Reflection;
28 using System.Runtime.InteropServices;
30 using System.Threading;
31 using System.Threading.Tasks;
35 using Xunit.Extensions;
37 namespace OpenTween.Thumbnail.Services
39 public class FoursquareCheckinTest
42 public void UrlPattern_Test()
44 // 通常のチェックイン URL (www.swarmapp.com/c/***)
45 var match = FoursquareCheckin.UrlPatternRegex.Match("https://www.swarmapp.com/c/xxxxxxxx");
46 Assert.True(match.Success);
47 Assert.Equal("xxxxxxxx", match.Groups["checkin_id"].Value);
51 public void LegacyUrlPattern_Test()
53 // 古い形式の URL (foursquare.com/***/checkin/***?s=***)
54 var match = FoursquareCheckin.LegacyUrlPatternRegex.Match("https://foursquare.com/hogehoge/checkin/xxxxxxxx?s=aaaaaaa");
55 Assert.True(match.Success);
56 Assert.Equal("xxxxxxxx", match.Groups["checkin_id"].Value);
57 Assert.Equal("aaaaaaa", match.Groups["signature"].Value);
59 // 古い形式の URL (www.swarmapp.com/***/checkin/***?s=***)
60 match = FoursquareCheckin.LegacyUrlPatternRegex.Match("https://www.swarmapp.com/hogehoge/checkin/xxxxxxxx?s=aaaaaaa");
61 Assert.True(match.Success);
62 Assert.Equal("xxxxxxxx", match.Groups["checkin_id"].Value);
63 Assert.Equal("aaaaaaa", match.Groups["signature"].Value);
67 public async Task GetThumbnailInfoAsync_NewUrlTest()
69 var handler = new HttpMessageHandlerMock();
70 using (var http = new HttpClient(handler))
72 var service = new FoursquareCheckin(http);
76 Assert.Equal(HttpMethod.Get, x.Method);
77 Assert.Equal("https://api.foursquare.com/v2/checkins/resolve",
78 x.RequestUri.GetLeftPart(UriPartial.Path));
80 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
82 Assert.Equal(ApplicationSettings.FoursquareClientId, query["client_id"]);
83 Assert.Equal(ApplicationSettings.FoursquareClientSecret, query["client_secret"]);
84 Assert.NotNull(query["v"]);
85 Assert.Equal("xxxxxxxx", query["shortId"]);
87 // リクエストに対するテストなのでレスポンスは適当に返す
88 return new HttpResponseMessage(HttpStatusCode.NotFound);
91 var post = new PostClass
96 var thumb = await service.GetThumbnailInfoAsync(
97 "https://www.swarmapp.com/c/xxxxxxxx",
98 post, CancellationToken.None);
100 Assert.Equal(0, handler.QueueCount);
105 public async Task GetThumbnailInfoAsync_LegacyUrlTest()
107 var handler = new HttpMessageHandlerMock();
108 using (var http = new HttpClient(handler))
110 var service = new FoursquareCheckin(http);
114 Assert.Equal(HttpMethod.Get, x.Method);
115 Assert.Equal("https://api.foursquare.com/v2/checkins/xxxxxxxx",
116 x.RequestUri.GetLeftPart(UriPartial.Path));
118 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
120 Assert.Equal(ApplicationSettings.FoursquareClientId, query["client_id"]);
121 Assert.Equal(ApplicationSettings.FoursquareClientSecret, query["client_secret"]);
122 Assert.NotNull(query["v"]);
123 Assert.Null(query["signature"]);
125 // リクエストに対するテストなのでレスポンスは適当に返す
126 return new HttpResponseMessage(HttpStatusCode.NotFound);
129 var post = new PostClass
134 var thumb = await service.GetThumbnailInfoAsync(
135 "https://foursquare.com/hogehoge/checkin/xxxxxxxx",
136 post, CancellationToken.None);
138 Assert.Equal(0, handler.QueueCount);
143 public async Task GetThumbnailInfoAsync_LegacyUrlWithSignatureTest()
145 var handler = new HttpMessageHandlerMock();
146 using (var http = new HttpClient(handler))
148 var service = new FoursquareCheckin(http);
152 Assert.Equal(HttpMethod.Get, x.Method);
153 Assert.Equal("https://api.foursquare.com/v2/checkins/xxxxxxxx",
154 x.RequestUri.GetLeftPart(UriPartial.Path));
156 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
158 Assert.Equal(ApplicationSettings.FoursquareClientId, query["client_id"]);
159 Assert.Equal(ApplicationSettings.FoursquareClientSecret, query["client_secret"]);
160 Assert.NotNull(query["v"]);
161 Assert.Equal("aaaaaaa", query["signature"]);
163 // リクエストに対するテストなのでレスポンスは適当に返す
164 return new HttpResponseMessage(HttpStatusCode.NotFound);
167 var post = new PostClass
172 var thumb = await service.GetThumbnailInfoAsync(
173 "https://foursquare.com/hogehoge/checkin/xxxxxxxx?s=aaaaaaa",
174 post, CancellationToken.None);
176 Assert.Equal(0, handler.QueueCount);
181 public async Task GetThumbnailInfoAsync_GeoLocatedTweetTest()
183 var handler = new HttpMessageHandlerMock();
184 using (var http = new HttpClient(handler))
186 var service = new FoursquareCheckin(http);
192 return new HttpResponseMessage(HttpStatusCode.NotFound);
195 // 既にジオタグが付いているツイートに対しては何もしない
196 var post = new PostClass
198 PostGeo = new PostClass.StatusGeo(134.04693603515625, 34.35067978344854),
201 var thumb = await service.GetThumbnailInfoAsync(
202 "https://www.swarmapp.com/c/xxxxxxxx",
203 post, CancellationToken.None);
205 Assert.Equal(1, handler.QueueCount);
210 public void ParseInLocation_Test()
213 ""meta"": { ""code"": 200 },
216 ""id"": ""xxxxxxxxx"",
217 ""type"": ""checkin"",
219 ""id"": ""4b73dedcf964a5206bbe2de3"",
220 ""name"": ""高松駅 (Takamatsu Sta.)"",
222 ""lat"": 34.35067978344854,
223 ""lng"": 134.04693603515625
229 var jsonBytes = Encoding.UTF8.GetBytes(json);
230 var location = FoursquareCheckin.ParseIntoLocation(jsonBytes);
232 Assert.NotNull(location);
233 Assert.Equal(34.35067978344854, location.Latitude);
234 Assert.Equal(134.04693603515625, location.Longitude);
238 public void ParseInLocation_PlanetTest()
241 ""meta"": { ""code"": 200 },
244 ""id"": ""xxxxxxxxx"",
245 ""type"": ""checkin"",
247 ""id"": ""5069d8bdc640385aa7711fe4"",
248 ""name"": ""Gale Crater"",
250 ""planet"": ""mars"",
258 var jsonBytes = Encoding.UTF8.GetBytes(json);
259 var location = FoursquareCheckin.ParseIntoLocation(jsonBytes);
261 // 地球以外の位置にあるベニューに対しては null を返す
262 Assert.Null(location);
266 public void ParseInLocation_VenueNullTest()
269 ""meta"": { ""code"": 200 },
272 ""id"": ""xxxxxxxxx"",
273 ""type"": ""checkin"",
278 var jsonBytes = Encoding.UTF8.GetBytes(json);
279 var location = FoursquareCheckin.ParseIntoLocation(jsonBytes);
281 // ベニュー情報が得られなかった場合は null を返す
282 Assert.Null(location);