Exemplo n.º 1
0
        static async Task RunAsync()
        {
            client.BaseAddress = new Uri("http://localhost:8384/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                string   validUrl = "http://www.google.com";
                URLInput url      = new URLInput
                {
                    url = validUrl
                };
                ShortLink shortLinkResponse;
                shortLinkResponse = await CreateShortLinkPostJsonAsync(url);

                Console.WriteLine($"Posted Json and received  {JsonConvert.SerializeObject(shortLinkResponse)}");

                string hashID = shortLinkResponse.HashID;
                string expectedRedirectUrl = shortLinkResponse.Url;
                string redirectUrl         = await GetURLRedirectAsync(hashID);

                Console.WriteLine($"Get {hashID}, expected {expectedRedirectUrl} and received {redirectUrl}");

                string invalidHashID = "abc";
                string invalidHashIDErrorResponse = await GetInvalidHashIDAsync(invalidHashID);

                Console.WriteLine($"Get invalid HashID and received {invalidHashIDErrorResponse}");

                string invalidUrl = "www.google.com";
                url = new URLInput
                {
                    url = invalidUrl
                };
                string urlErrorResponse = await CreateInvalidUrlPostJson(url);

                Console.WriteLine($"Posted invalid URL Json without protocal and received {urlErrorResponse}");

                invalidUrl = "http://.";
                url        = new URLInput
                {
                    url = invalidUrl
                };
                urlErrorResponse = await CreateInvalidUrlPostJson(url);

                Console.WriteLine($"Posted invalid URL Json with protocol and received {urlErrorResponse}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 2
0
        static async Task <string> CreateInvalidUrlPostJson(URLInput url)
        {
            string        serializedUrl = JsonConvert.SerializeObject(url);
            StringContent encodedUrl    = new StringContent(
                serializedUrl, UnicodeEncoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync("api/links", encodedUrl);

            string responseString = null;

            if (response != null)
            {
                responseString = await response.Content.ReadAsStringAsync();
            }
            return(responseString);
        }
Exemplo n.º 3
0
        static async Task <ShortLink> CreateShortLinkPostJsonAsync(URLInput url)
        {
            string        serializedUrl = JsonConvert.SerializeObject(url);
            StringContent encodedUrl    = new StringContent(
                serializedUrl, UnicodeEncoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync("api/links", encodedUrl);

            response.EnsureSuccessStatusCode();
            ShortLink shortLinkResponse = null;

            if (response != null)
            {
                string responseString = await response.Content.ReadAsStringAsync();

                shortLinkResponse = JsonConvert.DeserializeObject <ShortLink>(responseString);
            }
            return(shortLinkResponse);
        }