public async Task TranslateAsync_Test() { var handler = new HttpMessageHandlerMock(); var bing = new Bing(new HttpClient(handler)); handler.Enqueue(x => { Assert.Equal(HttpMethod.Get, x.Method); Assert.Equal("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate", x.RequestUri.GetLeftPart(UriPartial.Path)); var query = HttpUtility.ParseQueryString(x.RequestUri.Query); Assert.Equal("'hogehoge'", query["Text"]); Assert.Equal("'ja'", query["To"]); Assert.Equal("Raw", query["$format"]); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("<string>ほげほげ</string>"), }); }); var translatedText = await bing.TranslateAsync("hogehoge", langFrom : null, langTo : "ja"); Assert.Equal("ほげほげ", translatedText); Assert.Equal(0, handler.QueueCount); }
public async Task TranslateAsync_Test() { var handler = new HttpMessageHandlerMock(); var bing = new Bing(new HttpClient(handler)); handler.Enqueue(x => { Assert.Equal(HttpMethod.Get, x.Method); Assert.Equal("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate", x.RequestUri.GetLeftPart(UriPartial.Path)); var query = HttpUtility.ParseQueryString(x.RequestUri.Query); Assert.Equal("'hogehoge'", query["Text"]); Assert.Equal("'ja'", query["To"]); Assert.Equal("Raw", query["$format"]); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("<string>ほげほげ</string>"), }; }); var translatedText = await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja"); Assert.Equal("ほげほげ", translatedText); Assert.Equal(0, handler.QueueCount); }
public void CreateBasicAuthHeaderValue_Test() { var value = Bing.CreateBasicAuthHeaderValue("user", "pass"); Assert.Equal("Basic", value.Scheme); Assert.Equal("user:pass", Encoding.UTF8.GetString(Convert.FromBase64String(value.Parameter))); }
public async Task TranslateAsync_HttpErrorTest() { var handler = new HttpMessageHandlerMock(); var bing = new Bing(new HttpClient(handler)); handler.Enqueue(x => { return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable); }); await Assert.ThrowsAsync<HttpRequestException>(async () => await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja")); Assert.Equal(0, handler.QueueCount); }
public async Task TranslateAsync_HttpErrorTest() { var handler = new HttpMessageHandlerMock(); var bing = new Bing(new HttpClient(handler)); handler.Enqueue(x => { return(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)); }); await TestUtils.ThrowsAsync <HttpRequestException>(async() => await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja")); Assert.Equal(0, handler.QueueCount); }
private void doTranslation(string str) { Bing _bing = new Bing(); string buf = ""; if (string.IsNullOrEmpty(str)) return; string srclng = ""; string dstlng = SettingDialog.TranslateLanguage; string msg = ""; if (srclng != dstlng && _bing.Translate("", dstlng, str, out buf)) { PostBrowser.DocumentText = createDetailHtml(buf); } else { if (msg.StartsWith("Err:")) StatusLabel.Text = msg; } }
public void TestInit() { bing = new Bing(); //リフレクション使ってインスタンスから取得するようにしたい #region 言語テーブル定義 LanguageTable = new List<string>() { "af", "sq", "ar-sa", "ar-iq", "ar-eg", "ar-ly", "ar-dz", "ar-ma", "ar-tn", "ar-om", "ar-ye", "ar-sy", "ar-jo", "ar-lb", "ar-kw", "ar-ae", "ar-bh", "ar-qa", "eu", "bg", "be", "ca", "zh-tw", "zh-cn", "zh-hk", "zh-sg", "hr", "cs", "da", "nl", "nl-be", "en", "en-us", "en-gb", "en-au", "en-ca", "en-nz", "en-ie", "en-za", "en-jm", "en", "en-bz", "en-tt", "et", "fo", "fa", "fi", "fr", "fr-be", "fr-ca", "fr-ch", "fr-lu", "gd", "ga", "de", "de-ch", "de-at", "de-lu", "de-li", "el", "he", "hi", "hu", "is", "id", "it", "it-ch", "ja", "ko", "ko", "lv", "lt", "mk", "ms", "mt", "no", "no", "pl", "pt-br", "pt", "rm", "ro", "ro-mo", "ru", "ru-mo", "sz", "sr", "sr", "sk", "sl", "sb", "es", "es-mx", "es-gt", "es-cr", "es-pa", "es-do", "es-ve", "es-co", "es-pe", "es-ar", "es-ec", "es-cl", "es-uy", "es-py", "es-bo", "es-sv", "es-hn", "es-ni", "es-pr", "sx", "sv", "sv-fi", "th", "ts", "tn", "tr", "uk", "ur", "ve", "vi", "xh", "ji", "zu" }; #endregion }
private async Task doTranslation(string str) { if (string.IsNullOrEmpty(str)) return; var bing = new Bing(); try { var translatedText = await bing.TranslateAsync(str, langFrom: null, langTo: this._cfgCommon.TranslateLanguage); this.PostBrowser.DocumentText = this.createDetailHtml(translatedText); } catch (HttpRequestException e) { this.StatusLabel.Text = "Err:" + e.Message; } }
public void GetIndexFromLanguageEnum_Test(int expected, string lang) => Assert.Equal(expected, Bing.GetIndexFromLanguageEnum(lang));
public void GetLanguageEnumFromIndex_Test(string expected, int index) => Assert.Equal(expected, Bing.GetLanguageEnumFromIndex(index));
public void TestInit() { bing = new Bing(); //リフレクション使ってインスタンスから取得するようにしたい #region 言語テーブル定義 LanguageTable = new List <string>() { "af", "sq", "ar-sa", "ar-iq", "ar-eg", "ar-ly", "ar-dz", "ar-ma", "ar-tn", "ar-om", "ar-ye", "ar-sy", "ar-jo", "ar-lb", "ar-kw", "ar-ae", "ar-bh", "ar-qa", "eu", "bg", "be", "ca", "zh-tw", "zh-cn", "zh-hk", "zh-sg", "hr", "cs", "da", "nl", "nl-be", "en", "en-us", "en-gb", "en-au", "en-ca", "en-nz", "en-ie", "en-za", "en-jm", "en", "en-bz", "en-tt", "et", "fo", "fa", "fi", "fr", "fr-be", "fr-ca", "fr-ch", "fr-lu", "gd", "ga", "de", "de-ch", "de-at", "de-lu", "de-li", "el", "he", "hi", "hu", "is", "id", "it", "it-ch", "ja", "ko", "ko", "lv", "lt", "mk", "ms", "mt", "no", "no", "pl", "pt-br", "pt", "rm", "ro", "ro-mo", "ru", "ru-mo", "sz", "sr", "sr", "sk", "sl", "sb", "es", "es-mx", "es-gt", "es-cr", "es-pa", "es-do", "es-ve", "es-co", "es-pe", "es-ar", "es-ec", "es-cl", "es-uy", "es-py", "es-bo", "es-sv", "es-hn", "es-ni", "es-pr", "sx", "sv", "sv-fi", "th", "ts", "tn", "tr", "uk", "ur", "ve", "vi", "xh", "ji", "zu" }; #endregion }
private async Task DoTranslation(string str) { if (string.IsNullOrEmpty(str)) return; var bing = new Bing(); try { var translatedText = await bing.TranslateAsync(str, langFrom: null, langTo: SettingCommon.Instance.TranslateLanguage); this.PostBrowser.DocumentText = this.Owner.createDetailHtml(translatedText); } catch (HttpRequestException e) { this.RaiseStatusChanged("Err:" + e.Message); } catch (OperationCanceledException) { this.RaiseStatusChanged("Err:Timeout"); } }
private async Task doTranslation(string str) { if (string.IsNullOrEmpty(str)) return; var bing = new Bing(this.http); try { var translatedText = await bing.TranslateAsync(str, langFrom: null, langTo: this.SettingDialog.TranslateLanguage); this.PostBrowser.DocumentText = translatedText; } catch (HttpRequestException e) { this.StatusLabel.Text = "Err:" + e.Message; } }