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.
6 // This file is part of OpenTween.
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)
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
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.
25 using System.Net.Http;
27 using System.Threading.Tasks;
30 using Xunit.Extensions;
40 public async Task TranslateAsync_Test()
42 var handler = new HttpMessageHandlerMock();
43 var bing = new Bing(new HttpClient(handler));
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));
51 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
53 Assert.Equal("'hogehoge'", query["Text"]);
54 Assert.Equal("'ja'", query["To"]);
55 Assert.Equal("Raw", query["$format"]);
57 return new HttpResponseMessage(HttpStatusCode.OK)
59 Content = new StringContent("<string>ほげほげ</string>"),
63 var translatedText = await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja");
64 Assert.Equal("ほげほげ", translatedText);
66 Assert.Equal(0, handler.QueueCount);
70 public async Task TranslateAsync_HttpErrorTest()
72 var handler = new HttpMessageHandlerMock();
73 var bing = new Bing(new HttpClient(handler));
77 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
80 await TestUtils.ThrowsAsync<HttpRequestException>(async () =>
81 await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja"));
83 Assert.Equal(0, handler.QueueCount);
89 [InlineData("ja", 67)]
90 public void GetLanguageEnumFromIndex_Test(string expected, int index)
92 Assert.Equal(expected, Bing.GetLanguageEnumFromIndex(index));
98 [InlineData(67, "ja")]
99 public void GetIndexFromLanguageEnum_Test(int expected, string lang)
101 Assert.Equal(expected, Bing.GetIndexFromLanguageEnum(lang));
105 public void CreateBasicAuthHeaderValue_Test()
107 var value = Bing.CreateBasicAuthHeaderValue("user", "pass");
109 Assert.Equal("Basic", value.Scheme);
110 Assert.Equal("user:pass", Encoding.UTF8.GetString(Convert.FromBase64String(value.Parameter)));