コード例 #1
0
        public void TranslateRequest_EmptyTranslatorModel()
        {
            IRequestBuilder requestBuilder = new TranslatorRequestBuilder(_apiKey);
            var             requestModels  = new List <TranslatorRequestModel>();

            Assert.ThrowsException <ArgumentNullException>(() => requestBuilder.BuildTranslateRequest("fr", "en", requestModels));
        }
コード例 #2
0
        public void TranslateRequest_ValidRequest()
        {
            IRequestBuilder requestBuilder = new TranslatorRequestBuilder(_apiKey);
            var             requestModels  = new List <TranslatorRequestModel>();

            requestModels.Add(new TranslatorRequestModel()
            {
                Text = "C'est un exemple français."
            });
            string from = "fr", to = "en";
            var    request = requestBuilder.BuildTranslateRequest(from, to, requestModels);

            Assert.IsTrue(request.Headers.Contains("Ocp-Apim-Subscription-Key"));
            Assert.AreEqual(_apiKey, request.Headers.GetValues("Ocp-Apim-Subscription-Key").FirstOrDefault());

            Assert.AreEqual(TranslateUrl + $"&from={from}&to={to}", request.RequestUri.ToString());
            Assert.AreEqual(request.Content.ToString(), new StringContent(JsonConvert.SerializeObject(requestModels), Encoding.UTF8, "application/json").ToString());
        }