Skip to content

Commit

Permalink
Fixed stitching serialization invalid json.
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan-joly committed Jun 27, 2020
1 parent c885e75 commit 7d16c51
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 17 deletions.
Expand Up @@ -30,10 +30,6 @@ public ArrayWriter()

public void Advance(int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
_start += count;
_capacity -= count;
}
Expand Down
Expand Up @@ -235,13 +235,13 @@ await httpClient.PostAsync(default(Uri), requestContent)
break;

case IntValueNode i:
jsonWriter.Flush();
WriteNumberValue(writer, i.Value);
jsonWriter.WriteStringValue(i.Value);
RemoveQuotes(writer, jsonWriter, i.Value.Length);
break;

case FloatValueNode f:
jsonWriter.Flush();
WriteNumberValue(writer, f.Value);
jsonWriter.WriteStringValue(f.Value);
RemoveQuotes(writer, jsonWriter, f.Value.Length);
break;

case BooleanValueNode b:
Expand All @@ -255,16 +255,13 @@ await httpClient.PostAsync(default(Uri), requestContent)
}
}

private static void WriteNumberValue(IBufferWriter<byte> writer, string value)
private static void RemoveQuotes(IBufferWriter<byte> writer, Utf8JsonWriter jsonWriter, int length)
{
Span<byte> span = writer.GetSpan(value.Length);

for (int i = 0; i < value.Length; i++)
{
span[i] = (byte)value[i];
}

writer.Advance(value.Length);
jsonWriter.Flush();
writer.Advance(-(length + 2));
Span<byte> span = writer.GetSpan(length + 2);
span.Slice(1, length + 1).CopyTo(span);
writer.Advance(length);
}
}
}
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Language;
using Moq;
using Newtonsoft.Json;
using Xunit;

namespace HotChocolate.Stitching.Utilities
{
public class HttpQueryClientTests
{
private readonly HttpQueryClient sut;

public HttpQueryClientTests()
{
sut = new HttpQueryClient();
}

[Fact]
public async Task FetchAsync_Sends_Valid_Json_Request()
{
// arrange
var messageHandler = new FakeMessageHandler();

var httpClient = new HttpClient(messageHandler)
{
BaseAddress = new Uri("http://some.url")
};

IReadOnlyQueryRequest query = QueryRequestBuilder.New()
.SetQuery(
@"query aQuery {
foo
bar
}")
.AddVariableValue("strVar", new StringValueNode("some-string"))
.AddVariableValue("intVal", new IntValueNode(42))
.AddVariableValue("floatVal", new FloatValueNode(1.23m))
.AddVariableValue("boolVal", new BooleanValueNode(true))
.AddVariableValue("listVal",
new ListValueNode(
new ReadOnlyCollection<FloatValueNode>(
new List<FloatValueNode>
{
new FloatValueNode(1.23m),
new FloatValueNode(1.80m),
new FloatValueNode(2.80m)
}) ))
.AddVariableValue("listStringVal",
new ListValueNode(
new ReadOnlyCollection<StringValueNode>(
new List<StringValueNode>
{
new StringValueNode("a"),
new StringValueNode("b"),
new StringValueNode("c")
})))
.AddVariableValue("enumVal", new EnumValueNode(System.Net.HttpStatusCode.OK))
.AddVariableValue("otherStrVar", new StringValueNode("some-other-string"))
.Create();

// act
await sut.FetchAsync(
query,
httpClient);

// assert
try
{
dynamic requestObj = JsonConvert.DeserializeObject(messageHandler.requestBody);
Assert.NotNull(requestObj);
Assert.NotNull(requestObj.query);
Assert.NotNull(requestObj.variables);
}
catch (Exception e)
{
Assert.True(false, $"Unable to parse request as json: {e.Message}");
}
}

private class FakeMessageHandler : HttpMessageHandler
{
public string requestBody;

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
requestBody = await request.Content.ReadAsStringAsync();

var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("{}")
};
return response;
}
}
}
}

0 comments on commit 7d16c51

Please sign in to comment.