Skip to content

Commit b55f19b

Browse files
committed
Fixing bug with PlainTextMediaTypeFormatter
1 parent b1ffc78 commit b55f19b

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/WebJobs.Script.WebHost/Formatting/PlaintextMediaTypeFormatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public override async Task<object> ReadFromStreamAsync(Type type, Stream readStr
4242
}
4343
}
4444

45-
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
45+
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
4646
{
4747
if (value == null)
4848
{
49-
return Task.CompletedTask;
49+
return;
5050
}
5151

5252
Encoding selectedEncoding = SelectCharacterEncoding(content.Headers);
5353
using (var writer = new StreamWriter(writeStream, selectedEncoding, bufferSize: 1024, leaveOpen: true))
5454
{
55-
return writer.WriteAsync((string)value);
55+
await writer.WriteAsync((string)value);
5656
}
5757
}
5858
}

test/WebJobs.Script.Tests/NodeEndToEndTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,44 @@ public async Task HttpTrigger_WithRawResponse_ReturnsContent(string expectedCont
396396
Assert.Equal(body.ToString(), responseBody);
397397
}
398398

399+
[Fact]
400+
public async Task HttpTrigger_GetPlainText_WithLongResponse_ReturnsExpectedResult()
401+
{
402+
HttpRequestMessage request = new HttpRequestMessage
403+
{
404+
RequestUri = new Uri(string.Format("http://localhost/api/httptrigger-scenarios")),
405+
Method = HttpMethod.Get,
406+
};
407+
request.SetConfiguration(Fixture.RequestConfiguration);
408+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
409+
410+
JObject value = new JObject()
411+
{
412+
{ "status", "200" },
413+
{ "body", new string('.', 2000) }
414+
};
415+
JObject input = new JObject()
416+
{
417+
{ "scenario", "echo" },
418+
{ "value", value }
419+
};
420+
request.Content = new StringContent(input.ToString());
421+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
422+
423+
Dictionary<string, object> arguments = new Dictionary<string, object>
424+
{
425+
{ "req", request }
426+
};
427+
await Fixture.Host.CallAsync("HttpTrigger-scenarios", arguments);
428+
429+
HttpResponseMessage response = (HttpResponseMessage)request.Properties[ScriptConstants.AzureFunctionsHttpResponseKey];
430+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
431+
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
432+
433+
string body = await response.Content.ReadAsStringAsync();
434+
Assert.Equal(2000, body.Length);
435+
}
436+
399437
[Theory]
400438
[InlineData("application/json", "\"testinput\"")]
401439
[InlineData("application/xml", "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">testinput</string>")]

0 commit comments

Comments
 (0)