Skip to content

Commit

Permalink
Fix null reference if content-type specified but no body
Browse files Browse the repository at this point in the history
  • Loading branch information
BillHiebert authored and jonsequitur committed Apr 4, 2023
1 parent 20680e2 commit 9ca2fc3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,29 @@ public async Task can_set_body()
bodyAsString.Should().Be("{ \"key\" : \"value\", \"list\": [1, 2, 3] }");
}

[Fact]
public async Task can_set_contenttype_without_a_body()
{
HttpRequestMessage request = null;
var handler = new InterceptingHttpMessageHandler((message, _) =>
{
request = message;
var response = new HttpResponseMessage(HttpStatusCode.OK);
return Task.FromResult(response);
});
var client = new HttpClient(handler);
using var kernel = new HttpRequestKernel(client: client);

var result = await kernel.SendAsync(new SubmitCode(@"
Get https://location1.com:1200/endpoint
Authorization: Basic username password
Content-Type: application/json
"));

result.Events.Should().NotContainErrors();
request.Content.Headers.ContentType.ToString().Should().Be("application/json");
}

[Fact]
public async Task can_use_symbols_in_body()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public async Task HandleAsync(SubmitCode command, KernelInvocationContext contex
switch (kvp.Key.ToLowerInvariant())
{
case "content-type":
if (message.Content is null)
{
message.Content = new StringContent(httpRequest.Body);
}
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(kvp.Value);
break;
case "accept":
Expand Down Expand Up @@ -275,7 +279,7 @@ private IEnumerable<HttpRequest> ParseRequests(string requests)
verb = parts[0].Trim();
address = parts[1].Trim();
}
else if (!string.IsNullOrWhiteSpace(line) && IsHeader.Matches(line) is { } matches)
else if (!string.IsNullOrWhiteSpace(line) && IsHeader.Matches(line) is { } matches && matches.Count != 0)
{
foreach (Match match in matches)
{
Expand Down

0 comments on commit 9ca2fc3

Please sign in to comment.