Esempio n. 1
0
        public async Task ReadAsStringAsync_NoSubContent_MatchesExpected(MultipartContentToStringMode mode, bool async)
        {
            var mc = new MultipartContent("someSubtype", "theBoundary");

            Assert.Equal(
                "--theBoundary\r\n" +
                "\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode, async));
        }
Esempio n. 2
0
        public async Task ReadAsStringAsync_TwoSubContents_MatchesExpected(MultipartContentToStringMode mode, bool async)
        {
            var mc = new MultipartContent("someSubtype", "theBoundary");

            mc.Add(new ByteArrayContent(Encoding.UTF8.GetBytes("This is a ByteArrayContent")));
            mc.Add(new StringContent("This is a StringContent"));

            Assert.Equal(
                "--theBoundary\r\n" +
                "\r\n" +
                "This is a ByteArrayContent\r\n" +
                "--theBoundary\r\n" +
                "Content-Type: text/plain; charset=utf-8\r\n" +
                "\r\n" +
                "This is a StringContent\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode, async));
        }
Esempio n. 3
0
        public async Task ReadAsStringAsync_OneSubContentWithHeaders_MatchesExpected(MultipartContentToStringMode mode, bool async)
        {
            var subContent = new ByteArrayContent(Encoding.UTF8.GetBytes("This is a ByteArrayContent"));

            subContent.Headers.Add("someHeaderName", "andSomeHeaderValue");
            subContent.Headers.Add("someOtherHeaderName", new[] { "withNotOne", "ButTwoValues" });
            subContent.Headers.Add("oneMoreHeader", new[] { "withNotOne", "AndNotTwo", "butThreeValues" });

            var mc = new MultipartContent("someSubtype", "theBoundary");

            mc.Add(subContent);

            Assert.Equal(
                "--theBoundary\r\n" +
                "someHeaderName: andSomeHeaderValue\r\n" +
                "someOtherHeaderName: withNotOne, ButTwoValues\r\n" +
                "oneMoreHeader: withNotOne, AndNotTwo, butThreeValues\r\n" +
                "\r\n" +
                "This is a ByteArrayContent\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode, async));
        }
        private static async Task <string> MultipartContentToStringAsync(MultipartContent content, MultipartContentToStringMode mode)
        {
            Stream stream;

            switch (mode)
            {
            case MultipartContentToStringMode.ReadAsStreamAsync:
                stream = await content.ReadAsStreamAsync();

                break;

            default:
                stream = new MemoryStream();
                await content.CopyToAsync(stream);

                stream.Position = 0;
                break;
            }

            using (var reader = new StreamReader(stream))
            {
                return(await reader.ReadToEndAsync());
            }
        }
Esempio n. 5
0
        private static async Task<string> MultipartContentToStringAsync(MultipartContent content, MultipartContentToStringMode mode)
        {
            Stream stream;

            switch (mode)
            {
                case MultipartContentToStringMode.ReadAsStreamAsync:
                    stream = await content.ReadAsStreamAsync();
                    break;

                default:
                    stream = new MemoryStream();
                    await content.CopyToAsync(stream);
                    stream.Position = 0;
                    break;
            }

            using (var reader = new StreamReader(stream))
            {
                return await reader.ReadToEndAsync();
            }
        }
Esempio n. 6
0
        public async Task ReadAsStringAsync_TwoSubContents_MatchesExpected(MultipartContentToStringMode mode)
        {
            var mc = new MultipartContent("someSubtype", "theBoundary");
            mc.Add(new ByteArrayContent(Encoding.UTF8.GetBytes("This is a ByteArrayContent")));
            mc.Add(new StringContent("This is a StringContent"));

            Assert.Equal(
                "--theBoundary\r\n" +
                "\r\n" +
                "This is a ByteArrayContent\r\n" +
                "--theBoundary\r\n" +
                "Content-Type: text/plain; charset=utf-8\r\n" +
                "\r\n" +
                "This is a StringContent\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode));
        }
Esempio n. 7
0
        public async Task ReadAsStringAsync_OneSubContentWithHeaders_MatchesExpected(MultipartContentToStringMode mode)
        {
            var subContent = new ByteArrayContent(Encoding.UTF8.GetBytes("This is a ByteArrayContent"));
            subContent.Headers.Add("someHeaderName", "andSomeHeaderValue");
            subContent.Headers.Add("someOtherHeaderName", new[] { "withNotOne", "ButTwoValues" });
            subContent.Headers.Add("oneMoreHeader", new[] { "withNotOne", "AndNotTwo", "butThreeValues" });

            var mc = new MultipartContent("someSubtype", "theBoundary");
            mc.Add(subContent);

            Assert.Equal(
                "--theBoundary\r\n" +
                "someHeaderName: andSomeHeaderValue\r\n" +
                "someOtherHeaderName: withNotOne, ButTwoValues\r\n" +
                "oneMoreHeader: withNotOne, AndNotTwo, butThreeValues\r\n" +
                "\r\n" +
                "This is a ByteArrayContent\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode));
        }
Esempio n. 8
0
        public async Task ReadAsStringAsync_NoSubContent_MatchesExpected(MultipartContentToStringMode mode)
        {
            var mc = new MultipartContent("someSubtype", "theBoundary");

            Assert.Equal(
                "--theBoundary\r\n" +
                "\r\n" +
                "--theBoundary--\r\n",
                await MultipartContentToStringAsync(mc, mode));
        }