예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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)));
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
 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;
     }
 }
예제 #7
0
 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
 }
예제 #8
0
파일: Tween.cs 프로젝트: nanayaT/OpenTween
        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;
            }
        }
예제 #9
0
 public void GetIndexFromLanguageEnum_Test(int expected, string lang)
 => Assert.Equal(expected, Bing.GetIndexFromLanguageEnum(lang));
예제 #10
0
 public void GetLanguageEnumFromIndex_Test(string expected, int index)
 => Assert.Equal(expected, Bing.GetLanguageEnumFromIndex(index));
예제 #11
0
 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
 }
예제 #12
0
        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");
            }
        }
예제 #13
0
파일: Tween.cs 프로젝트: Mitsui/OpenTween
        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;
            }
        }