OSDN Git Service

PostLazyAsync(uri, param, media) のparam, mediaがnullの場合に例外が発生する不具合を修正
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 27 May 2016 17:51:27 +0000 (02:51 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 27 May 2016 19:42:52 +0000 (04:42 +0900)
Fixes: 4359037e ("IApiConnectionにmultipart/form-dataを送信するためのメソッドを追加")

OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
OpenTween/Connection/TwitterApiConnection.cs

index ebd0709..a36b670 100644 (file)
@@ -407,5 +407,54 @@ namespace OpenTween.Connection
                 }
             }
         }
+
+        [Fact]
+        public async Task PostLazyAsync_Multipart_NullTest()
+        {
+            using (var mockHandler = new HttpMessageHandlerMock())
+            using (var http = new HttpClient(mockHandler))
+            using (var apiConnection = new TwitterApiConnection("", ""))
+            {
+                apiConnection.http = http;
+
+                mockHandler.Enqueue(async x =>
+                {
+                    Assert.Equal(HttpMethod.Post, x.Method);
+                    Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
+                        x.RequestUri.AbsoluteUri);
+
+                    Assert.IsType<MultipartFormDataContent>(x.Content);
+
+                    var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>()
+                        .First(y => y.Name == "boundary").Value;
+
+                    // 前後のダブルクオーテーションを除去
+                    boundary = boundary.Substring(1, boundary.Length - 2);
+
+                    var expectedText =
+                        $"--{boundary}\r\n" +
+                        $"\r\n--{boundary}--\r\n";
+
+                    var expected = Encoding.UTF8.GetBytes(expectedText);
+
+                    Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
+
+                    return new HttpResponseMessage(HttpStatusCode.OK)
+                    {
+                        Content = new StringContent("\"hogehoge\""),
+                    };
+                });
+
+                var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
+
+                var result = await apiConnection.PostLazyAsync<string>(endpoint, param: null, media: null)
+                    .ConfigureAwait(false);
+
+                Assert.Equal("hogehoge", await result.LoadJsonAsync()
+                    .ConfigureAwait(false));
+
+                Assert.Equal(0, mockHandler.QueueCount);
+            }
+        }
     }
 }
index 5932a24..4b03c31 100644 (file)
@@ -175,11 +175,16 @@ namespace OpenTween.Connection
 
             using (var postContent = new MultipartFormDataContent())
             {
-                foreach (var kv in param)
-                    postContent.Add(new StringContent(kv.Value), kv.Key);
-
-                foreach (var kv in media)
-                    postContent.Add(new StreamContent(kv.Value.OpenRead()), kv.Key, kv.Value.Name);
+                if (param != null)
+                {
+                    foreach (var kv in param)
+                        postContent.Add(new StringContent(kv.Value), kv.Key);
+                }
+                if (media != null)
+                {
+                    foreach (var kv in media)
+                        postContent.Add(new StreamContent(kv.Value.OpenRead()), kv.Key, kv.Value.Name);
+                }
 
                 request.Content = postContent;