OSDN Git Service

バージョン 1.2.2-beta1 開発開始
[opentween/open-tween.git] / OpenTween.Tests / BingTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 the40san <http://sourceforge.jp/users/the40san/>
3 //           (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
4 // All rights reserved.
5 //
6 // This file is part of OpenTween.
7 //
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General public License as published by the Free
10 // Software Foundation; either version 3 of the License, or (at your option)
11 // any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General public License along
19 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
20 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 // Boston, MA 02110-1301, USA.
22
23 using System;
24 using System.Net;
25 using System.Net.Http;
26 using System.Text;
27 using System.Threading.Tasks;
28 using System.Web;
29 using Xunit;
30 using Xunit.Extensions;
31
32 namespace OpenTween
33 {
34     /// <summary>
35     /// Bingクラスのテストクラス
36     /// </summary>
37     public class BingTest
38     {
39         [Fact]
40         public async Task TranslateAsync_Test()
41         {
42             var handler = new HttpMessageHandlerMock();
43             var bing = new Bing(new HttpClient(handler));
44
45             handler.Enqueue(x =>
46             {
47                 Assert.Equal(HttpMethod.Get, x.Method);
48                 Assert.Equal("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate",
49                     x.RequestUri.GetLeftPart(UriPartial.Path));
50
51                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
52
53                 Assert.Equal("'hogehoge'", query["Text"]);
54                 Assert.Equal("'ja'", query["To"]);
55                 Assert.Equal("Raw", query["$format"]);
56
57                 return new HttpResponseMessage(HttpStatusCode.OK)
58                 {
59                     Content = new StringContent("<string>ほげほげ</string>"),
60                 };
61             });
62
63             var translatedText = await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja");
64             Assert.Equal("ほげほげ", translatedText);
65
66             Assert.Equal(0, handler.QueueCount);
67         }
68
69         [Fact]
70         public async Task TranslateAsync_HttpErrorTest()
71         {
72             var handler = new HttpMessageHandlerMock();
73             var bing = new Bing(new HttpClient(handler));
74
75             handler.Enqueue(x =>
76             {
77                 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
78             });
79
80             await TestUtils.ThrowsAsync<HttpRequestException>(async () =>
81                 await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja"));
82
83             Assert.Equal(0, handler.QueueCount);
84         }
85
86         [Theory]
87         [InlineData("af", 0)]
88         [InlineData("sq", 1)]
89         [InlineData("ja", 67)]
90         public void GetLanguageEnumFromIndex_Test(string expected, int index)
91         {
92             Assert.Equal(expected, Bing.GetLanguageEnumFromIndex(index));
93         }
94
95         [Theory]
96         [InlineData(0, "af")]
97         [InlineData(1, "sq")]
98         [InlineData(67, "ja")]
99         public void GetIndexFromLanguageEnum_Test(int expected, string lang)
100         {
101             Assert.Equal(expected, Bing.GetIndexFromLanguageEnum(lang));
102         }
103
104         [Fact]
105         public void CreateBasicAuthHeaderValue_Test()
106         {
107             var value = Bing.CreateBasicAuthHeaderValue("user", "pass");
108
109             Assert.Equal("Basic", value.Scheme);
110             Assert.Equal("user:pass", Encoding.UTF8.GetString(Convert.FromBase64String(value.Parameter)));
111         }
112     }
113 }