From: Kimura Youichi Date: Fri, 27 May 2016 17:51:27 +0000 (+0900) Subject: PostLazyAsync(uri, param, media) のparam, mediaがnullの場合に例外が発生する不具合を修正 X-Git-Tag: OpenTween_v1.3.3~49 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=4147cb79222f60ad4544bc594a4e62e1a6a92230;p=opentween%2Fopen-tween.git PostLazyAsync(uri, param, media) のparam, mediaがnullの場合に例外が発生する不具合を修正 Fixes: 4359037e ("IApiConnectionにmultipart/form-dataを送信するためのメソッドを追加") --- diff --git a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs index ebd07098..a36b6706 100644 --- a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs +++ b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs @@ -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(x.Content); + + var boundary = x.Content.Headers.ContentType.Parameters.Cast() + .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(endpoint, param: null, media: null) + .ConfigureAwait(false); + + Assert.Equal("hogehoge", await result.LoadJsonAsync() + .ConfigureAwait(false)); + + Assert.Equal(0, mockHandler.QueueCount); + } + } } } diff --git a/OpenTween/Connection/TwitterApiConnection.cs b/OpenTween/Connection/TwitterApiConnection.cs index 5932a245..4b03c31d 100644 --- a/OpenTween/Connection/TwitterApiConnection.cs +++ b/OpenTween/Connection/TwitterApiConnection.cs @@ -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;