Handle null collections when writing NegotiateResponse (#2202)
This commit is contained in:
parent
b1fccab7b1
commit
da7a95f563
|
|
@ -49,21 +49,27 @@ namespace Microsoft.AspNetCore.Http.Connections
|
|||
jsonWriter.WritePropertyName(AvailableTransportsPropertyName);
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
foreach (var availableTransport in response.AvailableTransports)
|
||||
if (response.AvailableTransports != null)
|
||||
{
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName(TransportPropertyName);
|
||||
jsonWriter.WriteValue(availableTransport.Transport);
|
||||
jsonWriter.WritePropertyName(TransferFormatsPropertyName);
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
foreach (var transferFormat in availableTransport.TransferFormats)
|
||||
foreach (var availableTransport in response.AvailableTransports)
|
||||
{
|
||||
jsonWriter.WriteValue(transferFormat);
|
||||
}
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName(TransportPropertyName);
|
||||
jsonWriter.WriteValue(availableTransport.Transport);
|
||||
jsonWriter.WritePropertyName(TransferFormatsPropertyName);
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
jsonWriter.WriteEndArray();
|
||||
jsonWriter.WriteEndObject();
|
||||
if (availableTransport.TransferFormats != null)
|
||||
{
|
||||
foreach (var transferFormat in availableTransport.TransferFormats)
|
||||
{
|
||||
jsonWriter.WriteValue(transferFormat);
|
||||
}
|
||||
}
|
||||
|
||||
jsonWriter.WriteEndArray();
|
||||
jsonWriter.WriteEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
jsonWriter.WriteEndArray();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http.Connections.Internal;
|
||||
using Microsoft.AspNetCore.Internal;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Http.Connections.Tests
|
||||
|
|
@ -50,5 +54,37 @@ namespace Microsoft.AspNetCore.Http.Connections.Tests
|
|||
|
||||
Assert.Equal(expectedMessage, exception.InnerException.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteNegotiateResponseWithNullAvailableTransports()
|
||||
{
|
||||
using (MemoryBufferWriter writer = new MemoryBufferWriter())
|
||||
{
|
||||
NegotiateProtocol.WriteResponse(new NegotiationResponse(), writer);
|
||||
|
||||
string json = Encoding.UTF8.GetString(writer.ToArray());
|
||||
|
||||
Assert.Equal("{\"availableTransports\":[]}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteNegotiateResponseWithNullTransferFormats()
|
||||
{
|
||||
using (MemoryBufferWriter writer = new MemoryBufferWriter())
|
||||
{
|
||||
NegotiateProtocol.WriteResponse(new NegotiationResponse
|
||||
{
|
||||
AvailableTransports = new List<AvailableTransport>
|
||||
{
|
||||
new AvailableTransport()
|
||||
}
|
||||
}, writer);
|
||||
|
||||
string json = Encoding.UTF8.GetString(writer.ToArray());
|
||||
|
||||
Assert.Equal("{\"availableTransports\":[{\"transport\":null,\"transferFormats\":[]}]}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue